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

1.展示

2.原理

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

①:not():不包含,如div:not(.test),就是在div中选中不包含有test类的,例如可以配合E:not(:last-of-type)来排除掉最后一个元素并选中剩余的;

②:target:目标的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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值