html响应式手风琴

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="utf-8" />
    <!-- <meta http-equiv="Refresh" content="0; url=login.html" /> -->
    <title></title>
</head>
<style>
    @import url('https://fonts.googleapis.com/css?family=Hind:300,400&display=swap');

    * {
        box-sizing: border-box;

        &::before,
        &::after {
            box-sizing: border-box;
        }
    }

    body {
        margin: 0;
        padding: 0;
        font-family: 'Hind', sans-serif;
        background: #fff;
        color: #e5e5e5;
        display: flex;
        min-height: 100vh;
    }

    .container {
        margin: 0 auto;
        padding: 4rem;
        width: 48rem;
    }

    .accordion {
        .accordion-item {
            border-bottom: 1px solid#000;

            button[aria-expanded='true'] {
                border-bottom: 1px solid #03b5d2;
            }
        }

        button {
            position: relative;
            display: block;
            width: 100%;
            text-align: left;
            padding: 1em 0;
            color: #7288a2;
            font-size: 1.15rem;
            font-weight: 400;
            border: none;
            background: none;
            outline: none;

            &:hover,
            &:focus {
                cursor: pointer;
                color: #03b5d2;

                &::after {
                    cursor: pointer;
                    color: #03b5d2;
                    border: 1px solid #03b5d2;
                }
            }

            .accordion-title {
                padding: 1em 1.5em 1em 0;
            }

            .icon {
                display: inline-block;
                position: absolute;
                top: 18px;
                right: 0;
                width: 22px;
                height: 22px;
                border: 1px solid;
                border-radius: 22px;

                &::before {
                    display: block;
                    position: absolute;
                    content: '';
                    top: 9px;
                    left: 5px;
                    width: 10px;
                    height: 2px;
                    background: currentColor;
                }

                &::after {
                    display: block;
                    position: absolute;
                    content: '';
                    top: 5px;
                    left: 9px;
                    width: 2px;
                    height: 10px;
                    background: currentColor;
                }
            }
        }

        button[aria-expanded='true'] {
            color: #03b5d2;

            .icon {
                &::after {
                    width: 0;
                }
            }

            +.accordion-content {
                opacity: 1;
                max-height: 9em;
                transition: all 200ms linear;
                will-change: opacity, max-height;
            }
        }

        .accordion-content {
            opacity: 0;
            max-height: 0;
            overflow: hidden;
            transition: opacity 200ms linear, max-height 200ms linear;
            will-change: opacity, max-height;

            p {
                font-size: 1rem;
                font-weight: 300;
                margin: 2em 0;
            }
        }
    }
</style>

<body>
    <div class="container">
        <h2>常见问题</h2>
        <div class="accordion">
            <div class="accordion-item">
                <button id="accordion-button-1" aria-expanded="false">
                    <span class="accordion-title">
                        为什么是月亮
                        有时白天出去?
                </span>
                <span class="icon" aria-hidden="true"></span></button>
                <div class="accordion-content">
                    <p>照顾他会很痛苦,但他们会照顾他。
                        痛苦和巨大的痛苦。一支生命元素的箭和两只狮子,就像一把宝石火炬。
                        生命被强大的力量所中止。</p>
                </div>
            </div>
            <div class="accordion-item">
                <button id="accordion-button-1" aria-expanded="false">
                    <span class="accordion-title">
                        为什么是月亮
                        有时白天出去?
                </span>
                <span class="icon" aria-hidden="true"></span></button>
                <div class="accordion-content">
                    <p>照顾他会很痛苦,但他们会照顾他。
                        痛苦和巨大的痛苦。一支生命元素的箭和两只狮子,就像一把宝石火炬。
                        生命被强大的力量所中止。</p>
                </div>
            </div>
        </div>
    </div>

    <script>
        const items = document.querySelectorAll(".accordion button");

        function toggleAccordion() {
            const itemToggle = this.getAttribute('aria-expanded');

            for (i = 0; i < items.length; i++) {
                items[i].setAttribute('aria-expanded', 'false');
            }

            if (itemToggle == 'false') {
                this.setAttribute('aria-expanded', 'true');
            }
        }

        items.forEach(item => item.addEventListener('click', toggleAccordion));
    </script>
</body>

</html>

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

滴滴答答哒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值