This article example shares the specific code of JavaScript to achieve the secondary menu effect for your reference. The specific content is as follows The effect diagram of this implementation is as follows: The effect of this secondary menu is: When you click on a box, the contents of the opened box will be retracted and the contents of the box currently clicked will be expanded. So how do we achieve this effect? We can step by step 1. First, we need to display the entire frame , that is, the appearance of all boxes expanded, because its display/non-display is done with 2. Then start writing the js part: make sure that when a box is clicked, it will change from closed to expanded. Our box is span. If we want to display all the contents of span's parent div, we first need to get all spans through 3. Then we need to collapse the expanded box when clicking span to expand a box. At this time, we need a variable now to record the box that was clicked last time . Set its initial value to null. If now is not equal to the currently clicked span, get the parent box of now, set its height to the height of the span, and then expand the currently clicked span (the second point has already explained how to expand); if now is equal to the currently clicked span, get the height of its parent box. If it is equal to the height of the span, set its height to the value of 4. Set a timer and call it when you are ready to increase or decrease the height of the parent box. When the height of the parent box reaches the height of the span or reaches The code is as follows: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0; padding: 0; } .container { width: 300px; height: 700px; margin: 100px auto; } .container div { height: 43px; overflow: hidden; } .container div span { width: 150px; height: 40px; line-height: 40px; border-radius: 15px; display: block; text-align: center; background-color: rgba(104, 250, 201, 0.849); cursor: pointer; } a { width: 150px; height: 40px; line-height: 40px; text-decoration: none; display: block; text-align: center; } </style> </head> <body> <div class="container"> <div id=""> <span id="one">Monday</span> Visualization Visualization </div> <div id=""> Tuesday <a href="">Algorithm Design</a> <a href="">Graphics</a> <a href="">Design Group Course Design</a> Operating System </div> <div id=""> <span id="three">Thursday</span> <a href="">Situation and Policy</a> Operating System </div> <div id=""> <span id="four">Friday</span> <a href="">Algorithm Design</a> </div> </div> <script> let menu = document.getElementsByTagName("span"); let now = null; for (let i = 0; i < menu.length; i++) { menu[i].onclick = function () { let par = menu[i].parentNode; if (now === i) { if (par.style.height === "43px") { open(par); } else { close(par); } } else { if (now !== null) { close(menu[now].parentNode); } open(par); now = i; } } } function open(par) { let time = setInterval(() => { let num = par.offsetHeight; if (num >= par.scrollHeight) { clearInterval(tem); } par.style.height = num + 1 + "px"; }, 7); } function close(par) { let time = setInterval(() => { let num = par.offsetHeight; if (num <= 43) { clearInterval(tem); return; } par.style.height = num - 1 + "px"; }, 7); } </script> </body> </html> The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: CSS: visited pseudo-class selector secret memories
>>: Steps to build a Docker image using Dockerfile
Table of contents Comprehensive comparison From t...
0. System requirements CPU I5-10400F or above Mem...
After the form input box input is set to the disa...
Solution function mergeImgs(list) { const imgDom ...
The download address of FlashFXP is: https://www....
Recently, when I was learning how to use webpack,...
Table of contents 1. Email 2. Mobile phone number...
When the existing video player cannot meet the ne...
I am going to review Java these two days, so I wr...
The main text starts below. 123WORDPRESS.COM Down...
1. mysqlbinlog: [ERROR] unknown variable 'def...
1. Introduction to TypeScript The previous articl...
Table of contents 1. Overview 2. Use docker to de...
I'm currently working on my own small program...
XHTML is the basis of CSS layout. jb51.net has al...