需要在页面中一个小的区域循环滚动展示新闻(公告、活动、图片等等),并且,鼠标悬停时停止滚动并提示,离开后,继续滚动。45GHTML5中文学习网 - HTML5先行者学习网
效果图:45GHTML5中文学习网 - HTML5先行者学习网

45GHTML5中文学习网 - HTML5先行者学习网
上干货45GHTML5中文学习网 - HTML5先行者学习网
html:45GHTML5中文学习网 - HTML5先行者学习网
45GHTML5中文学习网 - HTML5先行者学习网
<div id="news">45GHTML5中文学习网 - HTML5先行者学习网
<ul>45GHTML5中文学习网 - HTML5先行者学习网
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="aaaaaaaaaaaaaaa">aaaaaaaaaaaaaaa</a></li>45GHTML5中文学习网 - HTML5先行者学习网
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="bbbbbbbbbbbbbbb">bbbbbbbbbbbbbbb</a></li>45GHTML5中文学习网 - HTML5先行者学习网
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="ccccccccccccccc">ccccccccccccccc</a></li>45GHTML5中文学习网 - HTML5先行者学习网
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="ddddddddddddddd">ddddddddddddddd</a></li>45GHTML5中文学习网 - HTML5先行者学习网
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="eeeeeeeeeeeeeee">eeeeeeeeeeeeeee</a></li>45GHTML5中文学习网 - HTML5先行者学习网
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="fffffffffffffff">fffffffffffffff</a></li>45GHTML5中文学习网 - HTML5先行者学习网
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="ggggggggggggggg">ggggggggggggggg</a></li>45GHTML5中文学习网 - HTML5先行者学习网
</ul>45GHTML5中文学习网 - HTML5先行者学习网
</div>45GHTML5中文学习网 - HTML5先行者学习网
45GHTML5中文学习网 - HTML5先行者学习网css:
45GHTML5中文学习网 - HTML5先行者学习网45GHTML5中文学习网 - HTML5先行者学习网
ui,li {45GHTML5中文学习网 - HTML5先行者学习网
list-style: none;45GHTML5中文学习网 - HTML5先行者学习网
}45GHTML5中文学习网 - HTML5先行者学习网
#news{45GHTML5中文学习网 - HTML5先行者学习网
height: 75px;45GHTML5中文学习网 - HTML5先行者学习网
overflow: hidden;45GHTML5中文学习网 - HTML5先行者学习网
}45GHTML5中文学习网 - HTML5先行者学习网
45GHTML5中文学习网 - HTML5先行者学习网关键是js文件:
45GHTML5中文学习网 - HTML5先行者学习网45GHTML5中文学习网 - HTML5先行者学习网$(function() {
45GHTML5中文学习网 - HTML5先行者学习网var $this = $("#news");
45GHTML5中文学习网 - HTML5先行者学习网var scrollTimer;
45GHTML5中文学习网 - HTML5先行者学习网$this.hover(function() {
45GHTML5中文学习网 - HTML5先行者学习网clearInterval(scrollTimer);
45GHTML5中文学习网 - HTML5先行者学习网}, function() {
45GHTML5中文学习网 - HTML5先行者学习网scrollTimer = setInterval(function() {
45GHTML5中文学习网 - HTML5先行者学习网scrollNews($this);
45GHTML5中文学习网 - HTML5先行者学习网}, 2000);
45GHTML5中文学习网 - HTML5先行者学习网}).trigger("mouseleave");
45GHTML5中文学习网 - HTML5先行者学习网function scrollNews(obj) {45GHTML5中文学习网 - HTML5先行者学习网
var $self = obj.find("ul");45GHTML5中文学习网 - HTML5先行者学习网
var lineHeight = $self.find("li:first").height(); 45GHTML5中文学习网 - HTML5先行者学习网
$self.animate({45GHTML5中文学习网 - HTML5先行者学习网
"marginTop": -lineHeight + "px"45GHTML5中文学习网 - HTML5先行者学习网
}, 600, function() {45GHTML5中文学习网 - HTML5先行者学习网
$self.css({45GHTML5中文学习网 - HTML5先行者学习网
marginTop: 045GHTML5中文学习网 - HTML5先行者学习网
}).find("li:first").appendTo($self);45GHTML5中文学习网 - HTML5先行者学习网
})45GHTML5中文学习网 - HTML5先行者学习网
}45GHTML5中文学习网 - HTML5先行者学习网
})45GHTML5中文学习网 - HTML5先行者学习网
45GHTML5中文学习网 - HTML5先行者学习网主要就是对hover、setInterval、clearInterval、animate这些方法以及marginTop属性(marginLeft、top、left等等)的理解和运用,需要注意的是,如果不加.trigger("mouseleave"),在网页初始化的时候列表不会滚动,还有appendTo能直接移动元素,就这些了。
45GHTML5中文学习网 - HTML5先行者学习网