原生JS實(shí)現(xiàn) MUI導(dǎo)航欄透明漸變效果
mui內(nèi)置有H5版本的透明漸變導(dǎo)航控件,教程參考mui官網(wǎng);透明漸變導(dǎo)航是一種解決滾動(dòng)條通頂?shù)淖兺ǚ桨?,相比雙webview,具有更高的性能和更好的動(dòng)畫(huà)效果;
下面通過(guò)本文給大家分享基于原生JS實(shí)現(xiàn) MUI導(dǎo)航欄透明漸變效果,具體內(nèi)容詳情如下所示:
首先聲明:由于backgroundColor的值采用的是RGBA,IE8及以下不支持,所以此效果不支持IE8及以下的瀏覽器
css代碼
body,p,h1{margin: 0;} .module-layer{ width: 100%; position: fixed; top: 0; left: 0; z-index: 100000; } .module-layer-content{ position: relative; min-width: 320px; max-width: 750px; width: 100%; margin: 0 auto; background-color: rgba(255, 0, 0, 0.9); } .module-layer-bg{ width: 100%; height: 100%; background: #000; opacity: .7; position: absolute; top: 0; left: 0; z-index: -1; } .layer-head-name{ height: 50px; line-height: 50px; text-align: center; padding: 0 50px; font-size: 20px; } .layer-return,.layer-share{ width: 50px; height: 50px; line-height: 50px; text-align: center; position: absolute; top:0; z-index: 1; } .layer-return{left: 0;} .layer-share{right: 0;} .fixed-layer{ height: 100%; display: none; z-index: 100001; } .relative-layer{height: 100%;} .layer-content{ padding:3%; position: relative; top: 20%; } .layer-close{ width: 2rem; height: 2rem; position: absolute; top:3%; right: 3%; } .pr { position:relative; } #shop-input::-webkit-input-placeholder { color:#fff; } #shop-input:-moz-placeholder { color:#fff; } #shop-input::-moz-placeholder { color:#fff; } #shop-input:-ms-input-placeholder { color:#fff; } #shop-input { border:none; outline:none; background:transparent; } .search-box { height:30px; border-radius:20px; top:10px; overflow:hidden; z-index:10; } .search-box:after { content:''; display:block; width:100%; height:30px; background:#fff; opacity:.5; position:absolute; top:0; left:0px; z-index:-1; } #shop-input { height:28px; line-height:28px; font-size:16px; position:absolute; top:0; left:30px; } .shop-search { width:16px; height:16px; position:absolute; top:7px; left:6px; } .layer-return{ background: url(images/return.png) no-repeat center center/12px 20px; } .layer-share{ background: url(images/share.png) no-repeat center center/20px 30px; } a { -webkit-tap-highlight-color: transparent; -webkit-touch-callout: none; -webkit-user-select: none; } .module-content{ min-width: 320px; max-width: 750px; width: 100%; margin: 0 auto; background: #fff; } .module-content div:first-child img{margin-top: 0;} .module-content div img{ display: block; width: 100%; margin-top: 10px; }
HTML代碼
<header class="module-layer"> <div class="module-layer-content"> <p class="layer-return"></p> <h1 class="layer-head-name"> <div class="pr search-box"> <img class="shop-search" src="images/search.png"/> <input id="shop-input" type="text" placeholder="搜索店內(nèi)商品" value="" /> </div> </h1> <p class="layer-share"></p> </div> </header> <div class="module-content"> <div><img src="images/banner.png"/></div> <div><img src="images/banner1.png"/></div> <div><img src="images/banner3.png"/></div> <div><img src="images/banner4.jpg"/></div> <div><img src="images/banner5.png"/></div> <div><img src="images/banner6.png"/></div> <div><img src="images/banner7.jpg"/></div> <div><img src="images/banner8.jpg"/></div> </div>
JS代碼
(function(){ //獲取滾動(dòng)條當(dāng)前位置 function getScrollTop(){ var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0; if(document.body){ bodyScrollTop = document.body.scrollTop; } if(document.documentElement){ documentScrollTop = document.documentElement.scrollTop; } scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop; return scrollTop; } //獲取CSS樣式 function getStyle(element, attr){ if(element.currentStyle){ return element.currentStyle[attr]; }else{ return window.getComputedStyle(element,null)[attr]; } } //獲取原始backgroundColor值 var color = getStyle(document.getElementsByClassName('module-layer-content')[0],'backgroundColor'); //取到RGB var colorRgb = color.substring(0,color.lastIndexOf(',') + 1); //取到A var colorA = color.substring(color.lastIndexOf(',') + 1,color.length - 1); //對(duì)A判斷,如果最終值小于0.9,直接設(shè)置為1 if(colorA < 0.9){colorA = 1;} //設(shè)置背景色的A的函數(shù) var setCoverOpacity = function() { document.getElementsByClassName('module-layer-content')[0].style.background = colorRgb + (((getScrollTop() / 550) > colorA) ? colorA : (getScrollTop() / 550)) + ')'; } //初始化函數(shù) setCoverOpacity(); //綁定滾動(dòng)監(jiān)聽(tīng)事件 window.addEventListener('scroll',setCoverOpacity,false); }())
注意:
不兼容IE8及以下的IE瀏覽器;
550是滾動(dòng)條到達(dá)位置的最終透明度,此處根據(jù)需要自定義;
CSS終背景顏色的RGBA的A是最終透明度
總結(jié)
以上所述是小編給大家介紹的原生JS實(shí)現(xiàn) MUI導(dǎo)航欄透明漸變效果,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- JS 實(shí)現(xiàn)導(dǎo)航欄懸停效果
- JavaScript實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊導(dǎo)航欄變色特效
- JavaScript實(shí)現(xiàn)滑動(dòng)導(dǎo)航欄效果
- Fullpage.js固定導(dǎo)航欄-實(shí)現(xiàn)定位導(dǎo)航欄
- JS滾動(dòng)到指定位置導(dǎo)航欄固定頂部
- 一步步教大家編寫(xiě)酷炫的導(dǎo)航欄js+css實(shí)現(xiàn)
- js導(dǎo)航欄單擊事件背景變換示例代碼
- JavaScript NodeTree導(dǎo)航欄(菜單項(xiàng)JSON類(lèi)型/自制)
- js實(shí)現(xiàn)帶緩動(dòng)動(dòng)畫(huà)的導(dǎo)航欄效果
- JS實(shí)現(xiàn)百度新聞導(dǎo)航欄效果
相關(guān)文章
JavaScript加強(qiáng)之自定義event事件
event事件在本文以自定義的方式出現(xiàn),感興趣的朋友可以參考下,希望對(duì)大家有所幫助2013-09-09小程序圖片長(zhǎng)按識(shí)別功能的實(shí)現(xiàn)方法
這篇文章主要介紹了小程序圖片長(zhǎng)按識(shí)別功能的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08js模擬如何實(shí)現(xiàn)重載以及默認(rèn)參數(shù)
這篇文章主要介紹了js模擬如何實(shí)現(xiàn)重載以及默認(rèn)參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05javascript異常處理實(shí)現(xiàn)原理詳解
這篇文章主要介紹了javascript異常處理實(shí)現(xiàn)原理詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02Javascript 繼承機(jī)制的實(shí)現(xiàn)
要用ECMAScript實(shí)現(xiàn)繼承機(jī)制,首先從基類(lèi)入手。所有開(kāi)發(fā)者定義的類(lèi)都可作為基類(lèi)。出于安全原因,本地類(lèi)和宿主類(lèi)不能作為基類(lèi),這樣可以防止公用訪(fǎng)問(wèn)編譯過(guò)的瀏覽器級(jí)的代碼,因?yàn)檫@些代碼可以被用于惡意攻擊。2009-08-08通過(guò)學(xué)習(xí)bootstrop導(dǎo)航條學(xué)會(huì)修改bootstrop顏色基調(diào)
這篇文章主要介紹了通過(guò)學(xué)習(xí)bootstrop導(dǎo)航條學(xué)會(huì)修改bootstrop顏色基調(diào),需要的朋友可以參考下2017-06-06淺談Javascript如何實(shí)現(xiàn)勻速運(yùn)動(dòng)
這篇文章主要介紹了淺談Javascript如何實(shí)現(xiàn)勻速運(yùn)動(dòng)的方法及相關(guān)代碼,需要的朋友可以參考下2014-12-12js blob類(lèi)型url的視頻下載問(wèn)題的解決
這篇文章主要介紹了js blob類(lèi)型url的視頻下載問(wèn)題的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11