JavaScript滿天星導(dǎo)航欄實(shí)現(xiàn)方法
說(shuō)明
分享一個(gè)滿天星導(dǎo)航欄的效果,代碼不多,但效果挺好看,先看看效果圖吧。
解釋
實(shí)現(xiàn)這個(gè)效果,需要掌握的知識(shí)不用很多,知道簡(jiǎn)單的CSS,會(huì)用JS 獲取元素, 能綁定事件基本就足夠了。
好的,我們直接來(lái)看代碼,注釋已經(jīng)寫的很詳細(xì)了,不想看有注釋的,點(diǎn)這里點(diǎn)擊預(yù)覽。
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <style> body { background-color: #000; /* 防止出現(xiàn)左右的滾動(dòng)條 */ 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; } /* 閃爍動(dòng)畫,改變透明度 */ @keyframes blink { from { opacity: 0.2; } to { opacity: 1; } } </style> </head> <body> <div class="wrapper"> <ul class="nav"> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >導(dǎo)航1</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >導(dǎo)航2</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >導(dǎo)航3</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >導(dǎo)航4</a></li> </ul> </div> <script> // 定義一個(gè) starrySky 對(duì)象 var starrySky = { // 星星的數(shù)量 starNum: 100, // 星星的大小,返回一個(gè) 2 ~ 12 的隨機(jī)數(shù) starSize () { return 2 + Math.trunc(Math.random() * 10) }, // 星星的顏色 starColor: "#fff", // 線的顏色,鼠標(biāo)進(jìn)入導(dǎo)航區(qū)域,星星會(huì)連成一條線 lineColor: "#fff", // 線的高度 lineHeight: "3px", // 星星連成線的時(shí)間 changeTime: "1s", // 初始化方法,生成需要的星星,并調(diào)用需要的方法 init () { var html = ""; // 循環(huán)生成星星 for (var i = 0; i < this.starNum; i++) { html += "<div class='star' id='star" + i + "'></div>"; } // 拼接到 元素wrapper 中 document.querySelector(".wrapper").innerHTML += html; // 調(diào)用 星星分散 的方法 this.disperse(); // 調(diào)用 星星聚合連成線 的方法 this.combine(); }, disperse () { // 這個(gè)that 保存的是 starrySky 對(duì)象 var that = this; // 獲取 元素wrapper 的寬度 var width = document.querySelector('.wrapper').offsetWidth; // 獲取 元素wrapper 的高度 var height = document.querySelector('.wrapper').offsetHeight; // 循環(huán),開(kāi)始在元素wrapper 區(qū)域內(nèi),生成規(guī)定數(shù)量的 星星, for (var i = 0; i < that.starNum; i++) { // 星星的 top值,0 ~ 元素wrapper 高度的隨機(jī)數(shù) var top = Math.trunc(height * Math.random()); // 星星的 left值,0 ~ 元素wrapper 寬度的隨機(jī)數(shù) var left = Math.trunc(width * Math.random()); // 星星的大小,調(diào)用 starrySky對(duì)象的starSize()方法 var size = that.starSize(); // 設(shè)置分散時(shí)每個(gè)星星樣式 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 () { // 這個(gè)that 保存的是 starrySky 對(duì)象 var that = this; // 查找導(dǎo)航欄 里所有的 a元素,遍歷他們,每個(gè)都綁定上 鼠標(biāo)進(jìn)入 和 鼠標(biāo)移出 事件 document.querySelectorAll(".nav li a").forEach(function (item) { item.addEventListener("mouseover", function (e) { // 這里的this 是觸發(fā)事件的當(dāng)前節(jié)點(diǎn)對(duì)象,就是鼠標(biāo)進(jìn)入時(shí)候的 a元素 // 當(dāng)前a元素的寬度 / 星星數(shù) = 最后連成線時(shí),星星的寬度 var width = this.offsetWidth / that.starNum; // 遍歷,為每個(gè)星星修改樣式,開(kāi)始連成線 for (var i = 0; i < that.starNum; i++) { // 星星的top 值就是,當(dāng)前a元素的距離頂部的值 + 當(dāng)前a元素的高度 var top = this.offsetTop + this.offsetHeight; // 星星的left 值就是,當(dāng)前a元素的距離左邊界的值 + 第i個(gè)星星 * 星星的寬度 var left = this.offsetLeft + i * width // 設(shè)置每個(gè)星星連成線時(shí)的樣式 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; `; } }); // 鼠標(biāo)移出 調(diào)用 星星分散 的方法 item.addEventListener("mouseout", function () { that.disperse(); }); } ); }, } // 調(diào)用 starrySky對(duì)象的 init方法,實(shí)現(xiàn)滿天星效果 starrySky.init(); </script> </body> </html>
注意:如果需要修改樣式,不要把 nav元素 和 nav 里面的 li元素,給定位了,因?yàn)樽詈缶€的位置是根據(jù) a元素的 offsetHeight 和 offsetLeft 定位的,如果 nav元素 和 nav 里面的 li元素 定位了,會(huì)改變 a元素的offsetParent元素,位置就不對(duì)了。
對(duì)offsetHeight、offsetLeft 和 offsetParent 不理解的點(diǎn)這里:https://codepen.io/FEWY/pen/MQdoWX
總結(jié)
實(shí)現(xiàn)這個(gè)效果,就是做了一個(gè) starrySky對(duì)象,定義好一些必須的屬性,主要靠 disperse() 和 combine() 兩個(gè)方法,需要星星分散的時(shí)候調(diào)用disperse(),需要星星連成線的時(shí)候調(diào)用combine(),好的就這樣了。
相關(guān)文章
萬(wàn)字詳解JavaScript手寫一個(gè)Promise
這篇文章主要介紹了萬(wàn)字詳解JavaScript手寫一個(gè)Promise,Promise就是一個(gè)類,在執(zhí)行這個(gè)類的時(shí)候,需要傳遞一個(gè)執(zhí)行器(回調(diào)函數(shù))進(jìn)去,執(zhí)行器會(huì)立即執(zhí)行2022-07-07autojs 螞蟻森林能量自動(dòng)拾取即給指定好友澆水的實(shí)現(xiàn)方法
這篇文章主要介紹了autojs 螞蟻森林能量自動(dòng)拾取即給指定好友澆水的實(shí)現(xiàn)方法,本文通過(guò)圖文并茂實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05JS實(shí)現(xiàn)的隨機(jī)排序功能算法示例
這篇文章主要介紹了JS實(shí)現(xiàn)的隨機(jī)排序功能算法,結(jié)合具體實(shí)例形式分析了javascript常用的排序算法實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06JavaScript學(xué)習(xí)點(diǎn)滴 call、apply的區(qū)別
對(duì)于apply和call兩者在作用上是相同的,但兩者在參數(shù)上有區(qū)別的。2010-10-10js綁定事件this指向發(fā)生改變的問(wèn)題解決方法
js綁定事件this指向發(fā)生改變的問(wèn)題將在本文進(jìn)行詳細(xì)探討下,感興趣的朋友可以參考下哈,希望對(duì)你有所幫助2013-04-04