2009-05-27 50 views
20

我想以特定间隔(10秒)显示div并显示下一个div,并继续并重复相同的操作。
使用jQuery以特定的时间间隔显示和隐藏div

**

顺序:

** 月10第二 显示DIV1,隐藏其他的div,
后5秒间隔 显示DIV 2和隐藏其他divs,
5秒后间隔 显示div 3并隐藏其他div,
并每10秒重复相同操作。


代码如下:


<div id='div1' style="display:none;"> 
    <!-- content --> 
</div> 

<div id='div2' style="display:none;"> 
    <!-- content --> 
</div> 

<div id='div3' style="display:none;"> 
    <!-- content --> 
</div> 

回答

31

Working Example在这里 - 添加/编辑到URL的代码

你只需要使用JavaScript setInterval功能

$('html').addClass('js'); 

$(function() { 

    var timer = setInterval(showDiv, 5000); 

    var counter = 0; 

    function showDiv() { 
    if (counter ==0) { counter++; return; } 

    $('div','#container') 
     .stop() 
     .hide() 
     .filter(function() { return this.id.match('div' + counter); }) 
     .show('fast'); 
    counter == 3? counter = 0 : counter++; 

    } 

}); 

和HTML玩

<!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> 
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
     <title>Sandbox</title> 
     <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
     <style type="text/css" media="screen"> 
     body { background-color: #fff; font: 16px Helvetica, Arial; color: #000; } 
     .display { width:300px; height:200px; border: 2px solid #000; } 
     .js .display { display:none; } 
     </style> 
    </head> 

    <body> 
     <h2>Example of using setInterval to trigger display of Div</h2> 
     <p>The first div will display after 10 seconds...</p> 
     <div id='container'> 
     <div id='div1' class='display' style="background-color: red;"> 
     div1 
     </div> 
     <div id='div2' class='display' style="background-color: green;"> 
     div2 
     </div> 
     <div id='div3' class='display' style="background-color: blue;"> 
     div3 
     </div> 
     <div> 
    </body> 

</html> 

编辑:

在回答你关于容器DIV评论,只是修改此

$('div','#container') 

这个

$('#div1, #div2, #div3') 
+0

Russ感谢您对帖子的快速回复。在你的例子中,它显示我们需要有一个div容器,并且我们需要有其他div。但是我的div并不在其容器内部。如何相应地修改代码? – Webrsk 2009-05-27 10:40:27

+0

你有权限修改HTML吗?如果是这样,我会建议将div包装在容器div中以便于选择。如果没有容器div,代码将需要修改,以便只选择那些有问题的div,即带有ID 1,2和3的div。我现在更新我的答案 – 2009-05-27 10:44:50

5

InnerFade

<script type="text/javascript"> 
    $(document).ready(

    function() { 
     $('#portfolio').innerfade({ 
      speed: 'slow', 
      timeout: 10000, 
      type: 'sequence', 
      containerheight: '220px' 
     }); 
    }); 
</script> 
<ul id="portfolio"> 
    <li> 
     <a href="http://medienfreunde.com/deutsch/referenzen/kreation/good_guy__bad_guy.html"> 
     <img src="images/ggbg.gif" alt="Good Guy bad Guy" /> 
     </a> 
    </li> 
    <li> 
     <a href="http://medienfreunde.com/deutsch/referenzen/kreation/whizzkids.html"> 
     <img src="images/whizzkids.gif" alt="Whizzkids" /> 
     </a> 
    </li> 
    <li> 
     <a href="http://medienfreunde.com/deutsch/referenzen/printdesign/koenigin_mutter.html"> 
     <img src="images/km.jpg" alt="Königin Mutter" /> 
     </a> 
    </li> 
    <li> 
     <a href="http://medienfreunde.com/deutsch/referenzen/webdesign/rt_reprotechnik_-_hybride_archivierung.html"> 
     <img src="images/rt_arch.jpg" alt="RT Hybride Archivierung" /> 
     </a> 
    </li> 
    <li> 
     <a href="http://medienfreunde.com/deutsch/referenzen/kommunikation/tuev_sued_gruppe.html"> 
     <img src="images/tuev.jpg" alt="TÜV SÜD Gruppe" /> 
     </a> 
    </li> 
</ul> 
+0

好又简单的插件! – TruMan1 2012-12-27 15:22:37

17

遍历每10秒的div。

$(function() { 

    var counter = 0, 
     divs = $('#div1, #div2, #div3'); 

    function showDiv() { 
     divs.hide() // hide all divs 
      .filter(function (index) { return index == counter % 3; }) // figure out correct div to show 
      .show('fast'); // and show it 

     counter++; 
    }; // function to loop through divs and show correct div 

    showDiv(); // show first div  

    setInterval(function() { 
     showDiv(); // show next div 
    }, 10 * 1000); // do this every 10 seconds  

}); 
7

这里是一个jQuery插件,我想出了:

$.fn.cycle = function(timeout){ 
    var $all_elem = $(this) 

    show_cycle_elem = function(index){ 
     if(index == $all_elem.length) return; //you can make it start-over, if you want 
     $all_elem.hide().eq(index).fadeIn() 
     setTimeout(function(){show_cycle_elem(++index)}, timeout); 
    } 
    show_cycle_elem(0); 
} 

你需要为你婉周期的div一个共同的类名,像这样使用:

$("div.cycleme").cycle(5000) 
0

试试这个

 $('document').ready(function(){ 
     window.setTimeout('test()',time in milliseconds); 
     }); 

     function test(){ 

     $('#divid').hide(); 

     } 
0

他使用递归并且不使用可变变量来解决另一个问题。此外,即时通讯不使用setInterval所以没有清理,必须完成。

有了这个HTML

<section id="testimonials"> 
    <h2>My testimonial spinner</h2> 
    <div class="testimonial"> 
     <p>First content</p> 
    </div> 
    <div class="testimonial"> 
     <p>Second content</p> 
    </div> 
    <div class="testimonial"> 
     <p>Third content</p> 
    </div> 
</section> 

使用ES2016

在这里,您递归调用函数和更新的参数。

const testimonials = $('#testimonials') 
     .children() 
     .filter('div.testimonial'); 

    const showTestimonial = index => { 

    testimonials.hide(); 
    $(testimonials[index]).fadeIn(); 

    return index === testimonials.length 
     ? showTestimonial(0) 
     : setTimeout(() => { showTestimonial(index + 1); }, 10000); 
    } 

showTestimonial(0); // id of the first element you want to show.