jQuery无缝隙连续滚动是一种网页效果,通过JavaScript和CSS实现内容的自动滚动,给用户一种内容连续不断的视觉效果。这种效果常用于新闻滚动、广告展示等场景。
以下是一个简单的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: 100%;
overflow: hidden;
position: relative;
}
#scroll-content {
white-space: nowrap;
position: absolute;
top: 0;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div id="scroll-container">
<div id="scroll-content">
<span>新闻1</span>
<span>新闻2</span>
<span>新闻3</span>
<span>新闻4</span>
<span>新闻5</span>
</div>
</div>
<script>
$(document).ready(function() {
var $scrollContent = $('#scroll-content');
var $container = $('#scroll-container');
var contentWidth = $scrollContent.width();
var containerWidth = $container.width();
var scrollSpeed = 2; // 滚动速度
function scrollContent() {
$scrollContent.animate({
marginLeft: -scrollSpeed
}, 10, function() {
$scrollContent.css({
marginLeft: containerWidth
}).find('span:first').appendTo($scrollContent);
});
}
setInterval(scrollContent, 20);
});
</script>
</body>
</html>
#scroll-container
的overflow
属性设置为hidden
,并且#scroll-content
的white-space
属性设置为nowrap
。scrollSpeed
变量的值。通过以上示例代码和解决方法,你可以实现一个简单的jQuery无缝隙连续滚动效果,并根据需要进行调整和优化。