Jquery 无限循环滚动公告的实现代码分享

发布时间:2019-10-26编辑:脚本学堂
本文介绍下,用jquery实现的无限循环滚动公告信息的一段代码,供初学jquery的朋友作个参考。

1,jquery代码部分

<script>
/**
* 无限循环滚动
* edit by www.jb200.com
*/
/第二版:Newton改造
(function (win){
var callboarTimer;
var callboard = $('#callboard');
var callboardUl = callboard.find('ul');
var callboardLi = callboard.find('li');
var liLen = callboard.find('li').length;
var initHeight = callboardLi.first().outerHeight(true);
win.autoAnimation = function (){
if (liLen <= 1) return;
var self = arguments.callee;
var callboardLiFirst = callboard.find('li').first();
callboardLiFirst.animate({
marginTop:-initHeight
}, 500, function (){
clearTimeout(callboarTimer);
callboardLiFirst.appendTo(callboardUl).css({marginTop:0});
callboarTimer = setTimeout(self, 5000);
});
}

callboard.mouseenter(
function (){
clearTimeout(callboarTimer);
}).mouseleave(function (){
callboarTimer = setTimeout(win.autoAnimation, 5000);
});
}(window));
setTimeout(window.autoAnimation, 5000);
</script>

2,html页面内容

<div id="callboard">
 <ul>
 <li>
<a href="http://www.jb200.com/9652.html">公告:脚本学堂,欢迎大家的光临</a>
</li>
<li>
 <span style="color:red;">公告:流量持续增长中,敬请期待!</span>
</li>
 </ul>
</div>

3,CSS代码

#callboard { height:24px; line-height:24px; overflow:hidden;}
#callboard ul { padding:0;}
#callboard li { padding:0;}