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

Linux file management command example analysis [display, view, statistics, etc.]

This article describes the Linux file management ...

Solution to the problem of null column in NOT IN filling pit in MySQL

Some time ago, when I was working on a small func...

Detailed process of upgrading gcc (version 10.2.0) under CentOS7 environment

Table of contents Short Introduction 1. Check the...

Detailed steps to install JDK and Tomcat in Linux environment

Table of contents 1. Install JDK Manual Installat...

HTML4.0 element default style arrangement

Copy code The code is as follows: html, address, ...

Pitfalls based on MySQL default sorting rules

The default varchar type in MySQL is case insensi...

Linux Basic Tutorial: Special Permissions SUID, SGID and SBIT

Preface For file or directory permissions in Linu...

Centos7 installation of FFmpeg audio/video tool simple document

ffmpeg is a very powerful audio and video process...

Detailed explanation of the concept, principle and usage of MySQL triggers

This article uses examples to explain the concept...

Complete steps to build NFS file sharing storage service in CentOS 7

Preface NFS (Network File System) means network f...

30 excellent examples of color matching in web design

Today, this article has collected 30 excellent cas...

MySQL MyISAM default storage engine implementation principle

By default, the MyISAM table will generate three ...

How to enable slow query log in MySQL

1.1 Introduction By enabling the slow query log, ...