<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery向上滚动代码</title>
<link href="css/index.css" rel="stylesheet" type="text/css">
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<style>
.wrapper {
height: 40px;
margin: 0 auto;
background: red;
position: relative;
margin-top: 200px;
line-height: 40px;
overflow: hidden;
}
.container {
margin: 0 auto;
width: 80px;
line-height: 40px;
}
.test {
height: 30px;
line-height: 40px;
font-size: 15px;
padding: 5px 0;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="container">
<div class="test">跑马灯1</div>
<div class="test">跑马灯2</div>
<div class="test">跑马灯3</div>
</div>
</div>
<script type="text/javascript">
function autoScroll(obj) {
$('.container').animate({
marginTop : "-40px" //包裹层向上移动
},3000,function(){
$(this).css({marginTop : "0px"}).find(".test:first").appendTo(this); //将顶部div移动到底部 (这里需注意将div位置还原到初始位置)
})
}
$(function(){
var scroll = setInterval('autoScroll(".wrapper")',1500);
$(".wrapper").hover(function() {
clearInterval(scroll);
},function(){
scroll=setInterval('autoScroll(".wrapper")',1500);
});
});
</script>
</body>
</html>
网友评论