試試這個代碼:
<script type="text/javascript">
$(document).ready(function(){
$('#myDiv').hover(function(){
//after each 400ms
//when mouse over #myDiv class should increment from c1 > c2 > c3 > c4 >c5
//acutally its from cn > cn+1
$(function() {
var $target = $('#myDiv');
var classes = ['c1','c2', 'c3', 'c4', 'c5'];
var current = 0;
setInterval(function() {
if (current==4){
clearInterval();
}
else{
$target.removeClass(classes[current]);
current = (current+1)%classes.length;
$target.addClass(classes[current]);
}
}, 400); // 1500 ms loop
});
}, function(){
$(function() {
var $target = $('#myDiv');
var classes = ['c5', 'c4', 'c3', 'c2', 'c1'];
var current = 0;
setInterval(function() {
if (current==4){
clearInterval();
}
else{
$target.removeClass(classes[current]);
current = (current+1)%classes.length;
$target.addClass(classes[current]);
}
}, 400); // 1500 ms loop
});
});
});
</script>
這是jfiddle: http://jsfiddle.net/xDSaX/
你現在纔來設置類什麼你想..
你在c1,c2等類中有什麼風格。你可以在沒有類的情況下動畫。 – saji89
我必須將背景位置從0px更改爲200px至400px ... – coure2011
該位置是否爲一系列值,在每次後續迭代中都會加上相同的值?像'200px'每次增加? – saji89