JS 实现消息滚动效果
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>js实现消息滚动效果</title>
<script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
<style>
* {
margin:0;
padding:0;
}
.scroll-box {
width:200px;
height:200px;
border:2px solid #000;
margin:20px auto;
overflow:hidden;
}
.scroll-box ul {
list-style:none;
width:100%;
height:100%;
}
.scroll-box ul li {
width:100%;
height:40px;
box-sizing:border-box;
line-height:40px;
text-align:left;
}
</style>
</head>
<body>
<div class="scroll-box">
<ul>
<li>1,课程html</li>
<li>2,课程css</li>
<li>3,课程js</li>
<li>4,课程jquery</li>
<li>5,课程html5</li>
<li>6,课程css3</li>
<li>7,课程php</li>
<li>8,课程bootstrap</li>
<li>9,课程apicloud</li>
</ul>
</div>
<script>
$(function() {
//获得当前<ul>
var $uList = $(".scroll-box ul");
var timer = null;
//触摸清空定时器
$uList.hover(function() {
clearInterval(timer);
},
function() { //离开启动定时器
timer = setInterval(function() {
scrollList($uList);
}, 1000);
}).trigger("mouseleave"); //自动触发触摸事件
//滚动动画
function scrollList(obj) {
//获得当前<li>的高度
var scrollHeight = $("ul li:first").height();
//滚动出一个<li>的高度
$uList.stop().animate({
marginTop: -scrollHeight
}, 600,
function() {
//动画结束后,将当前<ul>marginTop置为初始值0状态,再将第一个<li>拼接到末尾。
$uList.css({
marginTop: 0
}).find("li:first").appendTo($uList);
});
}
});
</script>
</body>
</html>