How to implement the starry navigation bar in JS
This time I will show you how to implement the star-filled navigation bar with JS. What are the precautions for implementing the star-filled navigation bar with JS? The following is a practical case, let’s take a look.
Instructions
Share the effect of a starry navigation bar. There is not much code, but the effect is very beautiful. Let’s take a look at the rendering first.Explanation
To achieve this effect, you don’t need a lot of knowledge. You know simple CSS and can use JS to get elements. It is basically enough to be able to bind events.Okay, let’s look at the code directly. The comments have been written in great detail. If you don’t want to see the comments, click here to preview.
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <style> body { background-color: #000; /* 防止出现左右的滚动条 */ overflow: hidden; margin: 0; padding: 0; } .wrapper { width: 100%; height: 100px; } .wrapper .nav { list-style: none; width: 800px; height: 100px; padding: 0; margin: 0 auto; } .wrapper .nav li { width: 25%; height: 50px; float: left; margin-top: 25px; } .wrapper .nav li a { text-decoration: none; color: #fff; text-align: center; line-height: 50px; display: block; font-size: 20px; font-family: "KaiTi"; } /* 闪烁的星星 的基本样式 */ .star { width: 5px; height: 5px; background: #fff; position: absolute; z-index: -1; } /* 闪烁动画,改变透明度 */ @keyframes blink { from { opacity: 0.2; } to { opacity: 1; } } </style> </head> <body> <p class="wrapper"> <ul class="nav"> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >导航1</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >导航2</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >导航3</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >导航4</a></li> </ul> </p> <script> // 定义一个 starrySky 对象 var starrySky = { // 星星的数量 starNum: 100, // 星星的大小,返回一个 2 ~ 12 的随机数 starSize () { return 2 + Math.trunc(Math.random() * 10) }, // 星星的颜色 starColor: "#fff", // 线的颜色,鼠标进入导航区域,星星会连成一条线 lineColor: "#fff", // 线的高度 lineHeight: "3px", // 星星连成线的时间 changeTime: "1s", // 初始化方法,生成需要的星星,并调用需要的方法 init () { var html = ""; // 循环生成星星 for (var i = 0; i < this.starNum; i++) { html += "<p class='star' id='star" + i + "'></p>"; } // 拼接到 元素wrapper 中 document.querySelector(".wrapper").innerHTML += html; // 调用 星星分散 的方法 this.disperse(); // 调用 星星聚合连成线 的方法 this.combine(); }, disperse () { // 这个that 保存的是 starrySky 对象 var that = this; // 获取 元素wrapper 的宽度 var width = document.querySelector('.wrapper').offsetWidth; // 获取 元素wrapper 的高度 var height = document.querySelector('.wrapper').offsetHeight; // 循环,开始在元素wrapper 区域内,生成规定数量的 星星, for (var i = 0; i < that.starNum; i++) { // 星星的 top值,0 ~ 元素wrapper 高度的随机数 var top = Math.trunc(height * Math.random()); // 星星的 left值,0 ~ 元素wrapper 宽度的随机数 var left = Math.trunc(width * Math.random()); // 星星的大小,调用 starrySky对象的starSize()方法 var size = that.starSize(); // 设置分散时每个星星样式 document.querySelector("#star" + i).style.cssText += ` top:${top}px; left:${left}px; width:${size}px; height:${size}px; background:${that.starColor}; opacity:${Math.random()}; border-radius:50%; animation:blink 1s ${Math.random() * 2}s infinite alternate; `; } }, combine () { // 这个that 保存的是 starrySky 对象 var that = this; // 查找导航栏 里所有的 a元素,遍历他们,每个都绑定上 鼠标进入 和 鼠标移出 事件 document.querySelectorAll(".nav li a").forEach(function (item) { item.addEventListener("mouseover", function (e) { // 这里的this 是触发事件的当前节点对象,就是鼠标进入时候的 a元素 // 当前a元素的宽度 / 星星数 = 最后连成线时,星星的宽度 var width = this.offsetWidth / that.starNum; // 遍历,为每个星星修改样式,开始连成线 for (var i = 0; i < that.starNum; i++) { // 星星的top 值就是,当前a元素的距离顶部的值 + 当前a元素的高度 var top = this.offsetTop + this.offsetHeight; // 星星的left 值就是,当前a元素的距离左边界的值 + 第i个星星 * 星星的宽度 var left = this.offsetLeft + i * width // 设置每个星星连成线时的样式 document.querySelector("#star" + i).style.cssText += ` width:${width}px; top:${top}px; left:${left}px; height:${that.lineHeight}; background:${that.lineColor}; opacity:1; border-radius:0; transition:${that.changeTime}; animation:blink 1s infinite alternate; `; } }); // 鼠标移出 调用 星星分散 的方法 item.addEventListener("mouseout", function () { that.disperse(); }); } ); }, } // 调用 starrySky对象的 init方法,实现满天星效果 starrySky.init(); </script> </body> </html>
Summary
To achieve this effect, we need to make a starrySky object and define some necessary attributes, mainly relying on the two methods disperse() and combine(). When the stars need to be dispersed, call disperse(), and when the stars need to be connected in a line, call combine (), okay that’s it. I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:How to update the array with $set in vue.js
How to move the array in vue.js Position and update the view
The above is the detailed content of How to implement the starry navigation bar in JS. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

Implementing exact division operations in Golang is a common need, especially in scenarios involving financial calculations or other scenarios that require high-precision calculations. Golang's built-in division operator "/" is calculated for floating point numbers, and sometimes there is a problem of precision loss. In order to solve this problem, we can use third-party libraries or custom functions to implement exact division operations. A common approach is to use the Rat type from the math/big package, which provides a representation of fractions and can be used to implement exact division operations.

I'm really sorry that I can't provide real-time programming guidance, but I can provide you with a code example to give you a better understanding of how to use PHP to implement SaaS. The following is an article within 1,500 words, titled "Using PHP to implement SaaS: A comprehensive analysis." In today's information age, SaaS (Software as a Service) has become the mainstream way for enterprises and individuals to use software. It provides a more flexible and convenient way to access software. With SaaS, users don’t need to be on-premises

Title: Detailed explanation of data export function using Golang. With the improvement of informatization, many enterprises and organizations need to export data stored in databases into different formats for data analysis, report generation and other purposes. This article will introduce how to use the Golang programming language to implement the data export function, including detailed steps to connect to the database, query data, and export data to files, and provide specific code examples. To connect to the database first, we need to use the database driver provided in Golang, such as da
