SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jun 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript sideshow

    This slideshow works, but I had to put a conditional statement in where it shouldn't be needed. I did this because the variable imgSize returns a zero the first time the page loads. Does anyone know the reason for this? thanks.

    if(!imgindex==0)
    {
    centerAd(imgSize)
    }else{
    centerAd(firstimage)
    }

    Code:
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
    <script language="JavaScript1.1">
    <!--
    
    //resize event
    window.onresize = function(){centerAd();}
    
    //sideshow function
    var slideimages=new Array()
    
    function slideshowimages()
    {
    	for (i=0;i<slideshowimages.arguments.length;i++)
    	{
    	slideimages[i]=new Image()
    	slideimages[i].src=slideshowimages.arguments[i]
    	}
    }
    
    
    //center function.
    function centerAd(imgSize)
    {
     var sw;
     if(parseInt(navigator.appVersion)>3)
     {
      if(navigator.appName=="Netscape"){sw = window.innerWidth;}
      if(navigator.appName.indexOf("Microsoft")!=-1){sw = document.body.offsetWidth;}
      }
     var w = (sw-imgSize)/2;
     document.getElementById("slideshowContainer").style.width = sw+"px";
     document.getElementById("slideshow").style.marginLeft = w+"px";
     }
    
    //-->
    </script>
    <style type="text/css">
    body { margin: 0; padding: 0; }
    
    #slideshowContainer
    {
    /*width set by javascript above */
    /*screen width is detected on the fly */
    position:relative;
    overflow:hidden;
    background-color:#181818;
    }
    #slideshow
    {
    /*margin-left set by javascript above*/
    /*nessesary margin is calculated on the fly*/
    width:2680px;
    }
    
    
    </style>
    </head>
    <body > 
    
    <!-- first image to be displayed, image is modified by the below code to display a new image every X number of milliseconds -->
    <div id="slideshowContainer">
    	<div id="slideshow">
    	<!--src, because sideit() runs through once and imgindex=0, so first image will be displayed but left in becuse Active-X message-->
    		<img src="Adone.jpg" border="0" name="slide" height=481 >
    	</div>
    </div>
    
    
    
    <script>
    <!--
    //(1)Call sideshowimages function, preload images.
    slideshowimages("adone.jpg","adtwo.jpg","adthree.jpg","adfour.jpg")
    
    
    //configure the speed of the slideshow, in miliseconds
    var slideshowspeed=2000
    var imgindex=0
    var firstimage=2680
    function slideit()
    {
    //if browser does not support the image object, exit.
    	if (!document.images){
    		return
    	}
    
    		//core of code, changes the image.
    		document.images.slide.src=slideimages[imgindex].src
    		
    		//crop,center image
    		//this calls the centerAd function, passing it argument imgSize.
    		var imgSize=slideimages[imgindex].width
    		if(!imgindex==0)
    		{
    		centerAd(imgSize)
    		}else{
    		centerAd(firstimage)
    		}
    		
    	//if imgindex is not at the last image increment, else reset imgindex to zero.
    	if (imgindex<slideimages.length-1){
    		imgindex++
    	}
    	else{
    		imgindex=0
    		}
    		//call slideit function agian after some time.
    	setTimeout("slideit()",slideshowspeed)
    	
    }
    
    //(2)
    slideit()
    
    //-->
    </script>
    
    </body>
    </html>

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,682
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by webgeek1985 View Post
    This slideshow works, but I had to put a conditional statement in where it shouldn't be needed. I did this because the variable imgSize returns a zero the first time the page loads. Does anyone know the reason for this?
    That's quite likely due to the fact that the image is still loading, so the page has no idea of how big the image will be until it's finished loading.

    Do you want to wait until the image has finished loading?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Jun 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    humm, I guess that could be. But I thought since the images were preloaded I could get size right away. But I'm not against adding a wait.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,682
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    You could always check the complete property of the image, which in your case would be:
    slideimages[imgindex].complete

    If it's not set, you know that the image hasn't completed loading, at which point you can setup a setTimeout event to run the function again.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Member
    Join Date
    Jun 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well that is an excellent suggestion! Thank you very much for your comments, if I have any luck getting to the bottom of this I will post back.


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
  •