我正在使用精灵表和关键帧在单击按钮时为按钮上的图像设置动画。
当单击按钮时,我希望帧沿一个方向运行,并将按钮保留在精灵表中的最后一个图像上,当再次单击按钮时,我希望相同的帧向后运行,将按钮保留在精灵表中的第一个图像上精灵表。
我目前正在尝试使用 jquery 将按钮上的类更改为单击时的动画类,但这似乎不起作用。
fiddle: http://jsfiddle.net/CGmCe/10295/ http://jsfiddle.net/CGmCe/10295/
JS:
function animate(){
$('.hi').addClass('animate-hi');
}
CSS:
.hi {
width: 50px;
height: 72px;
background-image: url("http://s.cdpn.io/79/sprite-steps.png");
}
.animate-hi {
animation: play 2s steps(10);
}
@keyframes play {
from { background-position: 0px; }
to { background-position: -500px; }
}
确保您使用的是支持动画的浏览器。对我来说,这在 Firefox 中有效。
以下可能正是您想要的:
http://jsfiddle.net/CGmCe/10299/ http://jsfiddle.net/CGmCe/10299/
Code:
function animateButton() {
var button = $('.hi');
if (button.hasClass('animate-hi')) {
button.removeClass('animate-hi').addClass('animate-hi-reverse');
} else if (button.hasClass('animate-hi-reverse')) {
button.removeClass('animate-hi-reverse').addClass('animate-hi');
} else {
button.addClass('animate-hi');
}
};
$(document).ready(function() {
$('.hi').on("click", function() {
animateButton();
});
});
.hi {
width: 50px;
height: 72px;
background-image: url("http://s.cdpn.io/79/sprite-steps.png");
}
.animate-hi {
animation: play 2s steps(10);
}
.animate-hi-reverse {
animation: play-reverse 2s steps(10);
}
@keyframes play {
from {
background-position: 0px;
}
to {
background-position: -500px;
}
}
@keyframes play-reverse {
from {
background-position: -500px;
}
to {
background-position: 0px;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="http://s.cdpn.io/79/sprite-steps.png" />
<button class="hi" type="button"></button>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)