目标是在悬停时连续滚动吗?
使用与动画时间相同的 setInterval,并使用线性缓动。
- 使用 jQuery 1.9.1
(function(){
var myObject = {
leftInt: '',
rightInt: '',
init: function(){
$this = this;
$(function(){
$this.eventListeners();
});
},
eventListeners: function(){
$('#prev').on("mouseenter", myObject.goLeft);
$('#prev').on("mouseleave", myObject.stopLeft);
$('#next').on("mouseenter", myObject.goRight);
$('#next').on("mouseleave", myObject.stopRight);
},
goLeft: function(){
myObject.leftInt = setInterval(myObject.slideLeft, 300);
},
goRight: function(){
myObject.rightInt = setInterval(myObject.slideRight, 300);
},
stopLeft: function(){
clearInterval(myObject.leftInt);
},
stopRight: function(){
clearInterval(myObject.rightInt);
},
slideLeft: function(){
$("#slide").stop(true, true).animate({"left":'+=20px'}, 300, 'linear');
},
slideRight: function(){
$("#slide").stop(true, true).animate({"left":'-=20px'}, 300, 'linear');
}
}
myObject.init();
})();