JQuery specialists, I need your help!

Discussion in 'jQuery' started by DidierE, Mar 23, 2010.

  1. #1
    Hi,
    check the JQuery slideshow at the bottom of this page: http://tinyurl.com/ya6emkz

    Here is the code i currently have:

    <div id="outerContainer">
    			<div id="imageScroller">
    				<div id="viewer" class="js-disabled">
    					<a class="wrapper" href="#slide" onclick="javascript:expand()"><img id="myPicture1" src="http://www.mysite.be/slideshow-images/1.jpg"></a>
    					<a class="wrapper" href="#slide" onclick="javascript:expand1()"><img class="logo" id="apple" src="http://www.mysite.be/slideshow-images/2.jpg"></a>
    					<a class="wrapper" href="#slide" onclick="javascript:expand2()"><img class="logo" id="apple" src="http://www.mysite.be/slideshow-images/3.jpg"></a>
    					<a class="wrapper" href="#slide" onclick="javascript:expand3()"><img class="logo" id="apple" src="http://www.mysite.be/slideshow-images/4.jpg"></a>
    					<a class="wrapper" href="#slide" onclick="javascript:expand4()"><img class="logo" id="apple" src="http://www.mysite.be/slideshow-images/5.jpg"></a>
    					<a class="wrapper" href="#slide" onclick="javascript:expand5()"><img class="logo" id="apple" src="http://www.mysite.be/slideshow-images/6.jpg"></a>
    					<a class="wrapper" href="#slide" onclick="javascript:expand6()"><img class="logo" id="apple" src="http://www.mysite.be/slideshow-images/7.jpg"></a>
    					<a class="wrapper" href="#slide" onclick="javascript:expand7()"><img class="logo" id="apple" src="http://www.mysite.be/slideshow-images/8.jpg"></a>
    					<a class="wrapper" href="#slide" onclick="javascript:expand8()"><img class="logo" id="apple" src="http://www.mysite.be/slideshow-images/9.jpg"></a>
    					<a class="wrapper" href="#slide" onclick="javascript:expand9()"><img class="logo" id="apple" src="http://www.mysite.be/slideshow-images/10.jpg"></a>
    				</div>
    			</div>
    		</div>
    		<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
    		<script type="text/javascript">
    			$(function() {
    			
    			  //remove js-disabled class
    				
    			
    			  //create new container2 for images
    				$("<div>").attr("id", "container2").css({ position:"absolute"}).width($(".wrapper").length * 170).height(170).appendTo("div#viewer");
    				($("div#controls").length == 0) ? $("<div>").attr("id", "controls").appendTo("div#outerContainer").slideDown("slow") : null ;
    					($("a#rtl").length == 0) ? $("<a>").attr({ id:"rtl", href:"#slide", title:"" }).appendTo("#controls") : null ;
    					($("a#ltr").length == 0) ? $("<a>").attr({ id:"ltr", href:"#slide", title:"" }).appendTo("#controls") : null ;
    			    
    			  	
    				//add images to container2
    				$(".wrapper").each(function() {
    					$(this).appendTo("div#container2");
    				});
    				
    				//work out duration of anim based on number of images (1 second for each image)
    				var duration = $(".wrapper").length * 1000;
    				
    				//store speed for later (distance / time)
    				var speed = (parseInt($("div#container2").width()) + parseInt($("div#viewer").width())) / duration;
    								
    				//set direction
    				var direction = "rtl";
    				
    				//set initial position and class based on direction
    				(direction == "rtl") ? $("div#container2").css("left", $("div#viewer").width()).addClass("rtl") : $("div#container2").css("left", 0 - $("div#container2").width()).addClass("ltr") ;
    				
    				//animator function
    				var animator = function(el, time, dir) {
    				 
    					//which direction to scroll
    					if(dir == "rtl") {
    					  
    					  //add direction class
    						el.removeClass("ltr").addClass("rtl");
    					 		
    						//animate the el
    						el.animate({ left:"-" + el.width() + "px" }, time, "linear", function() {
    												
    							//reset container2 position
    							$(this).css({ left:$("div#imageScroller").width(), right:"" });
    							
    							//restart animation
    							animator($(this), duration, "rtl");
    							
    										
    											
    						});
    					} else {
    					
    					  //add direction class
    						el.removeClass("rtl").addClass("ltr");
    					
    						//animate the el
    						el.animate({ left:$("div#viewer").width() + "px" }, time, "linear", function() {
    												
    							//reset container2 position
    							$(this).css({ left:0 - $("div#container2").width() });
    							
    							//restart animation
    							animator($(this), duration, "ltr");
    							
    										
    						});
    					}
    				}
    				
    				//start anim
    				animator($("div#container2"), duration, direction);
    				
    				//pause on mouseover
    				$("a.wrapper").live("mouseover", function() {
    				  
    					//stop anim
    					$("div#container2").stop(true);
    					
    					//show controls
    					($("div#controls").length == 0) ? $("<div>").attr("id", "controls").appendTo("div#outerContainer").slideDown("slow") : null ;
    					($("a#rtl").length == 0) ? $("<a>").attr({ id:"rtl", href:"#", title:"rtl" }).appendTo("#controls") : null ;
    					($("a#ltr").length == 0) ? $("<a>").attr({ id:"ltr", href:"#", title:"ltr" }).appendTo("#controls") : null ;
    					
    					
    					//variable to hold trigger element
    					var title = $(this).attr("title");
    					
    					//add p if doesn't exist, update it if it does
    					
    				});
    				
    				//restart on mouseout
    				$("a.wrapper").live("mouseout", function(e) {
    				  
    					
    					
    					
    					//work out total travel distance
    					var totalDistance = parseInt($("div#container2").width()) + parseInt($("div#viewer").width());
    														
    					//work out distance left to travel
    					var distanceLeft = ($("div#container2").hasClass("ltr")) ? totalDistance - (parseInt($("div#container2").css("left")) + parseInt($("div#container2").width())) : totalDistance - (parseInt($("div#viewer").width()) - (parseInt($("div#container2").css("left")))) ;
    					
    					//new duration is distance left / speed)
    					var newDuration = distanceLeft / speed;
    				
    					//restart anim
    					animator($("div#container2"), newDuration, $("div#container2").attr("class"));
    
    				});
    												
    				//handler for ltr button
    				$("#ltr").live("click", function() {
    				 					
    					//stop anim
    					$("div#container2").stop(true);
    					
    					//swap class names
    					$("div#container2").removeClass("rtl").addClass("ltr");
    										
    					//work out total travel distance
    					var totalDistance = parseInt($("div#container2").width()) + parseInt($("div#viewer").width());
    					
    					//work out remaining distance
    					var distanceLeft = totalDistance - (parseInt($("div#container2").css("left")) + parseInt($("div#container2").width()));
    					
    					//new duration is distance left / speed)
    					var newDuration = distanceLeft / speed;
    					
    					//restart anim
    					animator($("div#container2"), newDuration, "ltr");
    				});
    				
    				//handler for rtl button
    				$("#rtl").live("click", function() {
    										
    					//stop anim
    					$("div#container2").stop(true);
    					
    					//swap class names
    					$("div#container2").removeClass("ltr").addClass("rtl");
    					
    					//work out total travel distance
    					var totalDistance = parseInt($("div#container2").width()) + parseInt($("div#viewer").width());
    
    					//work out remaining distance
    					var distanceLeft = totalDistance - (parseInt($("div#viewer").width()) - (parseInt($("div#container2").css("left"))));
    					
    					//new duration is distance left / speed)
    					var newDuration = distanceLeft / speed;
    				
    					//restart anim
    					animator($("div#container2"), newDuration, "rtl");
    				});
    			});
    		</script>
    PHP:
    Now my question for you specialists: how can i change my code above so the slideshow doesn't contain that ugly "white space" after the final image. In other words, how can i change the code so the script instantly loads image 1 again once image 10 is displayed (there can't be any long white space like it is now).

    Any idea?

    PS: i know the slideshow isn't displayed correctly in Firefox but i'll fix that later...

    Thanks in advance!
     
    DidierE, Mar 23, 2010 IP