简体   繁体   English

如何使手风琴(CSS + javascript)工作?

[英]How to make Accordion(CSS+javascript) work?

I have this code so far: 到目前为止,我有以下代码:

 var acc; acc = document.getElementByClassName('button'); acc.onclick = function() { this.classList.toggle("active"); this.nextElementSibling.classList.toggle("show"); } 
 div.panel { padding: 0 18px; background-color: white; max-height: 0; overflow: hidden; transition: 0.6s ease-in-out; opacity: 0; } div.panel.show { opacity: 1; max-height: 500px; } .button{ display:block; height:431px; width:100%; font-family: Futura, 'Trebuchet MS', Arial, sans-serif; color:black; font-size:80px; margin:0 auto; background-color:transparent; border-style:none; } .button:hover, .button.active{ background: url('pcluj.jpg') ; background-size: cover; } 
 <center><button class="button" id="button">CLUJ</button></center> <div class="panel"> <p>Lorem ipsum...</p> </div> 

Right now it doesn't work and I don't know why!! 现在它不起作用,我也不知道为什么! The div doesn't show up when I press the first button. 当我按第一个按钮时,div没有显示。 What is the solution? 解决办法是什么? Please help! 请帮忙!

It looked like my post was only code so I had to write this in order to be able to post ........... 看来我的帖子只是代码,所以我不得不写这篇文章才能发布........

javascript: javascript:

  window.onload = function(){
    var acc;
    acc = document.getElementById('button');

    acc.onclick = function()
    {
        this.classList.toggle("active");
        this.nextElementSibling.classList.toggle("show");
    }
}

html: HTML:
<button class="button" id="button">CLUJ</button> <div class="panel"> <p>Lorem ipsum...</p> </div>

One way to achieve it if you are only supporting Chrome and Safari or Mobile Devices is to use the HTML5 details element and style it with CSS. 如果仅支持Chrome和Safari或移动设备,则实现此目标的一种方法是使用HTML5 details元素并使用CSS设置其样式。 You won't even need JavaScript. 您甚至不需要JavaScript。

Read on: http://dipaksblogonline.blogspot.in/2014/09/html5-details-element.html 继续阅读: http : //dipaksblogonline.blogspot.in/2014/09/html5-details-element.html

Basic Demo: http://jsfiddle.net/8mthoj5g/1/ 基本演示: http : //jsfiddle.net/8mthoj5g/1/

<details open>
    <summary>Click me to toggle more information</summary>
    <div>The details element is used as a widget to show/hide additional information.</div>    
</details>

Another way is to use the onclick as attribute on the button - it will work - 另一种方法是在按钮上使用onclick作为属性-它将起作用-

<button class="button" id="button1" onclick="someFunc()">CLUJ</button>

Demo: https://jsfiddle.net/Lp05m27p/ 演示: https : //jsfiddle.net/Lp05m27p/

Jquery jQuery的

$("#button").click(function(){
  var divPnl = $('.panel');
  $(this).toggleClass("active");
  divPnl.toggleClass("show");
});

fiddle link 小提琴链接

Ideally you would use an established method or existing library for an accordion such as an Unordered List but what you've got already can be made to work with a few minor changes: 理想情况下,您可以使用已建立的方法或现有库来处理手风琴,例如“无序列表”,但只需稍作改动,就可以使用已有的方法:

Firstly, use querySelectorAll to select the .button element. 首先,使用querySelectorAll选择.button元素。 This returns a NodeList . 这将返回一个NodeList

Iterate through the NodeList using a basic for-loop and add an event listener to each item which calls a method to toggle the .button 's active class. 使用基本的for-loop循环遍历NodeList,并向每个项目添加一个事件侦听器,该事件侦听器调用一种方法来切换.buttonactive类。

Rather than find the children of the active .button we simply use CSS2's Adjacent Sibling Selector to select the .button s next sibling .panel 与其查找活动的.button的子代,我们仅使用CSS2的相邻兄弟选择器来选择.button的下一个兄弟.panel

EG: 例如:

 // Get the .buttons var buttons = document.querySelectorAll('.button'); // For each button for (i = 0; i < buttons.length; ++i) { //add an event listener buttons[i].addEventListener("click", onButtonClick, false); } // On button click... function onButtonClick(){ this.classList.toggle("active"); } 
 div.panel { padding: 0 18px; background-color: white; max-height: 0; overflow: hidden; transition: 0.6s ease-in-out; opacity: 0; } /* div.panel.show { opacity: 1; max-height: 500px; } */ .button{ display:block; height:50px; width:100%; font-family: Futura, 'Trebuchet MS', Arial, sans-serif; color:black; font-size:20px; margin:0 auto; background-color:transparent; border-style:none; outline:none; } .button:hover, .button.active{ background:#eee url('http://lorempixel.com/100/20/') ; background-size: cover; } #button1:hover, #button1.active{ background-image:url('http://lorempixel.com/100/20/'); } #button2:hover, #button2.active{ background-image:url('http://lorempixel.com/200/20/'); } /* adjacent sibling selector to select a .panel next to an button.active: */ .button.active + .panel { opacity: 1; max-height: 500px; } 
 <button class="button" id="button1">ONE</button> <div class="panel"> <p>Lorem ipsum one...</p> </div> <button class="button" id="button2">TWO</button> <div class="panel"> <p>Lorem ipsum two...</p> </div> 

You have several errors in your Javascript code. 您的Javascript代码中有几个错误。 You are trying to assign a click event handler to an element before it actually exists in the DOM. 您试图在元素中实际存在元素之前为其分配click事件处理程序。 You are also referring to the div that you are trying to show incorrectly. 您还指的是试图显示不正确的div。 See below a working JavaScript code with comments and your mistakes fixed. 请参见下面的有效JavaScript代码,其中包含注释和已修复的错误。

// You need to make sure that the button element exists before trying to attach a click handler to it.
// You can do that by running the function when the onload event is triggered.
window.onload = function () {
    var acc;
    acc = document.getElementsByClassName('button')[0]; 
    // You have errors in the above line, "s" is missing in getElement/s/ByClassName, also that method returns an array, not an element, so you need to add [0] at the end.
    acc.onclick = function()
    {
        this.classList.toggle("active");

        // this.nextElementSibling.classList.toggle("show");
        // This does not work because <button> is in <center> and the panel div is therefore not its sibling.

        // there are many ways to reference the panel div, eg:
        var panel= document.getElementsByClassName("panel")[0];
        panel.classList.toggle("show");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM