无缝左右滚动是一种网页效果,通过使用 jQuery 库来实现内容的平滑循环滚动。这种效果常用于展示新闻、图片或其他列表信息,使用户在浏览时能够持续看到新的内容。
以下是一个简单的 jQuery 无缝左右滚动的实现示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Seamless Scrolling</title>
<style>
#scrollingDiv {
width: 100%;
overflow: hidden;
white-space: nowrap;
}
#scrollContent {
display: inline-block;
animation: scroll 15s linear infinite;
}
@keyframes scroll {
0% { transform: translateX(100%); }
100% { transform: translateX(-100%); }
}
</style>
</head>
<body>
<div id="scrollingDiv">
<div id="scrollContent">
<span>Content 1 - This is a scrolling text example.</span>
<span>Content 2 - Another piece of information.</span>
<span>Content 3 - Yet more text to scroll through.</span>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var $content = $('#scrollContent');
var contentWidth = 0;
$content.children().each(function() {
contentWidth += $(this).outerWidth(true);
});
$content.css('width', contentWidth);
// Clone content for seamless scrolling
$content.clone().appendTo('#scrollingDiv');
});
</script>
</body>
</html>
translate3d
)。requestAnimationFrame
来优化动画性能。通过以上方法,可以有效实现并维护一个流畅的无缝左右滚动效果。
领取专属 10元无门槛券
手把手带您无忧上云