问题
I have designed a simple jquery banner for one website. The banner is working fine but how to refresh the banner with set interval. I have gone through various codes but it is not working. Please do help me by correcting the code repetition of the banner for a specific intervals.
THE CSS CODE:
.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; }
THE SCRIPT CODE with 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);
});
});
});
})();
});
THE HTML CODE:
<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:
Use setInterval
like this: http://jsfiddle.net/f2JCV/1/
setInterval
will not execute immediately. It waits for a timeout before it begins, so I wrote a helper function to call it immediately.
$(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
});
来源:https://stackoverflow.com/questions/18945569/banner-set-interval