CSS波浪选择器
2020-07-18 05:08
756 查看
~(波浪号)概念
~(波浪号):A ~ B表示选择A标签后的所有B标签,但是A和B标签必须有相同的父元素。
<style> h3~h5{ color: red; } </style> <body> <div> <h1>我是第1行</h1> <h2>我是第2行</h2> <div> <h3>我是第3行</h3> <h5>我是测试行</h5> <h4>我是第6行</h4> <h5>我是测试行</h5> </div> <h4>我是第4行</h4> <h5>我是第5行</h5> </div> </body>
【原文https://www.jianshu.com/p/d875f680fc6b】
使用案例
附上一个用了这个知识点的完整案例,导航栏
效果如图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Document</title> <link rel="stylesheet" type="text/css" href="./style.css"> </head> <body> <ul> <li class="active"> <a href="">link1</a></li> <li> <a href="javascript:void(0);">link2</a></li> <li> <a href="javascript:void(0);">link3</a></li> <li> <a href="javascript:void(0);">link4</a></li> <li> <a href="javascript:void(0);">link5</a></li> <li> <a href="#">link6</a></li> <li class="slide"></li> </ul> </body> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script> $(document).ready(function() { $('li').on('click',function(){ $(this).siblings().removeClass('active'); $(this).addClass('active'); console.log("ok"); }) }); </script> </html>
body{ margin:0; padding: 0; background: #ffeb3b; } ul{ position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%,-50%); -moz-transform: translate(-50%,-50%); -ms-transform: translate(-50%,-50%); -o-transform: translate(-50%,-50%); transform: translate(-50%,-50%); display: flex; background:#000; border-radius: 30px; overflow: hidden; box-shadow: 0 0 0 5px #333; margin:0; padding:0; } ul li{ list-style-type: none; width: 150px; } ul li a { display: block; padding: 20px; text-align: center; color: #fff; transition:0.5s; text-decoration: none; } ul li.active a{ color: #262626; } .slide{ position: absolute; width: 150px; height: 100%; top: 0; left: 0; background:#fff; z-index: -1; transition:0.5s; opacity: 0; } ul li:nth-child(1).active ~ .slide{ left:0; opacity:1; } ul li:nth-child(2).active ~ .slide{ left:150px; opacity:1; } ul li:nth-child(3).active ~ .slide{ left:300px; opacity:1; } ul li:nth-child(4).active ~ .slide{ left:450px; opacity:1; } ul li:nth-child(5).active ~ .slide{ left:600px; opacity:1; } ul li:nth-child(6).active ~ .slide{ left:750px; opacity:1; }
相关文章推荐
- css ~波浪线选择器连接符General Sibling Selector
- CSS入门(2)-选择器
- CSS 选择器参考手册
- CSS后代选择器可能的错误认识
- css学习之基本选择器+伪类选择器
- Python爬虫包 BeautifulSoup 学习(十一) CSS 选择器
- HTML常用标记、CSS三种选择器
- CSS_选择器
- CSS 扩展选择器
- [CSS]层叠样式表中的四种基础选择器的使用教程
- CSS后代选择器
- CSS中:before和:after选择器的用法
- css 选择器 优先级
- css 的几个选择器
- CSS复合选择器和div盒子模型
- jQuery的属性、css、文档处理、事件、选择器以及jsp要点简记
- CSS的三种声明方式和选择器(具体代码实现)
- css继承样式怎么控制?用选择器
- CSS基础1-选择器
- 选择器总结 浮动 CSS的三大特性