Solved

Progressive Image Load and Switch

Posted on 2004-09-30
7
170 Views
Last Modified: 2010-05-18
js gurus

I am trying to design some code to accomplish the following
* I have a list of images to be loaded, some of which are large
* assume a slow connection
* as each image is loaded I want it placed in another array which is cycled through on a timer and displayed in another window (the same window for all images). So, this array gets bigger as each of the images arrive.  So, when we have only 1 image, this just stays there unchanging. If we have 2 images (A and B) and the timer interval is 20 seconds, we would see A B A in the first minute, assuming C has not arrived. If C arrives any time during the first display of A or B then we would see A B C in the first minute.

I imagine this can be accomplished using the onload method of the Image object and the settimeout (or is it SetTimeout?) method of window (or Window? .. this case sensitivity is driving me nuts) .. but I am a js novice, so code that puts it all together would be nice

thanks
0
Comment
Question by:Mutley2003
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 6

Expert Comment

by:viola123
ID: 12196107
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
     <head>
         <title>Image Scrolling</title>
          <style type="text/css">
               ul     {
                    width:480px;
                    height:80px;
                    padding:0;
                    list-style:none;
                    position:relative;
                    overflow:hidden;
               }
               ul li     {
                    position:absolute;
                    -moz-opacity:0.5;
               }
               ul li:hover     {
                    -moz-opacity:0.99;
               }
          </style>
          <script type="text/javascript">
               var width = 0;
               var slideshow;
               var list;
               var img;

               var DELAY = 100;
               var STEP = 4;

               function init()
               {
                    slideshow = document.getElementById("slideshow");
                    list = slideshow.getElementsByTagName("li");
                    img = slideshow.getElementsByTagName("img");

                    var offset = 0;
                    for(var i = 0; i < list.length; i++)
                    {
                         offset += (i > 0) ? img[i - 1].width : 0;
                         list[i].style.left = offset + "px";
                         width += img[i].width;
                    }
                    move();
               }
               function move()
               {
                    for(var i = 0; i < list.length; i++)
                    {
                         var left = list[i].style.left.slice(0,-2) - STEP;
                         list[i].style.left = left + "px";

                         if(left <= -(img[i].width))
                         {
                              list[i].style.left = width - img[i].width + "px";
                         }
                    }
                    setTimeout("move()",DELAY);
               }
               window.onload = init;
          </script>
     </head>
     <body>
          <ul id="slideshow">
               <li><img src="1.jpg"></li>
               <li><img src="2.jpg"></li>
               <li><img src="3.jpg"></li>
               <li><img src="4.jpg"></li>
               <li><img src="5.jpg"></li>
          </ul>
     </body>
</html>
0
 

Author Comment

by:Mutley2003
ID: 12196136
That was quick! Trying it now, thanks

0
 

Author Comment

by:Mutley2003
ID: 12196197
OK, I tried it (cute) and studied the code, but it does not seem to address my concern about slow loading images. I don't want to preload them all, just work with them in the sequence they arrive over a slow connection.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 3

Accepted Solution

by:
hemebond earned 500 total points
ID: 12197823
I'd like some credit if you're going to post my code Viola.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
      <head>
            <title>Q_21151825</title>
            <style type="text/css">
                  ul      {
                        position:relative;
                  }
                  li      {
                        position:absolute;
                        z-index:0;
                  }
            </style>
      </head>
      <body>
            <ul>
                  <li><img src="/images/1.jpeg"></li>
                  <li><img src="/images/2.jpeg"></li>
                  <li><img src="/images/3.jpeg"></li>
                  <li><img src="/images/4.jpeg"></li>
                  <li><img src="/images/5.jpeg"></li>
            </ul>
      </body>
      <script type="text/javascript">
            var current = 0;
            var next = 1;
            var list = document.getElementsByTagName("li");

            for(var i = 0; i < list.length; i++)
            {
                  list[i].style.zIndex = 0;
            }
            list[current].style.zIndex = 1;

            setInterval("change()",1000);

            function change()
            {
                  list[current].style.zIndex = 0;
                  list[next].style.zIndex = 2;
                  current = next;

                  for(var i = 0; i < list.length; i++)
                  {
                        next++;
                        if(next == list.length)
                        {
                              next = 0;
                        }
                        if(list[next].firstChild.complete == true && list[next].firstChild.width > 0)
                        {
                              break;
                        }
                  }
            }
      </script>
</html>
0
 

Author Comment

by:Mutley2003
ID: 12206280
hemebond

that works, thanks. It seems to rely on the script being outside the <body> </body> tags .. I have never seen that before. I guess that makes this approach difficult to put in external js?

0
 
LVL 3

Expert Comment

by:hemebond
ID: 12221588
Not at all. By moving the script to the bottom of the page, outside the body, the page is able to load before the script attempts to modify it. It also means the visitor will see the page sooner, because they're not waiting for the script to download. You can move it to an external file, and you'll get all the same benefits (and more).
0
 

Author Comment

by:Mutley2003
ID: 12257038
ok, that's cool. It has started me thinking of ways of using an external script outside the body. thanks

0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question