下拉折叠效果

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .collapse-content {
            max-height: 0;
            overflow: hidden;
            transition: max-height 0.2s ease-out;
        }
    </style>
</head>

<body>
    <button class="collapse-button">Toggle Collapse</button>
    <div class="collapse-content">
        <p>This is some text that will be hidden or shown when the button is clicked.</p>
        <p>This is some text that will be hidden or shown when the button is clicked.</p>
        <p>This is some text that will be hidden or shown when the button is clicked.</p>
        <p>This is some text that will be hidden or shown when the button is clicked.</p>
        <p>This is some text that will be hidden or shown when the button is clicked.</p>
        <p>This is some text that will be hidden or shown when the button is clicked.</p>
        <p>This is some text that will be hidden or shown when the button is clicked.</p>
        <p>This is some text that will be hidden or shown when the button is clicked.</p>
    </div>
    <script>
        document.querySelector('.collapse-button').addEventListener('click', function () {
            var content = document.querySelector('.collapse-content');
            if (content.style.maxHeight) {
                // 当点击时,如果section是显示的,则隐藏它
                content.style.maxHeight = null;
            } else {
                // 当点击时,如果section是隐藏的,则显示它
                content.style.maxHeight = content.scrollHeight + "px";
            }
        });
    </script>
</body>

</html>
posted @ 2024-07-15 18:17  unique-yb  阅读(1)  评论(0编辑  收藏  举报