我创建了一个滑块,可根据鼠标位置(左/右)滑动。但是当滑块到达末尾时我需要停止滑动效果。我已经发现当容器达到0左或右offset
时我可以阻止它。问题是我无法弄清楚如何告诉滑块它应该在那时停止。
var withWindow = $( window ).width(),
rate = 0,
maxspeed=8,
x=0,
slider = $("#innerlist"),
direction = 0,
offsetL = 1,
offsetR =1 ,
list = $("#innerlist ul");
$(list).mousemove(function(e){
if(e.pageX < withWindow/2) //if mouse is on the left side
{
direction = 1;
}
else {direction = 0;}
if(direction ==1) // slide to left
{
rate = (withWindow/2 - e.pageX - $(this).offset().left + 1)/(withWindow/2);
}else{
rate = -(e.pageX - $(this).offset().left + 1)/(withWindow/2); //slide to right
}
});
list.hover(
function(){
var scroller = setInterval( moveList, 30 );
$(this).data('scroller', scroller);
},
function(){
var scroller = $(this).data('scroller');
clearInterval( scroller );
}
);
function moveList(){ //moving function
x += maxspeed * rate;
var newpos = x+'px';
slider.css('left',newpos);
offsetL= $("#innerlist").offset().left; //..also updating offset values
offsetR= $("#innerlist").offset().right;
}
可能我的代码有点乱,但我是初学者,所以任何帮助都会受到赞赏。谢谢!