我可以扩展我的div,但我该如何缩小它?

问题描述 投票:1回答:2

我有一系列的div,当点击时,会在每个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示例中的代码一样。

javascript html css
2个回答
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");
            }

0
投票

我所理解的是点击回答链接不会关闭问题,但如果我们点击已经打开的问题,它将关闭相应的答案。这是因为你只对onmanian“p”元素应用了onClick函数,而不是在面板“div”元素上。

您可以将1个公共类应用于accordion和panel元素,并在该类上应用onCLick函数。

acc[i].onclick = function() {
© www.soinside.com 2019 - 2024. All rights reserved.