Javascript特效:tab标签
发布日期:2021-06-30 15:43:53
浏览次数:2
分类:技术文章
本文共 4351 字,大约阅读时间需要 14 分钟。
知识点
- 排他思想:有我无他。上面五个li和下面五个li,只有一个可以有class='selected’和display:block
- 闭包 && 为标签增加我们需要的属性
- 设置class的时候,必须通过className,不能直接通过class
运行效果
点击不同标签显示不同内容
代码
html
Title
css
*{ margin: 0; padding: 0; list-style: none;}html{ font-size: 10px;}a{ text-decoration: none; color: #000000;}#tab{ width: 50rem; height: 12rem; border: 0.1rem solid #cccccc; margin: 10rem auto;}#tab>#tab_header{ height: 3rem; width: 100%; /*background-color: red;*/}#tab>#tab_header>ul{ display: flex; justify-content: space-around; height: 100%; text-align: center;}#tab>#tab_header>ul>li{ background-color: #cccccc; height: 100%; width: 100%; font-size: 1.5rem; line-height: 3rem; cursor: pointer;}#tab>#tab_header>ul>li:hover{ color:#ff6700;}#tab>#tab_header>ul>.selected{ background-color: white !important; border-bottom: none !important;}#tab>#tab_content{ height: 9rem; width: 100%; position: relative;}#tab>#tab_content>div{ height: 100%; width: 100%; position: absolute; top: 0; left: 0; display: none;}#tab>#tab_content>div>ul{ height: 100%; display: flex; flex-wrap: wrap; text-align: center;}#tab>#tab_content>div>ul>li{ height: 4.5rem; width:50%; line-height: 4.5rem; font-size: 1.7rem;}#tab>#tab_content>div>ul>li>a:hover{ color:#ff6700;}
JavaScript
①闭包实现window.onload = function (ev) { // 1. 获取标签 var allLis = $('tab_header').getElementsByTagName('li'); var allDom = $('tab_content').getElementsByClassName('dom'); // 2. 遍历监听 for (var i = 0; i < allLis.length; i++) { var li = allLis[i]; (function (index) { li.addEventListener('mouseover',function (evt) { // 2.1 清楚同级别选中样式雷 for (var j = 0; j < allLis.length; j++) { allLis[j].className = ''; allDom[j].style.display ='none'; } // 2.2 设置当前li标签选中样式类 this.className = 'selected'; //必须通过className,不能直接通过class allDom[index].style.display = 'block'; }) })(i); } /** * 根据id返回获取节点元素 * @param {string}id * @returns {any} */ function $(id) { return typeof id === 'string' ? document.getElementById(id) : null; }};
② 为li增加新的属性记录
window.onload = function (ev) { // 1. 获取标签 var allLis = $('tab_header').getElementsByTagName('li'); var allDom = $('tab_content').getElementsByClassName('dom'); // 2. 遍历监听 for (var i = 0; i < allLis.length; i++) { var li = allLis[i]; li.index = i; li.addEventListener('mouseover',function (evt) { // 2.1 清楚同级别选中样式雷 for (var j = 0; j < allLis.length; j++) { allLis[j].className = ''; allDom[j].style.display ='none'; } // 2.2 设置当前li标签选中样式类 this.className = 'selected'; //必须通过className,不能直接通过class allDom[this.index].style.display = 'block'; }) } /** * 根据id返回获取节点元素 * @param {string}id * @returns {any} */ function $(id) { return typeof id === 'string' ? document.getElementById(id) : null; }};
转载地址:https://kaisarh.blog.csdn.net/article/details/103649516 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年05月01日 12时57分04秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
COMP7404 Machine Learing——ROC
2021-07-03
MATLAB与CUDA
2021-07-03
Linux png转jpg (convert命令)
2019-04-30
Ubuntu更新后终端中字体的颜色全是白色
2019-04-30
vscode git
2019-04-30
基于MATLAB的二进制数字调制与解调信号的仿真——2PSK
2019-04-30
基于MATLAB的模拟调制信号与解调的仿真——DSB
2019-04-30
HDU - 1166 敌兵布阵 (树状数组模板题/线段树模板题)
2019-04-30
CodeForces - 761C Dasha and Password (思维 暴力)
2019-04-30
CodeForces - 456C Boredom (dp)
2019-04-30
CodeForces - 675A Infinite Sequence(简单数论 细节)
2019-04-30
CodeForces - 1042B Vitamins (思维)
2019-04-30
ACM 2013 长沙区域赛 Collision (几何)
2019-04-30
ACM 2014 鞍山区域赛 E - Hatsune Miku (dp)
2019-04-30
反向传播&梯度下降 的直观理解程序(numpy)
2019-04-30
CodeForces - 931B World Cup (思维 模拟)
2019-04-30
ACM 2017 北京区域赛 J-Pangu and Stones(区间dp)
2019-04-30