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 Image Scrolling</title>
<style>
#scroll-container {
width: 100%;
overflow: hidden;
position: relative;
}
#scroll-content {
white-space: nowrap;
position: absolute;
}
.scroll-item {
display: inline-block;
margin-right: 10px;
}
</style>
</head>
<body>
<div id="scroll-container">
<div id="scroll-content">
<div class="scroll-item"><img src="image1.jpg" alt="Image 1"></div>
<div class="scroll-item"><img src="image2.jpg" alt="Image 2"></div>
<div class="scroll-item"><img src="image3.jpg" alt="Image 3"></div>
<!-- Add more images as needed -->
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
function scrollImages() {
var $scrollContent = $('#scroll-content');
var scrollSpeed = 2; // Adjust the speed as needed
$scrollContent.animate({
marginLeft: '-=' + $scrollContent.width() + 'px'
}, scrollSpeed * 1000, function() {
$scrollContent.css('marginLeft', 0);
scrollImages();
});
}
scrollImages();
});
</script>
</body>
</html>
scrollSpeed
变量的值。#scroll-content
的宽度足够大,包含所有滚动内容,并且在动画完成后重置marginLeft
。通过以上代码和解释,你应该能够实现一个基本的jQuery无缝图片连续滚动效果,并根据需要进行调整和优化。
没有搜到相关的沙龙