使用CSS3伪类选择器实现简单手风琴效果,与其功能的介绍

1.展示

2.原理

主要使用了C3选择器中的两个:

①:不():不包含,如DIV:否(。测试),就是DIV中选中不包含有试验类的,例如可以配合E:否(:最后的型)来排除掉最后一个元素并选中剩余的;

②:目标:目标的ID变为的location.hash的值时,也就是锚点选中它时选中它的选择器,例如锚点可以通过标签的HREF里加#来改变。

3.测试时遇到两点需要注意的问题

①给元素设置ID时不能用单个的字母,不然锚点锚不上;

②想给效果加上过渡持续时间时发现,目前显示等属性不受支持。因为是基于数值和时间的计算(长度,百分比,角度,颜色也能转换为数值)。

4.源码

<!DOCTYPE html>
<html lang="cn">

<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>C3手风琴效果</title>
    <style type="text/css">
        :root,
        body,
        * {
            margin: 0;
            padding: 0;
            list-style: none;
            user-select: none;
            font-size: 24px;
            text-align: center;
            text-decoration: none;
        }

        .wrapper {
            margin: 10px auto;
            width: 300px;
            height: 400px;
            border: 2px solid lightblue;
            background-color: beige;
        }

        a:not([id]) {
            display: block;
            color: rgb(0, 172, 202);
            width: 100%;
            margin-top: 3px;
            background-color: rgb(109, 255, 206);
            border-radius: 10px;
            cursor: pointer;
            transition-duration: 0.3s;
        }

        a:hover {
            border: 1px solid transparent;
        }

        a:active {
            border: 2px solid transparent;
        }

        li {
            margin: 2px auto;
            width: 85%;
            font-size: 16px;
            background-color: rgb(115, 206, 241);
        }

        ul:not(:target) {
            display: none;
        }

        div:not([class]) {
            color: rgb(0, 89, 255);
            font-size: 30px;
            font-weight: 600;
        }
    </style>
</head>

<body>
    <div class="wrapper">
        <a href="#" id="miao">手风琴</a>
        <a href="#aaa">AAA</a>
        <ul id="aaa">
            <li>帅</li>
            <li>帅帅帅</li>
            <li>帅帅帅帅帅</li>
        </ul>
        <a href="#bbb">BBB</a>
        <ul id="bbb">
            <li>酷</li>
            <li>酷酷酷</li>
            <li>酷酷酷酷酷</li>
        </ul>
        <a href="#ccc">CCC</a>
        <ul id="ccc">
            <li>美</li>
            <li>美美美</li>
            <li>美美美美美</li>
        </ul>
        <a href="#ddd">DDD</a>
        <ul id="ddd">
            <li>身体好</li>
            <li>身体好</li>
            <li>身体好</li>
        </ul>
    </div>
    <script src="./js/test.js"></script>
</body>

</html>

猜你喜欢

转载自blog.csdn.net/zmdmwh/article/details/85212954