**要创建一个使用 jQuery 实现的左右滚动的缩略图滑块,你可以借助一些 HTML、CSS 和 JavaScript 来实现。以下是一个简单的示例:
HTML:**
<div class="thumbnail-container">
<div class="thumbnails">
<img src="image1.jpg" alt="Thumbnail 1">
<img src="image2.jpg" alt="Thumbnail 2">
<img src="image3.jpg" alt="Thumbnail 3">
<!-- 在此添加更多缩略图 -->
</div>
</div>
<div class="scroll-buttons">
<button class="prev">←</button>
<button class="next">→</button>
</div>
CSS:
.thumbnail-container {
width: 400px; /* 设置容器的宽度 */
overflow: hidden; /* 隐藏超出容器的内容 */
}
.thumbnails {
white-space: nowrap; /* 防止缩略图换行 */
}
.thumbnails img {
width: 100px; /* 设置缩略图的宽度 */
height: 75px; /* 设置缩略图的高度 */
margin: 0 10px; /* 设置缩略图之间的间距 */
}
.scroll-buttons {
margin-top: 10px;
}
button {
font-size: 20px;
padding: 5px 10px;
}
JavaScript/jQuery:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
const thumbnailContainer = $(".thumbnail-container");
const thumbnails = $(".thumbnails");
const scrollButtons = $(".scroll-buttons button");
const thumbnailWidth = $(".thumbnails img").outerWidth();
let currentPosition = 0;
// 向左滚动
scrollButtons.filter(".prev").click(function() {
if (currentPosition > 0) {
currentPosition -= thumbnailWidth;
thumbnails.animate({ left: currentPosition });
}
});
// 向右滚动
scrollButtons.filter(".next").click(function() {
if (currentPosition < thumbnails.width() - thumbnailContainer.width()) {
currentPosition += thumbnailWidth;
thumbnails.animate({ left: -currentPosition });
}
});
});
</script>
这个示例创建了一个包含缩略图的容器,以及左右滚动按钮。当点击左右按钮时,它使用 jQuery 的 animate 函数来滚动缩略图容器,从而实现左右滚动的效果。你可以根据自己的需求修改容器的宽度、缩略图的大小以及滚动按钮的样式。
版权属于:泽泽社长
本文链接:https://blog.zezeshe.com/archives/scrolling-function-using-jquery.html
本站未注明转载的文章均为原创,并采用
CC BY-NC-SA 4.0 授权协议,转载请注明来源,谢谢!