JS realizes the effect of Baidu News navigation bar

JS realizes the effect of Baidu News navigation bar

This article shares the specific code of JS to achieve the effect of Baidu News Navigation Bar for your reference. The specific content is as follows

I have been learning Web front-end recently and used js to simply implement the effect of Baidu News navigation bar. When the mouse moves over an option, a red background block will slide over the current option. When you click on an option, the fixed red background block will move to the current option, meaning that the current option is selected. Without further ado, the code is as follows

Body

<div class="box">
        <!--Two red background blocks-->
        <!--Background block that moves with the mouse-->
        <div id="move"></div>
        <!--The background block fixed somewhere after the mouse clicks-->
        <div id="fixed"></div>
        <a href="#">Home</a>
        <a href="#">Domestic</a>
        <a href="#">International</a>
        Military
        <a href="#">Finance</a>
        <a href="#">Entertainment</a>
        <a href="#">Sports</a>
        <a href="#">Internet</a>
        <a href="#">Technology</a>
        <a href="#">Games</a>
        <a href="#">Woman</a>
        <a href="#">Car</a>
        <a href="#">Property</a>
</div>

CSS part

 *{
            margin: 0;
            padding: 0;
        }
        .box{
            top:100px;
            width: 790px;
            height: 30px;
            font-size: 0;
            position: relative;
            margin: 0 auto;
            background-color: #01204f;
        }
        a{
            display: inline-block;
            position: relative;
            width: 60px;
            height: 30px;
            line-height: 30px;
            color: white;
            font-size: 16px;
            text-decoration: none;
            text-align: center;
            transition: all 0.6s;
        }
        #move{
            position: absolute;
            background-color: red;
            top: 0px;
            left: 0px;
            width: 60px;
            height: 30px;
            transition: all 0.6s;
        }
        #fixed{
            position: absolute;
            background-color: red;
            top: 0px;
            left: 0px;
            width: 60px;
            height: 30px;
        }

js part

window.onload = function () {
      let move = document.getElementById("move");//Sliding background block let fixed = document.getElementById("fixed");//Background block fixed somewhere let aList = document.getElementsByTagName("a");//a tag list let left = move.offsetLeft + "px";//Initial position of sliding background block //Bind all a tags to move in, move out, and click events for (let i = 0; i < aList.length; i++) {
                aList[i].onmouseover = function () {
                    // When the mouse moves into a certain a tag, the sliding background block slides to the position of the current a tag move.style.left = aList[i].offsetLeft + "px";
                }
                aList[i].onmouseout = function () {
                    // When the mouse moves out of label a, the sliding background block returns to the initial position move.style.left = left;
                }
                aList[i].onclick = function () {
                    // After a certain a tag is clicked, the fixed background block moves to the position of the current a tag fixed.style.left = aList[i].offsetLeft + "px";
                    // Update the initial position of the sliding background block to the current position of label a left = aList[i].offsetLeft + "px";
                }
            }
        }

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:
  • JS implements the hover effect of the navigation bar
  • JavaScript implements the color change effect of mouse clicking navigation bar
  • JavaScript to achieve sliding navigation bar effect
  • Fullpage.js fixed navigation bar-implementation of positioning navigation bar
  • JS scrolls to the specified position and the navigation bar is fixed at the top
  • Teach you step by step to write a cool navigation bar js+css implementation
  • js navigation bar click event background change sample code
  • JavaScript NodeTree navigation bar (menu item JSON type/self-made)
  • Native JS realizes transparent gradient effect of MUI navigation bar
  • js realizes the navigation bar effect with slow animation

<<:  Docker primary network port mapping configuration

>>:  Three useful codes to make visitors remember your website

Recommend

Pure CSS to achieve input box placeholder animation and input verification

For more exciting content, please visit https://g...

Several skills you must know when making web pages

1. z-index is invalid in IE6. In CSS, the z-index...

Detailed steps to expand LVM disk in Linux

1. Add a hard disk 2. Check the partition status:...

Detailed description of the function of new in JS

Table of contents 1. Example 2. Create 100 soldie...

How to use fdisk to partition disk in Linux

Commonly used commands for Linux partitions: fdis...

The best explanation of HTTPS

Good morning everyone, I haven’t updated my artic...

HTML table cross-row and cross-column operations (rowspan, colspan)

Generally, the colspan attribute of the <td>...

Installation steps of docker-ce on Raspberry Pi 4b ubuntu19 server

The Raspberry Pi model is 4b, 1G RAM. The system ...

How to build DockerHub yourself

The Docker Hub we used earlier is provided by Doc...

About nginx to implement jira reverse proxy

Summary: Configure nginx reverse proxy jira and i...

Setting up shadowsocks+polipo global proxy in Linux environment

1. Install shadowsocks sudo apt-get install pytho...

MySQL series of experience summary and analysis tutorials on NUll values

Table of contents 1. Test Data 2. The inconvenien...

How to install tomcat8 in docker

1. Install tomcat8 with docker 1. Find the tomcat...

Simple implementation method of two-way data binding in js project

Table of contents Preface Publish-Subscriber Patt...