zik
zik

Reputation: 5

Stop slide at last slide

Here is the code

I have created custom slider using 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
   })
})

Upvotes: 0

Views: 77

Answers (2)

Omar Elawady
Omar Elawady

Reputation: 3350

unbind the event haandler when reaches the last div. add this line to the first of your function.

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

Upvotes: 0

Stepashka
Stepashka

Reputation: 2698

You can add sliderPosition variable and decrease it every time you move slider. When it is 0 or 1 (based on what you want) - stop sliding. 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.

Upvotes: 2

Related Questions