【问题标题】:I can expand my div, but how do I shrink it?我可以扩展我的 div,但如何缩小它?
【发布时间】:2019-04-20 11:21:08
【问题描述】:

我有一系列 div,单击它们时,会在每个 div 的正下方展开一个 div。单击另一个 div 时,任何先前展开的 div 都将关闭,而单击的 div 下方的 div 会展开。这真的很好用。我的问题是单击已经打开的 div 应该缩小它下面的 div,但事实并非如此。 我的代码基于这个例子...... https://wpbeaches.com/create-expandcollapse-faq-accordion-collapse-click/ 此示例完美运行 - 单击任何 div 会关闭任何其他 div,包括它自己的(如果它已经打开)。 我的代码运行良好,但如果它已经打开,单击一个 div 并不会关闭它下面的 div。

CSS:

        /* Style the element that is used to open and close the accordion class */
        div.accordion {
            background-color: #fff;
            color: #444;
            cursor: pointer;
            padding: 0px;
            width: 100%;
            text-align: left;
            border: none;
            outline: none;
            transition: 0.4s;
            margin-bottom:10px;
            opacity: 0.70;
            filter: alpha(opacity=70);
        }

        /* Add a background color to the accordion if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */
        div.accordion.active, div.accordion:hover {
            background: rgb(255,255,255);
            background: linear-gradient(90deg, rgba(255,255,255,1) 0%, rgba(232,231,222,1) 50%, rgba(255,255,255,1) 100%);
            opacity: 1;
            filter: alpha(opacity=100);
        }

        /* Style the element that is used for the panel class */

        div.panel {
            padding: 0 18px;
            background-color: white;
            max-height: 0;
            overflow: hidden;
            transition: 0.4s ease-in-out;
            opacity: 0;
            margin-bottom:10px;
        }

        div.panel.show {
            opacity: 1;
            max-height: 500px; /* Whatever you like, as long as its more than the height of the content (on all screen sizes) */
        }

JAVASCRIPT:

    <script>
        document.addEventListener("DOMContentLoaded", function(event) { 
        var acc = document.getElementsByClassName("accordion");
        var panel = document.getElementsByClassName('panel');

        for (var i = 0; i < acc.length; i++) {
            acc[i].onclick = function() {
                var setClasses = !this.classList.contains('active');
                setClass(acc, 'active', 'remove');
                setClass(panel, 'show', 'remove');

                if (setClasses) {
                    this.classList.toggle("active");
                    this.nextElementSibling.classList.toggle("show");
                }
            }
        }

        function setClass(els, className, fnName) {
            for (var i = 0; i < els.length; i++) {
                els[i].classList[fnName](className);
            }
        }

        });
    </script>

HTML:

我的 html 就像上面 wpbeaches.com 示例中的代码一样。

【问题讨论】:

    标签: javascript html css


    【解决方案1】:

    在 else 情况下添加删除逻辑或删除 if 条件,因为您使用的是切换功能。在您的情况下,切换的工作方式类似于 add 方法。只需检查代码

                if (setClasses) {
                    this.classList.add("active");
                    this.nextElementSibling.classList.add("show");
                } else {
                    this.classList.remove("active");
                    this.nextElementSibling.classList.remove("show");
                }
    

    【讨论】:

    • 非常感谢 Prashanth。我尝试添加您的 else 案例,但没有任何区别(扩展的 div 仍然没有关闭)。我还尝试删除 if 语句,但是根本没有 div 会打开或关闭。
    • 我已尝试在Codepen 上复制您的问题。我点击问题,答案就打开了。我点击打开的问题,打开的答案被关闭。这是你问的没有发生在你身上吗?
    • 非常感谢 Prashanth 抽出宝贵时间来做这件事。我终于弄清楚了为什么它不适用于我的版本。在我的手风琴 div 中,我有一张桌子。我无意中为每个 以及包含的 div 分配了一个“手风琴”类。一旦我从 中删除了该类,它就可以完美地工作。我真的不知道我为什么首先这样做,除了愚蠢!再次感谢,您帮了大忙!
    【解决方案2】:

    我的理解是点击答案链接不会关闭问题,但如果我们点击已经打开的问题,它将关闭相应的答案。这是因为您仅在手风琴“p”元素上应用了 onClick 功能,而不是在面板“div”元素上。

    您可以将 1 个通用类应用于手风琴元素和面板元素,并在该类上应用 onCLick 函数。

    acc[i].onclick = function() {
    

    【讨论】:

    • 是的,当我再次单击一个已经打开的问题时,我希望它关闭,就像示例一样。我的只有在我点击另一个问题时才关闭答案 - 而不是父问题。示例代码和我自己的代码之间的区别在于,我删除了在每个问题上显示 + 和 - 按钮的 css。您可能会说 javascript 绝对不是我的强项之一!
    • 对不起tbaveja - 我刚刚重新阅读了你的信息。我不想点击答案。我只希望能够单击该问题 - 一次打开它,一次关闭它。点击任何问题会关闭任何其他未解决的问题(正确),但如果它已经打开,它不会自行关闭。
    • 它对我有用,我想也可以在 Codepen 上工作,正如 Prashanth 所提到的。你能告诉我你在上面的代码上运行的是哪个浏览器和操作系统吗?
    • 非常感谢 tbaveja 抽出宝贵时间查看此内容。你和 Prashanth 都帮了大忙。请参阅上面我对 Prashanth 的回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 2013-08-13
    • 1970-01-01
    • 2013-05-21
    相关资源
    最近更新 更多