js實(shí)現(xiàn)導(dǎo)航跟隨效果
本文實(shí)例為大家分享了js實(shí)現(xiàn)導(dǎo)航跟隨效果展示的具體代碼,供大家參考,具體內(nèi)容如下
如何實(shí)現(xiàn)上面的效果,請(qǐng)看下面的步驟
第一步:用 css 調(diào)整樣式 ,這里小編用的是彈性盒子實(shí)現(xiàn)導(dǎo)航的平均分配。(聰明的你可以嘗試用其他的方式看看能不能實(shí)現(xiàn))css代碼如下:
<style type="text/css">
*{padding:0;margin:0;}
a{text-decoration:none;}
html,body{height:100%;width:100%;background:black;}
ul{position:relative;width:990px;list-style:none;background:white;display: flex;flex-direction:row;justify-content: space-around;margin:50px auto;border-radius:10px;}
ul li{position: relative;flex:1;text-align:center;}
ul li a{font-size:18px;color:#333;padding:10px 0;display: block;}
.cloud{position:absolute;left:32px;top:0;bottom:0;margin:auto;width:83px;height:42px;background:url('images/cloud.gif');}
</style>
html代碼如下:這里 a 標(biāo)簽中的 href 屬性后面加上那句代碼是為了在實(shí)現(xiàn)點(diǎn)擊事件時(shí)不讓他有其他事件發(fā)生
<ul> <span class="cloud"></span> <li> <a href="javascript:viod(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁(yè) </a></li> <li><a href="javascript:viod(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >電視劇</a></li> <li><a href="javascript:viod(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >最新電影</a></li> <li><a href="javascript:viod(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >新聞?lì)^條</a></li> <li><a href="javascript:viod(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >八卦?shī)蕵?lè)</a></li> <li><a href="javascript:viod(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >軍事熱點(diǎn)</a></li> </ul>
第二步:分析下如何獲得 圖片(cloud.gif) 距離最左邊的 left 值

第三步:實(shí)現(xiàn)鼠標(biāo)移動(dòng),移除,和點(diǎn)擊事件的效果
<script type="text/javascript">
//獲得類為cloud的標(biāo)簽,
var pic=document.getElementsByClassName('cloud')[0];
//獲得所有的 li 標(biāo)簽
var liList=document.getElementsByTagName('li');
//定義向右的移動(dòng)初始距離
var liLeft=32;
//定義緩慢動(dòng)畫的初始值
var header=32;
//用于定義當(dāng)鼠標(biāo)點(diǎn)擊時(shí)的初始位置
var currentLeft=32;
for(var i=0;i<liList.length;i++){
//鼠標(biāo)放上事件
liList[i].onmouseover=function(){
//獲取目標(biāo)距離
liLeft = this.offsetLeft+this.offsetWidth/2-pic.offsetWidth/2;
}
//鼠標(biāo)移除事件
liList[i].onmouseout=function(){
//當(dāng)鼠標(biāo)移除某個(gè)li的時(shí)候把目標(biāo)距離改為初始狀態(tài)
liLeft=currentLeft;
}
//鼠標(biāo)點(diǎn)擊事件
liList[i].onclick=function(){
currentLeft=this.offsetLeft+this.offsetWidth/2-pic.offsetWidth/2;
}
}
//定義緩慢動(dòng)畫
setInterval(function(){
header = header + (liLeft-header)/10;
pic.style.left = header + 'px';
},20);
</script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 博客側(cè)邊欄模塊跟隨滾動(dòng)條滑動(dòng)固定效果的實(shí)現(xiàn)方法(js+jquery等)
- js實(shí)現(xiàn)簡(jiǎn)單鼠標(biāo)跟隨效果的方法
- js實(shí)現(xiàn)文字跟隨鼠標(biāo)移動(dòng)而移動(dòng)的方法
- 基于JavaScript實(shí)現(xiàn)div層跟隨滾動(dòng)條滑動(dòng)
- javascript DIV實(shí)現(xiàn)跟隨鼠標(biāo)移動(dòng)
- javascript用DIV模擬彈出窗口_窗體滾動(dòng)跟隨
- js 右側(cè)浮動(dòng)層效果實(shí)現(xiàn)代碼(跟隨滾動(dòng))
- JavaScript實(shí)現(xiàn)文字跟隨鼠標(biāo)特效
相關(guān)文章
談?wù)刯s中的prototype及prototype屬性解釋和常用方法
prototype是javascript中筆記難理解的一部分內(nèi)容,下面通過(guò)幾個(gè)關(guān)鍵知識(shí)點(diǎn)給大家講解js中的prototype,對(duì)js中的prototype相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2015-11-11
JS設(shè)置自定義快捷鍵并實(shí)現(xiàn)圖片上下左右移動(dòng)
這篇文章主要介紹了JS設(shè)置自定義快捷鍵并實(shí)現(xiàn)圖片上下左右移動(dòng),文中通過(guò)使用自定義熱鍵或者使用鍵盤上下左右鍵移動(dòng)圖片,以此來(lái)實(shí)現(xiàn)此功能,需要的朋友可以參考下2019-10-10
JavaScript學(xué)習(xí)筆記整理之引用類型
引用類型是JavaScript中很重要的內(nèi)容,通過(guò)本文給大家介紹JavaScript學(xué)習(xí)筆記整理之引用類型,對(duì)js引用類型相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-01-01
簡(jiǎn)單實(shí)現(xiàn)Bootstrap標(biāo)簽頁(yè)
這篇文章主要教大家簡(jiǎn)單實(shí)現(xiàn)Bootstrap標(biāo)簽頁(yè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
微信小程序 導(dǎo)入圖標(biāo)實(shí)現(xiàn)過(guò)程詳解
這篇文章主要介紹了微信小程序 導(dǎo)入圖標(biāo)實(shí)現(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
JavaScript 中調(diào)用 Kotlin 方法實(shí)例詳解
這篇文章主要介紹了JavaScript 中調(diào)用 Kotlin 方法實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06

