Results 1 to 3 of 3

Thread: rotating throught divs

  1. #1
    Join Date
    Nov 2006
    Posts
    236
    Thanks
    4
    Thanked 1 Time in 1 Post

    Angry rotating throught divs

    I'm trying to make something similar to a rotating banner with DIVs
    I have a code I tried made but it gives weird looping. It won't display one image at time. I cycles between 1 image 3, none 2 and 4 being displayed at once.
    I think the if and else are not in sink.

    HTML Code:
    window.onload=function() {
    
    timer = setInterval('flip()', 2500);
    
    
    }
    
    
    
    function flip(){
    var div = document.getElementById('displaybox');
    var divs = div.getElementsByTagName('div');
    
    for (var i = 0; i < divs.length; i++) {
    if(divs[i].style.display != 'block') {
    divs[i].style.display = 'block';
    }
    
    else {divs[i].style.display = 'none';i++
    
    }
    
    } 
    }
    here is the CSS and html. I know the function is setting everything to display block. I would expect the else clause to remove images 1 by 1.
    If it was doing the opposite of what I wanted I could fix it. Strangely I can't find similar scripts on the web.
    HTML Code:
    .window{
    display:none;
    
    }
    
    
    <div id="displaybox"> 
    
    <div class="window"><span class="dis"></span><img  id="image1"  src="test2.gif"/></div>
    
    <div class="window"><span class="dis"></span><img  id="image2" src="test1.gif"/></div>
    
    <div class="window"><span class="dis"></span><img  id="image3" src="test3.png"/></div>
    
    <div calss="window"><span class="dis">4</span><img  id="image4" src=""/></div>
    
    <div class="window"><span class="dis">5</span><img  id="image5" src=""/></div>
    
    
    </div>

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    incrementing i when the display is none misses out a div as i is incremented twice

    better

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script type="text/javascript">
    /*<![CDATA[*/
    window.onload=function() {
    
     flip(0);
    
    }
    
    
    
    function flip(cnt){
     var div = document.getElementById('displaybox'),divs = div.getElementsByTagName('div'),i = 0;
     for (; i < divs.length; i++) {
       divs[i].style.display = cnt!=i?'none':'block';
     }
     cnt=cnt=++cnt%divs.length; // increment the count
     timer = setTimeout(function(){ flip(cnt); },2500);
    }
    /*]]>*/
    </script></head>
    
    <body>
    <div id="displaybox">
    
    <div class="window"><span class="dis"></span><img  id="image1" width="100" height="100" src="http://www.vicsjavascripts.org.uk/StdImages/Two.gif"/></div>
    
    <div class="window"><span class="dis"></span><img  id="image2" width="100" height="100" src="http://www.vicsjavascripts.org.uk/StdImages/One.gif"/></div>
    
    <div class="window"><span class="dis"></span><img  id="image3" width="100" height="100" src="http://www.vicsjavascripts.org.uk/StdImages/Three.gif"/></div>
    
    <div calss="window"><span class="dis">4</span><img  id="image4" width="100" height="100" src=""/></div>
    
    <div class="window"><span class="dis">5</span><img  id="image5" width="100" height="100" src=""/></div>
    
    
    </div>
    </body>
    
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Nov 2006
    Posts
    236
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    If I had changed it to visibility: hidden would that have still cause problem with the increment. I knew something was wrong with the incrementation but I couldn't catch the pattern. Why does it do this?

Similar Threads

  1. Iframe-divs: a technique for letting divs overlay anything we want
    By molendijk in forum Submit a DHTML or CSS code
    Replies: 0
    Last Post: 03-22-2012, 08:30 PM
  2. Processing form throught ajax
    By remp in forum JavaScript
    Replies: 2
    Last Post: 11-23-2010, 01:22 AM
  3. Replies: 1
    Last Post: 03-10-2010, 01:10 AM
  4. Layered DIVs / Hide All Other DIVs When DIV is Selected
    By johnnyrokkit in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 08-05-2009, 06:23 AM
  5. Animated Collapsible DIVS -additional DIVS float aside 3rd DIV
    By jono in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 05-12-2009, 12:57 PM

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
  •