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
html: In fact, it is to arrange several solid cir...
Table of contents 1. Ubuntu source change 2. Inst...
Table of contents jQuery's $.ajax The beginni...
1. Environmental requirements 1. Docker 17 and ab...
1. Getting started with setUp Briefly introduce t...
Today, I set up a newly purchased Alibaba Cloud E...
Table of contents 1. Lock and Latch 2. Repeatable...
Table of contents Overview 1. Clearly understand ...
Sometimes we need to import some data from anothe...
Recently, when I was working on a project, I was ...
Official Website https://cli.vuejs.org/en/guide/ ...
Table of contents Preface Publish-Subscriber Patt...
Environment Preparation Before starting any opera...
mycli MyCLI is a command line interface for MySQL...
Table of contents 1. Merge interface 1.1 Non-func...