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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Infinite Loop of .next .last HELP!

    I am trying to create a plugin for a client I am working for.

    The desire something that seemed to start easy enough but now I am running into issues.

    They want a static front page with a div containing images of products they have.

    there will be 5 divs with 2 images per div.

    The want a next and previous button image which will show the next or last products where if it reaches whichever end it loops over to begin from the last or the first in whichever direction they are clicking.

    Once each button is pressed it will also change the background of the parent DIV it is nested in.

    Here is the Jquery code I wrote:

    Code:
     
    var backgrounds = new Array ();
    backgrounds[0] = "http://localhost/elginsite/wp-content/themes/elgintheme/images/elgin_2013_life03.jpg";
    backgrounds[1] = "http://localhost/elginsite/wp-content/themes/elgintheme/images/elgin_2013_life03.jpg";
    backgrounds[2] = "http://localhost/elginsite/wp-content/themes/elgintheme/images/elgin_2013_life02.jpg";
    backgrounds[3] = "http://localhost/elginsite/wp-content/themes/elgintheme/images/elgin_2013_life01.jpg";
    backgrounds[4] = "http://localhost/elginsite/wp-content/themes/elgintheme/images/elgin_2013_life03.jpg";
    backgrounds[5] = "http://localhost/elginsite/wp-content/themes/elgintheme/images/1307031_08_116.jpg";
    
    var divs = new Array ();
    divs[0] = "#item1";
    divs[1] = "#item1";
    divs[2] = "#item2";
    divs[3] = "#item3";
    divs[4] = "#item4";
    divs[5] = "#last-item";
    
    <!-- Script written by G.B. -->
    
    <!-- set max button clicks til reloop -->
    var $clicker = 1;
    <!-- Math Vault -->
    var $divTotal = 1;
    
    $(function(){
    
      function prev() {
        var $item = $($(this).data('carousel')).find('.item.active');
    	var $bg = document.getElementById("z2");
    
        if (!$item.is(':first-child') && $divTotal >= 1) {
          $item.removeClass('active').hide().prev('.item').addClass('active').fadeTo(1000, 1);
    	  $divTotal = ($divTotal - $clicker);
    	  $stuffingTotal = ($divTotal + 1);
    	  	  	$("#z2").fadeTo(1000, 1).css({'background-image' : "url('" + backgrounds[$divTotal] + "')"});
    			$(this).stop();
    	}
    }
      // get the active item, if it is not the last, set next as active
      function next() 
      {
        var $item = $($(this).data('carousel')).find('.item.active');
    	var $bg = document.getElementById("z2");
        if (!$item.is(':last-child') && $divTotal < 5) {
         $item.removeClass('active').fadeOut(10).next().fadeTo(1000, 1).addClass('active');
    
    	  $divTotal = ($divTotal + $clicker);
    	  	  $stuffingTotal = ($divTotal - 1);
    	  	  	$("#z2").hide().fadeTo(1000, 1).css({'background-image' : "url('" + backgrounds[$divTotal] + "')"});
    			$(this).stop();
    			
    	} 	else
    	var $bg = document.getElementById("z2");
    	if ($item.is("last-item") && $divTotal === 5) {
          $item.removeClass('active').fadeOut(10).next().fadeTo(1000, 1).addClass('active');
    	  $stuffingTotal = ($divTotal - 1);
    	  	  	$("#z2").hide().css({'background-image' : "url('" + backgrounds[$divTotal] + "')"});
    			$(this).stop();
    	}
    }
      // bind the controls to the actions
      $('a.prev').on('click', prev);
      $('a.next').on('click', next);
    
    });
    Here is the Html:

    Code:
     
    <div id="z2">
    &nbsp;
    </div>
    
    <div id="z3">
    
    <div id="watchswap" style="height:250px; width:1024px; margin-top:0px; background-image:url('http://localhost/elginsite/wp-content/themes/elgintheme/images/watchswap-bg.png'); margin-bottom:20px;">
    	<div class="buttons1" style="margin-top:0px;">
    		<a class="next" href="#" data-carousel="#my-carousel"><img class="next" src="http://localhost/elginsite/wp-content/themes/elgintheme/images/arrow-right.png" border="0"></a>
    	</div>
    			<div id="my-carousel" class="carousel">
    
        		
                
    				<div class="items" id="my-carousel">
    
    <div class="item active" id="first-item">
    
    			<div class="watch" style="width:140px; float:right; margin-top:0px; text-align:center; padding:0px;"> 
    				<a href="./"><img src="http://localhost/elginsite/wp-content/themes/elgintheme/images/watches/elgin_watch_FG9051.png" height="250" border="0"></a>
    			</div>
                <div class="watch" style="width:200px; float:right; margin-top:0px; text-align:center; padding:0px;"> 
    				<a href="./"><img src="http://localhost/elginsite/wp-content/themes/elgintheme/images/watches/elgin_watch_EG9020ST.png" height="250" border="0"></a>
               </div>
    </div>
    
    
    <div class="item" id="item2">
    
    
    			<div class="watch" style="width:140px; float:right; margin-top:0px; text-align:center; padding:0px;"> 
    				<a href="./"><img src="http://localhost/elginsite/wp-content/themes/elgintheme/images/watches/elgin_watch_FG8030.png" height="250" border="0"></a>
    			</div>
    
    </div>
    
    <div class="item" id="item3">
    
    			<div class="watch" style="width:140px; float:right; margin-top:0px; text-align:center; padding:0px;"> 
    				<a href="./"><img src="http://localhost/elginsite/wp-content/themes/elgintheme/images/watches/elgin_watch_FG9116.png" height="250" border="0"></a>
    			</div>
    
    </div>
    
    <div class="item" id="item4">
    
    			<div class="watch" style="width:140px; float:right; margin-top:0px; text-align:center; padding:0px;"> 
    				<a href="./"><img src="http://localhost/elginsite/wp-content/themes/elgintheme/images/watches/elgin_watch_FG1016.png" height="250" border="0"></a>
    			</div>
                <div class="watch" style="width:200px; float:right; margin-top:0px; text-align:center; padding:0px;"> 
    				<a href="./"><img src="http://localhost/elginsite/wp-content/themes/elgintheme/images/watches/elgin_watch_EG9026STS.png" height="250" border="0"></a>
               </div>
    </div>
    
    <div class="item" id="last-item">
    
    
    			<div class="watch" style="width:140px; float:right; margin-top:0px; text-align:center; padding:0px;"> 
    				<a href="./"><img src="http://localhost/elginsite/wp-content/themes/elgintheme/images/watches/elgin_watch_FG9039.png" height="250" border="0"></a>
    			</div>
    
    </div>
    
    
    
    				</div>
    
    			</div>
                
    	<div class="buttons2" style="float:right;">
    		<a class="prev" href="#" data-carousel="#my-carousel"><img class="prev" src="http://localhost/elginsite/wp-content/themes/elgintheme/images/arrow-left.png" border="0"></a>
    	</div>
    
    </div>
    </div>
    I utilized a variable which holds and integer and refers to a variable which holds a parameter URL to load the proper image according to the number of clicks. I have the background and products load correctly with their appropriate fadeIn's and fadeOut's. except it doesn't loop =(

    Thanks for any help i can get on this, my job depends on it!

  • #2
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Solved

    I figured it out!

    Thanks!

    PM me for solution.



  •  

    Tags for this Thread

    Posting Permissions

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