SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Fixing Slider

  1. #1
    SitePoint Zealot
    Join Date
    Mar 2013
    Location
    Philippines
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Fixing Slider

    I'm trying to figure out how i can set the slider to stop at the last item. Basically the slider still slides to left even though their aren't anymore items to follow. Here is the link to my site.

    http://federalland.ph/

    The slider is located below the video presentation. Here is my code

    Code:
    <script type="text/javascript">
    $(document).ready(function(){
        
    	var $item = $('div.slider_post'),
    	    visible = 1,
    		index = 0,
    		endIndex = ( $item.length / visible ) -1
    		
    	$('input#slide_left').click(function(){
    	    if(index < endIndex ){
    		    index++;
    			$item.animate({'left':'-=235px'});
    		}
    	});
    		
    	$('input#slide_right').click(function(){
    	    if(index > 0){
    		    index--;
    			$item.animate({'left':'+=235px'});
    		    }
    	 });
    });
    </script>

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Hi there,

    I saw your post on SO, so for the sake of completeness I'll post the solution here, too:

    You are calculating the width of your slides incorrectly, as you are neglecting the margin.

    Either hard code them, or determine the width programatically:

    Code:
    var w = $item.outerWidth( true );
    
    $item.animate({'left':'-=' + w + 'px'});
    $item.animate({'left':'+=' + w + 'px'});
    Also, to have the slide stop at the last slide, you will have to change your end index variable.

    You can either hard code this as 5, or do it programatically:

    Code:
    endIndex = $item.length - Math.floor($(".slide_post").width()/w);
    Here's everything together:

    Code:
    var $item = $('div.slider_post'),
      w = $item.outerWidth( true ),
      visible = 1,
      index = 0,
      endIndex = $item.length - Math.floor($(".slide_post").width()/w);
    
    $('input#slide_left').click(function(){
      if(index < endIndex ){
        index++;
        $item.animate({'left':'-=' + w + 'px'});
    
    });
    
    $('input#slide_right').click(function(){
      if(index > 0){
        index--;
        $item.animate({'left':'+=' + w + 'px'});
      }
    });
    Here's a demo of it working.

    If you have any other questions, just let us know


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •