JS如何实现多重选项卡切换轮播图
发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,这篇文章将为大家详细讲解有关JS如何实现多重选项卡切换轮播图,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。轮播图简介:在一个网站的某一特定模块,通过电脑上鼠标点击或
千家信息网最后更新 2024年09月22日JS如何实现多重选项卡切换轮播图
这篇文章将为大家详细讲解有关JS如何实现多重选项卡切换轮播图,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
轮播图简介:在一个网站的某一特定模块,通过电脑上鼠标点击或鼠标移入、手机上手指滑动后,可以分别展示不同的图片,这个模块就叫做轮播模块。
html布局部分:
CSS样式部分:
/* 为了布局方便,容器里大多采用的flex */#box { position: relative; width: 460px; height: 300px; margin: 40px auto; border: 1px solid rgb(109, 98, 98); user-select: none; } /* 侧边栏布局 */ .leftbar { display: flex; flex-direction: column; justify-content: space-between; position: absolute; top: -1px; left: -80px; width: 80px; height: 100%; text-align: center; font-size: 20px; cursor: pointer; } .leftbar div { flex: 1; line-height: 100px; background-color: cadetblue; letter-spacing: 5px; } .leftbar div:nth-child(2) { border-top: 1px solid #fff; border-bottom: 1px solid #fff; } /* 底部切换按钮样式设计 */ .bottombar { display: flex; justify-content: space-between; position: absolute; bottom: -1px; right: -1px; z-index: 10; width: 200px; height: 30px; cursor: pointer; } .bottombar div { flex: 1; line-height: 30px; background-color: rgb(232, 233, 235, .5); text-align: center; font-weight: 700; } .bottombar div~div { border-left: 1px solid grey; } img { position: absolute; display: block; width: 460px; height: 300px; } .show { z-index: 5; } .leftbar .checked, .bottombar .checked { background-color: rgb(241, 5, 5); }
javascript逻辑实现部分:
var Box = document.querySelector('#box'), pic = Box.querySelectorAll('.pic'), Idx = 0, index = 0, timer = null, ltDiv = Box.querySelector('.leftbar'), btDiv = Box.querySelector('.bottombar'), Img = Box.querySelectorAll('img'); function createBtBar(len) {//动态创建底部切换按钮 var str = ''; for (var i = 0; i < len; i++) { str += `${i + 1}`; } btDiv[xss_clean] = str; btDiv.children[0].classList.add('checked'); } function initialize() {//页面初始状态 createBtBar(pic[0].children.length); } initialize(); function clearZindex() {//重置所有图片的定位层级 for (var k = 0; k < Img.length; k++) { Img[k].classList.remove('show'); } } ltDiv.addEventListener('click', (e) => {//侧边栏项目切换 if (e.target.tagName.toLowerCase() === 'div') { for (var j = 0; j < ltDiv.children.length; j++) { ltDiv.children[j].classList.remove('checked'); } clearZindex(); Idx = 0; index = getEleIdx(e.target); ltDiv.children[index].classList.add('checked'); pic[index].children[0].classList.add('show'); createBtBar(pic[index].children.length); } }); btDiv.addEventListener('click', (e) => {//委托监听底部切换按钮 if (e.target.tagName.toLowerCase() === 'div') { function changePic(callback) { btDiv.children[Idx].classList.remove('checked'); clearZindex(); callback && callback(); btDiv.children[Idx].classList.add('checked'); pic[index].children[Idx].classList.add('show'); } changePic(function () { Idx = getEleIdx(e.target); }); } }); function getEleIdx(item) {//获取DOM元素下标 var elements = item[xss_clean].children; for (var i = 0, len = elements.length; i < len; i++) { if (item === elements[i]) { return i; } } } function loopShow() {//循环自动展示 clearInterval(timer); timer = setInterval(function () { pic[index].children[Idx].classList.remove('show'); btDiv.children[Idx].classList.remove('checked'); Idx += 1; if (Idx < 0 || Idx > pic[index].children.length - 1) { Idx = 0; } pic[index].children[Idx].classList.add('show'); btDiv.children[Idx].classList.add('checked'); }, 1000); } loopShow(); Box.addEventListener('mouseover', function () { clearInterval(timer);//鼠标移入展示区停止轮播 }); Box.addEventListener('mouseout', function () { loopShow();//鼠标移出展示区自动轮播 });
具体实现的展示效果入下:
关于"JS如何实现多重选项卡切换轮播图"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
切换
鼠标
布局
底部
按钮
模块
篇文章
部分
侧边
图片
展示区
更多
样式
不同
不错
实用
下标
元素
内容
动态
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
亚信安全深度反病毒服务器
linux怎么备份数据库
网络安全考研密码学
五大网络安全
维护网络安全的意思
服务器无法识别到网卡
软件开发与编程培训
郧西好的软件开发专业服务
戴尔r740安装什么数据库
和平精英要下载多少个服务器
新买的苹果x应用与数据库
阜新购物软件开发
安徽软件开发人月标准
lol服务器为啥老蹦
软件开发项目具体内容
网络安全大队职业
项目网络安全审核
我的世界什么是数据库
外空网络安全
开发游戏服务器
工作站图像软件开发
衡水软件开发教学教学视频
网络安全法第二十一条释义
支付宝数据库技术好吗
商业版本数据库最好的是
适用于服务器的安全软件
松江区服务器设备回收厂家
数据库技术ncre
如何衡量网络安全
古诗词取名软件开发