jQuery无缝上下滚动是一种网页效果,通过使用jQuery库实现页面内容的无缝滚动。这种效果通常用于新闻滚动、广告展示等场景,可以吸引用户的注意力并提高页面的动态效果。
以下是一个简单的jQuery无缝上下滚动的示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery无缝上下滚动</title>
<style>
#scroll-container {
width: 300px;
height: 100px;
overflow: hidden;
border: 1px solid #ccc;
}
#scroll-content {
position: relative;
}
.scroll-item {
height: 50px;
line-height: 50px;
text-align: center;
border-bottom: 1px solid #ccc;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div id="scroll-container">
<div id="scroll-content">
<div class="scroll-item">Item 1</div>
<div class="scroll-item">Item 2</div>
<div class="scroll-item">Item 3</div>
<div class="scroll-item">Item 4</div>
</div>
</div>
<script>
$(document).ready(function() {
function scrollContent() {
var container = $('#scroll-container');
var content = $('#scroll-content');
var itemHeight = $('.scroll-item').height();
var scrollSpeed = 2000; // 滚动速度,单位为毫秒
content.animate({ top: -itemHeight }, scrollSpeed, function() {
content.css('top', container.height());
scrollContent();
});
}
scrollContent();
});
</script>
</body>
</html>
通过以上方法,可以实现一个简单的jQuery无缝上下滚动效果,并解决常见的滚动问题。
领取专属 10元无门槛券
手把手带您无忧上云