简体   繁体   English

在最后一张幻灯片上停止幻灯片

[英]Stop slide at last slide

Here is the code 这是代码

I have created custom slider using jQuery. 我已经使用jQuery创建了自定义滑块。

I want it to stop at the last slide. 我希望它停在最后一张幻灯片上。

$(document).ready(function(){
   var slideCount = $('.billAddress .addressBlock').length
  var outerWidth = slideCount * 273
  $('.billAddressCnt').css({
    width: outerWidth,
    left: 0
  })
 })

var i = 0;
$('.nextBillAddress').click(function(){   
   i++
   var leftMove = 273*i
   $('.billAddressCnt').animate({
     left : -leftMove
   })
})

You can add sliderPosition variable and decrease it every time you move slider. 您可以添加sliderPosition变量,并在每次移动滑块时减小它。 When it is 0 or 1 (based on what you want) - stop sliding. 当它是0或1(根据需要)时-停止滑动。 The code will be like this 代码将像这样

$(document).ready(function(){
    var slideCount = $('.billAddress .addressBlock').length
    var sliderPosition = slideCount
    var outerWidth = slideCount * 273
    $('.billAddressCnt').css({
      width: outerWidth,
      left: 0
    })
})

var i = 0;
$('.nextBillAddress').click(function(){   
    i++
    if (sliderPosition == 1) {
        return false;
    }
    sliderPosition--;
    var leftMove = 273*i
    $('.billAddressCnt').animate({
        left : -leftMove
   })
})

There is also a possibility to do things using more advanced way like creating slider object, implement counter and slider there. 也可以使用更高级的方式来执行操作,例如创建滑块对象,在其中实现计数器和滑块。

unbind the event haandler when reaches the last div. 到达最后一个div时取消绑定事件处理程序。 add this line to the first of your function. 将此行添加到您的函数的第一个。

if(i + 1 === $('.addressBlock').length) $('.nextBillAddress').unbind( "click", arguments.callee );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM