2011-09-19 97 views
1

我有一个jQuery滑块,单击下一个按钮时水平滑动。但是,当它到达图像/列表序列的末尾时,它会继续滑动并且不会停止。我设法让它不从左端滑落,但右端是一个问题。jquery滑块不会停止在最后

$(document).ready(function(){ 



$("#inner").css("overflow-x", "hidden"); 

var xPos = $('#scroller li:last').position(); 
var pos = '-' + xPos.left + 'px'; 
alert(pos); 



$('#next').click(function(){ 

if(("#scroller ").css("margin-left") > pos){ 



    $('#scroller').animate({ 

      marginLeft: "-=133px" 



    }, 200) 





} 


}); 


$('#prev').click(function(){ 

    if($("#scroller").css("margin-left") < "0"){ 

     $('#scroller').animate({ 

      marginLeft: "+=133px" 



    }, 200) 


    } 
}); 

});

+1

没有什么能让它停下来。您的下一个按钮每次只增加一个133边距。您需要以某种方式设置最大边距,或者甚至更好,在达到一定边距后使右/左按钮消失。如果我有时间,我会研究一种方式,稍后发布答案。但这就是你应该射击的。 – alt

+0

我试图告诉它不会移动,除非margin-left的值大于滑块内最后一个元素的位置。 – evan

回答

1

首先,你必须在该行的错误:

if(("#scroller ").css("margin-left") > pos){ 

将其更改为

if($("#scroller ").css("margin-left") > pos){ 

如果它不帮助,也许你应该试试这个:

if(parseInt($("#scroller").css("margin-left"),0) < 0){ 

将它改为$('#prev')。click function,并且

if(parseInt($("#scroller ").css("margin-left"),10) > parseInt(pos,10)){ 

in $('#next')。点击功能