2013-09-22 65 views
1

我为一个网站设计了一个简单的jquery横幅。横幅工作正常,但如何刷新横幅设置的时间间隔。我已经通过各种代码,但它不工作。请通过纠正特定时间间隔内的横幅代码重复来帮助我。横幅设置间隔

CSS代码:

.banner {-webkit-border-radius:6px; -moz-border-radius:8px; border-radius:8px; 
      -khtml-border-radius: 8px; border:#bbd9ef solid 1px; background:#f5fffa; 
      padding: 5px 0 0 20px; width: 200px; height: 110px; 
     } 
    .k, .l, .m, .n {position: relative; top: -200px; text-decoration: none; } 
    .n { font-weight: bold; color: red; } 

脚本代码与jquery1.9.1:

$(document).ready(function() { 
     $(".banner a").hide(); 
      (function() { 
      $(".k").show().animate({top: "0"}, 3000, function() { 
       $(".l").show().animate({top: "0"},3000, function(){ 
       $(".m").show().animate({top: "0"},3000, function(){ 
        $(".n").show().animate({top: "0"},3000); 
        }); 
       }); 
      }); 
      })(); 
     }); 

的HTML代码:

<div class="banner"> 
    <a href="#" class="k">Design banner in your ownway</a><br /> 
    <a href="#" class="l"> Get more taffic and publishers.</a><br/> 
    <a href="#" class="m">Still doubt, please do contact:</a><br/><br/> 
    <a href="#" class="n">www.freemenu.info</a> 
</div> 
+1

我让你的问题的的jsfiddle:http://jsfiddle.net/f2JCV/ –

回答

1

使用setInterval这样的:http://jsfiddle.net/f2JCV/1/

setInterval将不会立即执行。它在开始之前等待超时,所以我写了一个帮助函数来立即调用它。

$(document).ready(function() { 

    // Call the fn immediately, then every interval milliseconds 
    function setIntervalNow(fn,interval) { 
     fn(); 
     return setInterval(fn, interval); 
    } 

    setIntervalNow(function() { 
     $(".banner a").hide().css('top',-200); // reset the items to the top 
      (function() { 
      $(".k").show().animate({top: "0"}, 3000, function() { 
       $(".l").show().animate({top: "0"},3000, function(){ 
       $(".m").show().animate({top: "0"},3000, function(){ 
        $(".n").show().animate({top: "0"},3000); 
        }); 
       }); 
      }); 
      })(); 
    }, 15000); // repeat every 15 seconds 
}); 
+0

提问者没有说他需要它立即执行,是不是?为什么要在这种情况下经历麻烦?无论如何,我真的很喜欢setIntervalNow函数。它提醒我一个do/while循环,除了内置的延迟。扩展JavaScript的很酷的例子! +1 – jmort253

+0

其实,是的,他做到了。他给出的例子立即执行。 –