Creating and configuring a basic accordion is simple, but you should be sure to read the entire article carefully as the more advanced options can be somewhat complex.
Basics
Creating a new accordion
To create a new accordion, you need to select some pairs of elements - containing a title and content. Therefore, first of all, you need to assign a css class name to each title and each content block:
Reference code:
Toggle 1
Here is the content of toggle 1 p>
Toggle 2
Here is the content of toggle 2
Now, we select all elements with the css class name "togglers" and all elements with the css class name "elements", assign them to variables, and then initialize an accordion object.
Reference code:
var toggles = $$( '.togglers');
var content = $$('.elements');
// Create your object variables
// Use "new" to create a new accordion object
/ / Set the switch (toogle) array
// Set the content array
var AccordionObject = new Accordion(toggles, content);
The default settings of the accordion may give you this effect :
Toggle 1
Here is the content of toggle 1
Toggle 2
Here is the content of toggle 2
Toggle 3
Here is the content of toggle 3
Options
Of course, if you want something other than the accordion's default effect, you'll need to tweak the options. Here we will explain them one by one.
display
Defaults to 0
This option determines which element will be displayed when the page loads. The default value is 0, so the first element is displayed. If you want to set it to another element, you only need to set it to the index value of another element (an integer). Unlike "show", "display" will use a gradient animation to expand the element.
Reference code:
var AccordionObject = new Accordion( toggles, content {
display: 0 // Default is 0
});
show
Default is 0
is very similar to "display", "show" Determines which element will expand when the page loads, but it does not have a gradient animation. It is only displayed after the page loads without using any gradient animation.
Reference code:
var AccordionObject = new Accordion( toggles, content {
display: 0 // Default is 0
});
height
Default is true
When set to true, the content will be displayed. There is a high gradient animation effect. This is the same as you saw above, a standard accordion setup.
Reference code:
var AccordionObject = new Accordion( toggles, content {
height: true // Default is true
});
width
Default is false
Similar to "height", but instead of using height Gradient animation to display content, instead use width gradient animation to display content. If you use the "width" option along with the other standard settings we've seen, the distance between individual title switches will remain the same, based solely on the height of the content. The content div will gradually become wider from left to right to display the content.
Reference code:
var AccordionObject = new Accordion( toggles, content {
width: false // Defaults to false
});
opacity
Defaults to true
This option sets when you hide or show content , whether to use the opacity gradient effect. Since we used the default settings above, you can see the effect.
Reference code:
var AccordionObject = new Accordion(toggles, content {
opacity: true // Default is true
});
fixedHeight
Default is false
To set a fixed height, you can set an integer here (for example, you can set 100, which will make the height of the content 100px). If the height you plan to set is less than the height of the content, you need to set the overflow attribute of the content in CSS. As you might expect, when you use the "show" option, it is not registered when the page first loads.
Reference code:
var AccordionObject = new Accordion( toggles, content {
fixedHeight: false // Default is false
});
fixedWidth
Default is false
Similar to "fixedHeight" above, if you This option is given an integer, which will set its width.
Reference code:
var AccordionObject = new Accordion( toggles, content {
fixedWidth: false // Default is false
});
alwaysHide
Default is false
This option allows you to add a toggle to the title . By setting this option to true, when you click on a header whose content has expanded, it will close the content block, but will not expand any elements. You can see it right away in the example below.
Reference code:
var AccordionObject = new Accordion( toggles, content {
alwaysHide: false // Default is false
});
Event
onActive
This event is triggered when you toggle an element. He will pass the switch control element and content element, as well as the switch state as parameters.
Reference code:
var AccordionObject = new Accordion( toggles, content {
onActive: function(toggler, element) {
toggler.highlight('#76C83D'); // Green
element.highlight('#76C83D');
}
});
onBackground
This event is triggered when the ige element starts to hide, it will pass all other regular closed elements as parameters, not the expanded elements.
Reference code:
var AccordionObject = new Accordion( toggles, content {
onBackground: function(toggler, element) {
toggler.highlight('#DC4F4D'); // Red
element.highlight('#DC4F4D');
}
});
onComplete
This is a standard onComplete event. It is passed a variable containing the content element. Here is another better way to get these things, if anyone knows, please make a note.
Reference code:
var AccordionObject = new Accordion( toggles, content {
onComplete: function(one, two, three, four){
one.highlight('#5D80C8'); // blue
two.highlight('#5D80C8');
three.highlight('#5D80C8');
four.highlight('#5D80C8');
}
});
Methods
.addSection();
With this method you can add a section (a title/content element pair) in the middle. This is the same as many other methods we've seen. First, we reference an accordion object, followed by .addSection, then you can call the id of the title, the id of the content, and finally specify a position for it - the position where this new element will appear (0 is the first position) .
Reference code: [Copy code] [Save code]
AccordionObject.addSection('togglersID', 'elementsID', 2);
Note: When you add a section this way, although it will The index value 2 is displayed, but its real index should be the last index value plus 1. If you have 5 items in an array and you add a sixth, its index will be 5, regardless of where you added it via the .addSection(); method.
.display();
This method allows you to expand a specified element. You select the element by its index (if you add a new pair of elements and you want to expand them, you need to use a new index).
Reference code:
AccordionObject.display(5) ; // This will display your newly added element
Example
Here we have a fully functional accordion, using all the events and methods we saw above, and much more options. Carefully read the code below and the related comments within the code to see how they are used.
Reference code:
// Assign the toggle elements and content elements to two variables
var toggles = $$('.togglers');
var content = $$('.elements');
// Create an object variable
// Use new to create a new accordion object
// Set the switch array
// Set the content array
// Set related options and events
var AccordionObject = new Accordion(toggles, content, {
// When the page loads
// This will show the content element (the element corresponding to the index)
// without any gradient animation, Just expand
// Also note: if you use "fixedHeight",
// the show option will not work when the page first loads
// the "show" option will override "display" option
show: 0,
// When the page loads
// This will display (display) the content element (the element corresponding to the index)
// When the content is expanded there will be Gradient animation
// If the display option and show option are set at the same time
// The show option will override the display option
// display: 0,
// The default is true
// This A vertical accordion will be created
height : true,
// This is the horizontal accordion parameter used
// This will be more complicated due to the involvement of CSS
// We will discuss it later Let’s talk about it again?
width: false,
// Default is true
// This will give the content an opacity gradient effect
opacity: true,
// Default It is false, or it can be an integer -
// The height of all content blocks will be fixed
// You need to set the overflow rule in css
// If "show" is used, the first page on the page It will not take effect when loading for the first time
fixedHeight: false,
// It can be false or an integer
// Similar to the fixedHeight above,
// But this is a setting for a horizontal accordion
fixedWidth: false,
// Allows you to toggle an expanded item
alwaysHide: true,
// Standard onComplete event
// Pass a variable for each content block element
onComplete: function(one, two, three, four, five){
one.highlight('#5D80C8'); // blue
two.highlight('#5D80C8');
three.highlight('#5D80C8');
four.highlight('#5D80C8');
five.highlight('#5D80C8'); // This is the added section
$('complete ').highlight('#5D80C8');
},
// This event will be triggered when you switch an element
// will pass the switch element being opened
// and Content element
onActive: function(toggler, element) {
toggler.highlight('#76C83D'); // Green
element.highlight('#76C83D');
$('active ').highlight('#76C83D');
},
// This event will be triggered when an element starts to hide
// Will pass all closing elements
// or Unexpanded element
onBackground: function(toggler, element) {
toggler.highlight('#DC4F4D'); // Red
element.highlight('#DC4F4D');
$( 'background').highlight('#DC4F4D');
}
});
$('add_section').addEvent('click', function(){
// New addition The sections are in pairs
// (including the id of the switch and the id of the related content)
// followed by the index of where they are to be placed
AccordionObject.addSection('togglersID', 'elementsID ', 0);
});
$('display_section').addEvent('click', function(){
// Will determine which element is displayed when the page is first loaded
// Will override the display option settings
AccordionObject.display(4);
});
Here you can see when the event was triggered.
onCompleteonActivonBackground
You can try adding a pair of content using the button below.
Toggle 1
Here is the content of toggle 1 Here is the content of toggle 1 Here is the content of toggle 1 Here is the content of toggle 1 Here is the content of toggle 1 Here is the content of toggle 1 Here is the content of toggle 1
Toggle 2
Here is the content of toggle 2
Toggle 3
Here is the content of toggle 3
Toggle 4
Here is the content of toggle 4
Toggle Add
Here is the content of toggle 4
Things to note
.display can identify the index, but If you use the addSection method, then this section will use the last index
If you use the "fixedHeight" option, it will not have any effect under the "show" option, but it will have an effect under the "display" option
More accordion options, events and methods
The Accordion class inherits from the Fx.Element class, which in turn inherits from the Fx class. You can use various options of these classes to optimize your accordion. Although it may seem like a simple thing, the accordion is a very useful and powerful plug-in. I'd love to see any effects someone makes with this.
Learn more
Refer to the accordion section in the documentation, as well as Fx.Elements and FxThese two sections. You can also take a look at the use of accordion in the official demo of MooTools .
Download a zip package with everything you need to get started
Includes MooTools 1.2's core library and extension (more) libraries, the example above, an external JavaScript file, a simple HTML page and a CSS file.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

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.


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

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)