jquery 列表自动滚动
时间: 2024-01-11 11:03:52 浏览: 16
以下是使用jQuery实现列表自动滚动的代码,当鼠标悬停在列表上时,滚动停止并提示,当鼠标离开时,滚动继续。
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jQuery列表自动滚动</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<style>
.wrapper {
width: 200px;
height: 100px;
overflow: hidden;
position: relative;
}
ul {
position: absolute;
top: 0;
left: 0;
margin: 0;
padding: 0;
list-style: none;
}
li {
height: 50px;
line-height: 50px;
border-bottom: 1px solid #ccc;
text-align: center;
}
</style>
</head>
<body>
<div class="wrapper">
<ul>
<li>新闻1</li>
<li>新闻2</li>
<li>新闻3</li>
<li>新闻4</li>
<li>新闻5</li>
</ul>
</div>
<script>
$(function() {
play();
var id;
function play() {
id = setInterval(function() {
$("ul").animate({
"top": "-50px"
}, 1000, function() {
$(this).append($("li:first"));
$(this).css("top", "0")
});
}, 1500)
}
$(".wrapper").hover(
function() {
clearInterval(id);
alert("滚动已停止");
},
function() {
play();
}
)
})
</script>
</body>
</html>
```