jQuery无缝滚动是一种网页效果,通过JavaScript和CSS实现元素(如<div>
)内容的连续滚动,给用户一种内容无限延伸的视觉效果。这种效果常用于新闻滚动、广告展示等场景。
以下是一个简单的jQuery实现上下无缝滚动的示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Seamless Scrolling</title>
<style>
#scroll-container {
width: 300px;
height: 100px;
overflow: hidden;
border: 1px solid #ccc;
}
#scroll-content {
position: relative;
height: 200px; /* 高度是容器高度的两倍 */
}
.scroll-item {
height: 100px;
line-height: 100px;
text-align: center;
border-bottom: 1px solid #ccc;
}
</style>
</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 src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
function scrollContent() {
var container = $('#scroll-container');
var content = $('#scroll-content');
content.animate({ top: -container.height() }, 5000, function() {
content.css('top', container.height());
scrollContent();
});
}
scrollContent();
});
</script>
</body>
</html>
通过以上方法,可以实现一个简单的jQuery无缝滚动效果,并解决常见的滚动问题。
没有搜到相关的沙龙