JavaScript to show and hide the drop-down menu

JavaScript to show and hide the drop-down menu

This article shares the specific code for JavaScript to display and hide the drop-down menu for your reference. The specific content is as follows

Sometimes this page effect is needed:

When the mouse moves over an element, a drop-down menu is implemented. When the mouse moves away from the element, the drop-down menu disappears.

Implementation ideas

1. A box contains two parts, the lower part is the submenu, which is set to be hidden first: display: none;
2. When the mouse moves to the box: trigger event - - -onmouseover, js sets the display value of the submenu below to - - -block, making the submenu appear
3. Move the mouse away from the box: trigger the event - - -onmouseout, js sets the display value of the submenu below to - - -none, making the submenu hidden again
4. Change the font color, background color and other styles according to your needs

Code Sample

<!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>Operation element-Sina drop-down menu</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        ul li {
            list-style: none;
        }
        
        a {
            text-decoration: none;
            color: #4c4c4c;
        }
        
        a:hover {
            color: #e88415;
        }
        
        .box {
            width: 80px;
            margin: 50px auto;
            font-size: 14px;
            color: #4c4c4c;
        }
        
        .weibo {
            position: relative;
            background-color: #fcfcfc;
        }
        
        .weibo a {
            display: block;
            height: 40px;
            line-height: 40px;
            padding-left: 20px;
        }
        
        .change {
            color: #f9a74f;
            background-color: #edeef0;
        }
        
        i {
            position: absolute;
            top: 50%;
            right: 15px;
            margin-top: -4px;
            width: 5px;
            height: 5px;
            border-bottom: 1px solid orangered;
            border-right: 1px solid orangered;
            transform: rotate(45deg);
        }
        
        .weiboList {
            display: none;
        }
        
        .weiboList li a {
            display: block;
            width: 80px;
            height: 33px;
            line-height: 33px;
            padding-left: 15px;
            border-bottom: 1px solid #fecc5b;
            background-color: #fff;
        }
        
        .weiboList li a:hover {
            background-color: #fff5da;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="weibo"><a href="#" >Weibo<i class="select"></i></a></div>
        <ul class="weiboList">
            <li><a href="#" >Private Message</a></li>
            <li><a href="#" >Comments</a></li>
            <li><a href="#" >@Me</a></li>
        </ul>
    </div>

    <script>
        var box = document.querySelector('.box');
        var weibo = document.querySelector('.weibo');
        var weiboList = document.querySelector('.weiboList');

        box.onmouseover = function() {
            weibo.className = 'weibo change'
            weiboList.style.display = 'block';

        }

        box.onmouseout = function() {
            weibo.className = 'weibo';
            weiboList.style.display = 'none';
        }
    </script>
</body>

</html>

Page effect:

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:
  • JavaScript to show and hide the drop-down menu
  • Vue.js Example code for showing and hiding the left secondary menu
  • A simple example of showing or hiding the effect of js menu click

<<:  VMware virtual machine installation Apple Mac OS super detailed tutorial

>>:  A simple ID generation strategy: Implementation of generating globally unique ID from MySQL table

Recommend

How to view version information in Linux

How to view version information under Linux, incl...

Use of Linux xargs command

1. Function: xargs can convert the data separated...

HTML+CSS to achieve charging water drop fusion special effects code

Table of contents Preface: accomplish: Summarize:...

Example code for implementing anti-shake in Vue

Anti-shake: Prevent repeated clicks from triggeri...

MySQL table name case selection

Table of contents 1. Parameters that determine ca...

Summary of Mysql exists usage

Introduction EXISTS is used to check whether a su...

Understanding of CSS selector weight (personal test)

Copy code The code is as follows: <style type=...

JavaScript implements password box input verification

Sometimes it is necessary to perform simple verif...

Practical method of deleting associated tables in MySQL

In the MySQL database, after tables are associate...

Introduction to nesting rules of html tags

There are many XHTML tags: div, ul, li, dl, dt, d...

Talking about ContentType(s) from image/x-png

This also caused the inability to upload png files...

HTML dl, dt, dd tags to create a table vs. Table creation table

Not only does it reduce the cost of website develo...

What does this.parentNode.parentNode (parent node of parent node) mean?

The parent node of the parent node, for example, t...