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;
height: 200px;
}
.scroll-item {
height: 50px;
line-height: 50px;
text-align: center;
}
</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 = 1; // 滚动速度
content.animate({
top: -itemHeight
}, scrollSpeed * 1000, function() {
content.css('top', container.height());
scrollContent();
});
}
scrollContent();
});
</script>
</body>
</html>
requestAnimationFrame
代替 setTimeout
或 setInterval
。通过以上方法,可以实现一个简单的 jQuery 上下无缝滚动效果,并解决一些常见问题。
没有搜到相关的沙龙