原生js實(shí)現(xiàn)波浪導(dǎo)航效果
本文實(shí)例為大家分享了原生js實(shí)現(xiàn)波浪導(dǎo)航效果的具體代碼,供大家參考,具體內(nèi)容如下
展示效果:
源碼展示:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>js波浪導(dǎo)航</title> <style> ?* {undefined ? ? margin:0; ? ? padding:0; ? ? font-family:"微軟雅黑"; } body {undefined ? ? width:100vw; } .wave-ul {undefined ? ? list-style:none; } .wave-a {undefined ? ? text-decoration:none; ? ? display:block; } .wave-span {undefined ? ? background:#f69; ? ? color:#fff; ? ? width:20px; ? ? display:block; } .waveli-in-right {undefined ? ? float:left; } .waveli-in-left {undefined ? ? float:right; } .wavenav-right {undefined ? ? left:100%; ? ? margin-left:-20px; } .wavenav-left {undefined ? ? left:0%; ? ? margin-left:-130px; } .wave-li {undefined ? ? margin:1px; ? ? display:block; ? ? background:#ccc; ? ? width:150px; ? ? overflow:hidden; } .wave-nav {undefined ? ? position:fixed; } </style> </head> <body> <div id="test"><div></div></div> <script> var wavenav = function(json, dir, top) {undefined ? ? this.json = json; ? ? this.dir = dir; ? ? this.top = top; } wavenav.prototype = {undefined ? ? constructor: wavenav, ? ? createHTML: function() {undefined ? ? ? ? var json = this.json; ? ? ? ? var htmlstr = '<nav class="wave-nav"><ul class="wave-ul">'; ? ? ? ? for (var key in json) {undefined ? ? ? ? ? ? htmlstr += '<li class="wave-li"><span class="wave-span">' + key + ? ? ? ? ? ? ? ? '</span><a href="' + json[key][1] + ? ? ? ? ? ? ? ? '" class="wave-a">' + json[key][0] + '</a></li>'; ? ? ? ? } ? ? ? ? htmlstr += '</ul></nav>' ? ? ? ? return htmlstr; ? ? }, ? ? renderCSS: function() {undefined ? ? ? ? var dir = this.dir; ? ? ? ? var top = this.top; ? ? ? ? var oNavLenght = document.getElementsByClassName('wave-nav').length; ? ? ? ? var oLastNav = document.getElementsByClassName('wave-nav')[oNavLenght - 1]; ? ? ? ? oLastNav.style.top = top; ? ? ? ? var oLastUl = oLastNav.children[0]; ? ? ? ? var oLi = oLastUl.children; ? ? ? ? switch (dir) { ? ? ? ? ? ? ? case 'LEFT': ? ? ? ? ? ? ? ? addClassName('wavenav-left', 'waveli-in-left'); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? addClassName('wavenav-right', 'waveli-in-right'); ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? function addClassName(classname1, classname2) {undefined ? ? ? ? ? ? oLastNav.classList.add(classname1); ? ? ? ? ? ? for (let i = 0; i < oLi.length; i++) {undefined ? ? ? ? ? ? ? ? oLi[i].firstChild.classList.add(classname2); ? ? ? ? ? ? ? ? oLi[i].lastChild.classList.add(classname2); ? ? ? ? ? ? } ? ? ? ? } ? ? }, ? ? bindEVENT: function() {undefined ? ? ? ? var oUl = document.getElementsByClassName('wave-ul'); ? ? ? ? for (let i = 0; i < oUl.length; i++) {undefined ? ? ? ? ? ? oUl[i].onmouseover = function() {undefined ? ? ? ? ? ? ? ? var oLi = oUl[i].children; ? ? ? ? ? ? ? ? for (let i = 0; i < oLi.length; i++) {undefined ? ? ? ? ? ? ? ? ? ? oLi[i].style.transition = '1s ' + 0.1 * i + 's'; ? ? ? ? ? ? ? ? ? ? if (oLi[i].firstChild.className == 'wave-span waveli-in-left') {undefined ? ? ? ? ? ? ? ? ? ? ? ? oLi[i].style.marginLeft = '130px'; ? ? ? ? ? ? ? ? ? ? } else {undefined ? ? ? ? ? ? ? ? ? ? ? ? oLi[i].style.marginLeft = '-130px'; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? oUl[i].onmouseleave = function() {undefined ? ? ? ? ? ? ? ? var oLi = oUl[i].children; ? ? ? ? ? ? ? ? for (let i = 0; i < oLi.length; i++) {undefined ? ? ? ? ? ? ? ? ? ? oLi[i].style.marginLeft = '0px'; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? } } function createWaveNav(dom, json, direction = 'RIGHT', top = '0px') {undefined ? ? var n = new wavenav(json, direction, top); ? ? dom.innerHTML += n.createHTML(); ? ? n.renderCSS(); ? ? wavenav.prototype.bindEVENT(); } var json = {undefined ? ? '1': ['HTML', 'javascript:void(0)'], ? ? '2': ['CSS', 'javascript:void(0)'], ? ? '3': ['JAVASCRIPT', 'javascript:void(0)'], } var json1 = {undefined ? ? '1': ['HTML', 'javascript:void(0)'], ? ? '2': ['CSS', 'javascript:void(0)'], ? ? '3': ['JAVASCRIPT', 'javascript:void(0)'], ? ? '4': ['java', 'javascript:void(0)'] } //調(diào)用方法 createWaveNav(document.getElementById('test'), json); createWaveNav(document.getElementById('test'), json1, 'RIGHT', '200px'); createWaveNav(document.getElementById('test'), json1, 'LEFT'); createWaveNav(document.getElementById('test'), json, 'LEFT', '200px'); </script> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JS中用三種方式實(shí)現(xiàn)導(dǎo)航菜單中的二級(jí)下拉菜單
- JS實(shí)現(xiàn)選中當(dāng)前菜單后高亮顯示的導(dǎo)航條效果
- 一個(gè)js控制的導(dǎo)航菜單實(shí)例代碼
- CSS3+Js實(shí)現(xiàn)響應(yīng)式導(dǎo)航條
- JS 實(shí)現(xiàn)導(dǎo)航欄懸停效果
- js實(shí)現(xiàn)移動(dòng)端導(dǎo)航點(diǎn)擊自動(dòng)滑動(dòng)效果
- JavaScript實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊導(dǎo)航欄變色特效
- js實(shí)現(xiàn)無(wú)限級(jí)樹(shù)形導(dǎo)航列表效果代碼
- js實(shí)現(xiàn)的常用的左側(cè)導(dǎo)航效果
- JavaScript實(shí)現(xiàn)滑動(dòng)導(dǎo)航欄效果
相關(guān)文章
javascript 動(dòng)態(tài)樣式添加的簡(jiǎn)單實(shí)現(xiàn)
下面小編就為大家?guī)?lái)一篇javascript 動(dòng)態(tài)樣式添加的簡(jiǎn)單實(shí)現(xiàn)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10JavaScript遞歸操作樹(shù)形結(jié)構(gòu)代碼示例
前端樹(shù)形結(jié)構(gòu)一般用于網(wǎng)頁(yè)的地理位置輸入框,地理位置級(jí)聯(lián)選擇,人員的部門(mén)選擇等,這篇文章主要給大家介紹了關(guān)于JavaScript遞歸操作樹(shù)形結(jié)構(gòu)的相關(guān)資料,需要的朋友可以參考下2024-01-01

JavaScript實(shí)現(xiàn)生成動(dòng)態(tài)表格和動(dòng)態(tài)效果的方法詳解

深入理解JavaScript中為什么string可以擁有方法

一文教你學(xué)會(huì)用JS實(shí)現(xiàn)圖片懶加載功能

List all the Databases on a SQL Server

開(kāi)源一個(gè)微信小程序儀表盤(pán)組件過(guò)程解析