用CSS制作Tab页切换的步骤方法是什么
Admin 2022-06-07 群英技术资讯 879 次浏览
最近切一个页面的时候涉及到了一个tab切换的部分,因为不想用js想着能不能用纯CSS的选择器来实现切换效果。搜了一下大致有下面三种写法。
1、利用 :hover 选择器
缺点:只有鼠标在元素上面的时候才有效果,无法实现选中和默认显示某一个的效果
2、利用 a标签的锚点 + :target选择器
缺点:因为锚点会将选中的元素滚动到页面最上面,每次切换位置都要移动,体验极差。
3、利用 label和radio 的绑定关系和 radio选中时的:checked 来实现效果
缺点:HTML结构元素更复杂
经过实验发现第三种方法达到的效果最好。所以下面讲一下第三种实现的方法。
这种方法的写法不固定,我查资料的时候各种各样的写法都有一度让我一头雾水的。最后看完发现总体思路都是一样的,无非就是下面的几个步骤。
/* 当radio为选中状态时设置它的test-label兄弟元素的属性 */ input[type="radio"]:checked+.test-label { /* 为了修饰存在的边框背景属性 */ border-color: #cbcccc; border-bottom-color: #fff; background: #fff; /* 为了修饰存在的层级使下边框遮挡下方div的上边框 */ z-index: 10; } /* 当radio为选中状态时设置与它同级的tab-box元素的显示层级 */ input[type="radio"]:checked~.tab-box { /* 选中时提升层级,遮挡其他tab页达到选中切换的效果 */ z-index: 5; }
这样就可以实现一个Tab页切换的效果了,不用一点儿js,当然肯定也有兼容性的问题。实际操作中tab页还是使用js比较好。下面是小Demo的代码,样式比较多主要是为了实现各种选中效果, 真正用来达到选择切换目地的核心代码就几行
演示地址
代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>CSS实现Tab切换效果</title> <style> ul { margin: 0; padding: 0; } .clearfloat { zoom: 1; } .clearfloat::after { display: block; clear: both; content: ""; visibility: hidden; height: 0; } .tab-list { position: relative; } .tab-list .tab-itom { float: left; list-style: none; margin-right: 4px; } .tab-itom .test-label { position: relative; display: block; width: 85px; height: 27px; border: 1px solid transparent; border-top-left-radius: 5px; border-top-right-radius: 5px; line-height: 27px; text-align: center; background: #e7e8eb; } .tab-itom .tab-box { /* 设置绝对定位方便定位相对于tab-list栏的位置,同时为了可以使用z-index属性 */ position: absolute; left: 0; top: 28px; width: 488px; height: 248px; border: 1px solid #cbcccc; border-radius: 5px; border-top-left-radius: 0px; background: #fff; /* 设置层级最低方便选中状态遮挡 */ z-index: 0; } /* 用绝对定位使按钮脱离文档流,透明度设置为0将其隐藏 */ input[type="radio"] { position: absolute; opacity: 0; } /* 利用选择器实现 tab切换 */ /* 当radio为选中状态时设置它的test-label兄弟元素的属性 */ input[type="radio"]:checked + .test-label { /* 为了修饰存在的边框背景属性 */ border-color: #cbcccc; border-bottom-color: #fff; background: #fff; /* 为了修饰存在的层级使下边框遮挡下方div的上边框 */ z-index: 10; } /* 当radio为选中状态时设置与它同级的tab-box元素的显示层级 */ input[type="radio"]:checked ~ .tab-box { /* 选中时提升层级,遮挡其他tab页达到选中切换的效果 */ z-index: 5; } </style> </head> <body class="clearfloat"> <ul class="tab-list clearfloat"> <li class="tab-itom"> <input type="radio" id="testTabRadio1" class="test-radio" name="tab" checked="checked"> <label class="test-label" for="testTabRadio1">选项卡一</label> <div class="tab-box"> 111111111111 </div> </li> <li class="tab-itom"> <input type="radio" id="testTabRadio2" class="test-radio" name="tab"> <label class="test-label" for="testTabRadio2">选项卡二</label> <div class="tab-box"> 2222222222222 </div> </li> <li class="tab-itom"> <input type="radio" id="testTabRadio3" class="test-radio" name="tab"> <label class="test-label" for="testTabRadio3">选项卡三</label> <div class="tab-box"> 33333333333333 </div> </li> </ul> </body> </html>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
我们知道CSS的font属性是字体样式设置的一个属性,我们经常需要对字体样式进行操作,对此这篇就给大家来讲讲font属性的时候,以及使用需要注意的一些事项问题,文中的示例代码有一定的参考价值,有需要的朋友可以了解看看。
HTML表头怎样设置垂直对齐,方法是什么?想对这方面内容了解清楚的朋友,可以看看小编所整理的资料。
今天给大家分享的是用css怎样实现鼠标悬浮图片缩放的内容,下文有用纯CSS实现的代码供大家参考,实现原理是比较简单的,对新手学习CSS有一定的帮助,感兴趣的朋友接下来跟随小编一起看看吧。
css中删除input输入框的阴影:1、阴影是浏览器生成的。2、css样式分为内外阴影。2、去除input阴影,直接在input css里写-webkit-appearance: none; 就行了。
本文主要介绍了纯html+css实现Element loading效果,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008