我可以扩展div,但是如何缩小呢?

时间:2019-04-20 11:21:08

标签: javascript html css

我有一系列的div,当单击这些div时,将它们直接扩展到每个div下方。单击另一个div时,任何先前扩展的div都将关闭,并且单击的div下方的那个将被扩展。这真的很好。我的问题是,单击已经打开的div应该将其下面的div缩小,但不能这样做。 我的代码基于此示例... https://wpbeaches.com/create-expandcollapse-faq-accordion-collapse-click/ 这个例子非常完美-单击任何一个div都会关闭其他所有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示例中的代码一样。

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

我了解的是,单击答案链接不会关闭问题,但是如果单击已经打开的问题,它将关闭相应的答案。这是因为您仅将onClick函数应用于Accordian“ p”元素,而不应用于面板“ div”元素。

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

acc[i].onclick = function() {
相关问题