jQuery轮播图6张图 带切换按钮 带预览小图 小图只显示前后4张

时间: 2024-05-05 22:20:16 浏览: 11
以下是一个简单的jQuery轮播图代码,其中包括6张图片、切换按钮和预览小图。预览小图只显示前后4张图片。 HTML代码: ``` <div class="slideshow"> <div class="slides"> <img src="img/1.jpg"> <img src="img/2.jpg"> <img src="img/3.jpg"> <img src="img/4.jpg"> <img src="img/5.jpg"> <img src="img/6.jpg"> </div> <div class="controls"> <div class="prev"><</div> <div class="next">></div> </div> <div class="thumbnails"> <div class="thumb-prev"><</div> <div class="thumb-next">></div> <ul> <li><img src="img/1.jpg"></li> <li><img src="img/2.jpg"></li> <li><img src="img/3.jpg"></li> <li><img src="img/4.jpg"></li> <li><img src="img/5.jpg"></li> <li><img src="img/6.jpg"></li> </ul> </div> </div> ``` CSS代码: ``` .slideshow { position: relative; width: 800px; height: 400px; margin: 0 auto; overflow: hidden; } .slides { position: absolute; width: 100%; height: 100%; } .slides img { width: 100%; height: 100%; display: none; } .slides img.active { display: block; } .controls { position: absolute; top: 50%; transform: translateY(-50%); width: 60px; height: 60px; background-color: rgba(0, 0, 0, 0.5); color: #fff; font-size: 24px; text-align: center; line-height: 60px; cursor: pointer; } .controls:hover { background-color: rgba(0, 0, 0, 0.8); } .prev { left: 0px; } .next { right: 0px; } .thumbnails { position: absolute; bottom: 0px; width: 100%; height: 80px; background-color: rgba(0, 0, 0, 0.5); overflow: hidden; } .thumbnails ul { position: absolute; left: 0px; top: 0px; width: 1000%; height: 100%; margin: 0; padding: 0; list-style: none; transition: left 0.5s ease-in-out; } .thumbnails li { float: left; width: calc(100% / 6); height: 100%; overflow: hidden; } .thumbnails li img { width: 100%; height: 100%; opacity: 0.5; cursor: pointer; } .thumbnails li img.active { opacity: 1; } .thumb-prev { position: absolute; left: 0px; top: 0px; width: 60px; height: 100%; background-color: rgba(0, 0, 0, 0.5); color: #fff; font-size: 24px; text-align: center; line-height: 60px; cursor: pointer; } .thumb-prev:hover { background-color: rgba(0, 0, 0, 0.8); } .thumb-next { position: absolute; right: 0px; top: 0px; width: 60px; height: 100%; background-color: rgba(0, 0, 0, 0.5); color: #fff; font-size: 24px; text-align: center; line-height: 60px; cursor: pointer; } .thumb-next:hover { background-color: rgba(0, 0, 0, 0.8); } ``` jQuery代码: ``` $(document).ready(function() { // 初始化轮播图 var slideIndex = 1; showSlide(slideIndex); // 切换按钮事件 $('.prev').click(function() { slideIndex -= 1; showSlide(slideIndex); }); $('.next').click(function() { slideIndex += 1; showSlide(slideIndex); }); // 预览小图事件 $('.thumbnails li img').click(function() { slideIndex = $(this).parent().index() + 1; showSlide(slideIndex); }); $('.thumb-prev').click(function() { moveThumbnails(-1); }); $('.thumb-next').click(function() { moveThumbnails(1); }); // 轮播图显示函数 function showSlide(n) { var slides = $('.slides img'); var thumbnails = $('.thumbnails li img'); if (n > slides.length) { slideIndex = 1; } if (n < 1) { slideIndex = slides.length; } slides.removeClass('active'); slides.eq(slideIndex - 1).addClass('active'); thumbnails.removeClass('active'); thumbnails.eq(slideIndex - 1).addClass('active'); updateThumbnails(); } // 预览小图移动函数 function moveThumbnails(n) { var thumbnails = $('.thumbnails ul'); var thumbWidth = $('.thumbnails li').width(); var thumbCount = $('.thumbnails li').length; var thumbVisible = Math.min(thumbCount, 8); var thumbOffset = thumbnails.position().left - (n * thumbWidth * thumbVisible); if (thumbOffset > 0) { thumbOffset = 0; } if (thumbOffset < -(thumbCount - thumbVisible) * thumbWidth) { thumbOffset = -(thumbCount - thumbVisible) * thumbWidth; } thumbnails.animate({left: thumbOffset}, 500); } // 预览小图更新函数 function updateThumbnails() { var thumbnails = $('.thumbnails li img'); var thumbCount = $('.thumbnails li').length; var thumbVisible = Math.min(thumbCount, 8); var thumbIndex = slideIndex - 1; thumbnails.removeClass('active'); thumbnails.eq(thumbIndex).addClass('active'); for (var i = 0; i < thumbCount; i++) { var thumb = thumbnails.eq(i); if (i >= thumbIndex - 4 && i <= thumbIndex + 3) { thumb.parent().show(); } else { thumb.parent().hide(); } } } }); ```

相关推荐

最新推荐

recommend-type

opencv实现多张图像拼接

主要为大家详细介绍了opencv实现多张图像拼接功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

python实现批量处理将图片粘贴到另一张图片上并保存

今天小编就为大家分享一篇python实现批量处理将图片粘贴到另一张图片上并保存,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

在tensorflow下利用plt画论文中loss,acc等曲线图实例

主要介绍了在tensorflow下利用plt画论文中loss,acc等曲线图实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

python pyecharts 实现一个文件绘制多张图

主要介绍了python pyecharts 实现一个文件绘制多张图,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

邻接表或者邻接矩阵为存储结构实现连通无向图的深度优先和广度优先遍历

程序设计任务: 设计一个程序,实现以邻接表或者邻接矩阵为存储结构,实现连通无向图的深度优先和广度优先遍历。基本要求:以邻接表或者邻接矩阵为存储结构,实现连通无向图的深度优先和广度优先遍历。以用户指定的...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB图像处理算法宝典:从理论到实战

![MATLAB图像处理算法宝典:从理论到实战](https://img-blog.csdnimg.cn/20200717112736401.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2d1emhhbzk5MDE=,size_16,color_FFFFFF,t_70) # 1. MATLAB图像处理基础理论 MATLAB图像处理是一种利用MATLAB编程语言进行图像处理的强大工具。它提供了丰富的函数和工具箱,用于图像获取、增强、分
recommend-type

matlab中1/x的非线性规划

在MATLAB中,可以使用非线性规划函数(`fmincon`)来优化一个包含1/x的非线性目标函数。下面是一个简单的例子: ```matlab % 定义目标函数 fun = @(x) 1/x; % 定义约束函数(这里没有约束) nonlcon = []; % 定义初始点 x0 = 1; % 定义优化选项 options = optimoptions('fmincon', 'Display', 'iter'); % 进行非线性规划 [x, fval] = fmincon(fun, x0, [], [], [], [], [], [], nonlcon, options); ``` 在
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。