Previous words
The Collapse plug-in (folding) in the Bootstrap framework is actually our common accordion effect. When a trigger element is clicked, it is displayed or hidden in another collapsible area, and the display state can be reversed when clicked again. The classic scene is the accordion style of multiple folding areas and the style of a single title/content. This article will introduce the Bootstrap accordion effect in detail
Structure
The most critical part of the accordion , that is, each title corresponds to a piece of content. In the Bootstrap framework, these two parts are combined and called a panel. As shown in the effect below, there are three panel panels. When these three panels are combined together, it is a panel. Combining panel-group, which is the structure of the accordion

, is simply a trigger and a folding area
<button>触发器</button><div>折叠区</div>
<div> <div> <div><h4 id="a-标题一-a"><a>标题一</a></h4></div> <div><div>标题一对应的内容</div></div> </div> <div> <div><h4 id="a-标题二-a"><a>标题二</a></h4></div> <div><div>标题二对应的内容</div></div> </div> <div> <div><h4 id="a-标题三-a"><a>标题三</a></h4></div> <div><div>标题三对应的内容</div></div> </div> </div>
Declarative triggering
The trigger accordion can be triggered through the custom data-toggle attribute. The data-toggle value is set to collapse, data-target="#folding area identifier". Next let’s look at a simple example
Step one: Design a panel combination with three folding areas
<div> <div></div> <div></div> <div></div> </div>
Step two: Add content to the panel , each panel includes two parts, the first is the panel title panel-heading, and the title panel-title is added inside. The second part is the panel content, which is the folding area, using the panel-collapse style
<div> <div><h4 id="标题一">标题一</h4></div> <div><div>折叠区内容...</div></div> </div>
Step 3: In order to bundle the title and content area together, you can use the anchor link method , connect the title area and the panel area together
<div> <div> <div><h4 id="a-标题一-a"><a>标题一</a></h4></div> <div><div>折叠区内容...</div></div> </div> <div> <div><h4 id="a-标题二-a"><a>标题二</a></h4></div> <div><div>折叠区内容...</div></div> </div>......</div>
Step 4: Control whether the panel content area is visible. In the Bootstrap framework, if you want the content area to be invisible, you only need to add collapse to the panel-collapse style; if you want the content area to be visible by default, you need to add the style collapse and in
<div> <div><h4 id="a-标题一-a"><a>标题一</a></h4></div> <div><div>折叠区内容...</div></div> </div>
Step 5: Activate the accordion interaction behavior. To complete the interactive behavior, you need to customize two attributes in the title link. One is data-toggle, and its value is collapse; the other is data-target, and its value is the identifier of each panel content area, such as ID. In this example, they are #panel1, #panel2 and #panel3 respectively:
[Note] It is okay not to add data-target="#panel1"
in this case, because it is already there href="#panel1"
, but if the button button is used as the trigger, you must use the data-target="#panel1"
statement
<div> <div><h4 id="a-标题一-a"><a>标题一</a></h4></div> <div><div>折叠区内容...</div></div> </div>
Step 6: Define the data-parent attribute so that when one of the elements is clicked, all folding areas are closed, and then the clicked area is opened (if the clicked area is displayed, it will be closed). This data-parent value matches the identifier of the accordion panel container. For example, this example refers to #myAccordion
<div> <div> <div><h4 id="a-标题一-a"><a>标题一</a></h4></div>…</div> <p>. Through the above 6 steps, the following key points can be summarized</p> <p> ☑ Use the panel-title of the panel as the trigger element, and use the parent element of the panel-body as the folding area; </p> <p> ☑ Use a panel-group to contain multiple panels to achieve an accordion effect; </p> <p> ☑ Each trigger element in the panel must specify the data-parent attribute. The value of the data-parent attribute corresponds to the ID or other style identifier of the panel-group style element; </p> <p> ☑ The trigger element needs to be specified data-toggle, and the value is collapse; </p> <p> ☑ The trigger element needs to specify the data-target attribute. The value of the data-target attribute corresponds to the ID or other style identifier of the parent element of the panel-body; if it is an a element , you can specify the href attribute instead </p> <div class="cnblogs_code"><pre class="brush:php;toolbar:false"><div> <div> <div><h4 id="a-标题一-a"><a>标题一</a></h4></div> <div><div>折叠区内容一</div></div> </div> <div> <div><h4 id="a-标题二-a"><a>标题二</a></h4></div> <div><div>折叠区内容二</div></div> </div> <div> <div><h4 id="a-标题三-a"><a>标题三</a></h4></div> <div><div>折叠区内容三</div></div> </div> </div>
JS trigger
[Keyword]
<span style="color: #000000;">$(element).collapse('show');//显示折叠区域 $(element).collapse('hide');//隐藏折叠区域 $(element).collapse('toggle');//反转折叠区域<br></span>
<button>显示折叠区域</button><button>隐藏折叠区域</button><button>反转折叠区域</button><div> <div> <div><h4 id="a-标题一-a"><a>标题一</a></h4></div> <div><div>折叠区内容一</div></div> </div> <div> <div><h4 id="a-标题二-a"><a>标题二</a></h4></div> <div><div>折叠区内容二</div></div> </div> <div> <div><h4 id="a-标题三-a"><a>标题三</a></h4></div> <div><div>折叠区内容三</div></div> </div> </div><script>$(function(){ $('#btn1').click(function(){ $('.collapse').collapse('show'); }) $('#btn2').click(function(){ $('.collapse').collapse('hide'); }) $('#btn3').click(function(){ $('.collapse').collapse('toggle'); }) })</script>
【Event】
This plug-in supports 4 types of event subscriptions
show.bs.collapse show方法调用之后立即触发该事件 shown.bs.collapse 此事件在collapse已经显示出来(并且同时在 CSS 过渡效果完成)之后被触发 hide.bs.collapse hide方法调用之后立即触发该事件。 hidden.bs.collapse 此事件在collapse被隐藏(并且同时在 CSS 过渡效果完成)之后被触发
<div> <div> <div><h4 id="a-标题一-a"><a>标题一</a></h4></div> <div><div>折叠区内容一</div></div> </div> <div> <div><h4 id="a-标题二-a"><a>标题二</a></h4></div> <div><div>折叠区内容二</div></div> </div> <div> <div><h4 id="a-标题三-a"><a>标题三</a></h4></div> <div><div>折叠区内容三</div></div> </div> </div><script>$(function(){ $("#accordion").on("show.bs.collapse",function(e){var $element = $(e.target).siblings().first().find('a'); $element.html($element.data('html') + '[折叠区已打开]'); }).on("hide.bs.collapse",function(e){var $element = $(e.target).siblings().first().find('a'); $element.html($element.data('html') + '[折叠区已关闭]'); }) }); </script>
The above is the detailed content of Using Bootstrap to achieve accordion effect. For more information, please follow other related articles on the PHP Chinese website!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software