Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder Common's Avatar
    Join Date
    Jan 2009
    Location
    Glasgow, UK
    Posts
    133
    Thanks
    16
    Thanked 13 Times in 13 Posts

    How To Check a Value Before Executing jQuery

    First of all, let me say I am very inexperienced with jQuery so apologies for the very basic question, but I just need to get this working quickly.

    I am creating a little jQuery image slider, dead simple. When you click left or right it animates the container to the left and the overflow is hidden within the div. This is my code for the left and right arrows, what I want to know is how to stop the code running past the last image.

    Code:
    $(document).ready(function(){
    
    	$('.leftarrow').click(function() {
    		$('#folioinner').animate({
    				left: '-=805'
    		}, 800,'swing', function() {
    			// Animation complete.
    		});
    	});
    	
    	$('.rightarrow').click(function() {
    		$('#folioinner').animate({
    			left: '+=805'
    		}, 800,'swing', function() {
    			// Animation complete.
    		});
    	});
    
    });
    I tried just using a javascript to check if left was greater than a certain number, but just couldn't get anything working!

    Thanks for any help you can give!

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,182
    Thanks
    23
    Thanked 603 Times in 602 Posts
    You have to adjust the pic_count = 4; for your application and the
    function() {// Animation complete.}); does nothing so I removed it.
    Code:
    <script type="text/javascript">
    $(document).ready(function(){
    pic_count = 4; 
    	$('.leftarrow').click(function() {
    		if(pic_count != 0){
    		   $('#folioinner').animate({left: '-=805'}, 800,'swing');
    		pic_count--;
    		}
    	});
    
    	$('.rightarrow').click(function() {
    		if(pic_count != 4){
    		   $('#folioinner').animate({left: '+=805'}, 800,'swing');
    		pic_count++;
    		}
    	});
    });
    </script>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    Common (07-26-2013)

  • #3
    Regular Coder Common's Avatar
    Join Date
    Jan 2009
    Location
    Glasgow, UK
    Posts
    133
    Thanks
    16
    Thanked 13 Times in 13 Posts
    Thanks!! Works a charm.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    You can avoid having to hardcode the pic_count by doing this:

    Code:
    var pic_count = $('image selector here').length;


  •  

    Posting Permissions

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