js實現(xiàn)導航跟隨效果
本文實例為大家分享了js實現(xiàn)導航跟隨效果展示的具體代碼,供大家參考,具體內(nèi)容如下
如何實現(xiàn)上面的效果,請看下面的步驟
第一步:用 css 調(diào)整樣式 ,這里小編用的是彈性盒子實現(xiàn)導航的平均分配。(聰明的你可以嘗試用其他的方式看看能不能實現(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 標簽中的 href 屬性后面加上那句代碼是為了在實現(xiàn)點擊事件時不讓他有其他事件發(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" >首頁 </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" >新聞頭條</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> </ul>
第二步:分析下如何獲得 圖片(cloud.gif) 距離最左邊的 left 值

第三步:實現(xiàn)鼠標移動,移除,和點擊事件的效果
<script type="text/javascript">
//獲得類為cloud的標簽,
var pic=document.getElementsByClassName('cloud')[0];
//獲得所有的 li 標簽
var liList=document.getElementsByTagName('li');
//定義向右的移動初始距離
var liLeft=32;
//定義緩慢動畫的初始值
var header=32;
//用于定義當鼠標點擊時的初始位置
var currentLeft=32;
for(var i=0;i<liList.length;i++){
//鼠標放上事件
liList[i].onmouseover=function(){
//獲取目標距離
liLeft = this.offsetLeft+this.offsetWidth/2-pic.offsetWidth/2;
}
//鼠標移除事件
liList[i].onmouseout=function(){
//當鼠標移除某個li的時候把目標距離改為初始狀態(tài)
liLeft=currentLeft;
}
//鼠標點擊事件
liList[i].onclick=function(){
currentLeft=this.offsetLeft+this.offsetWidth/2-pic.offsetWidth/2;
}
}
//定義緩慢動畫
setInterval(function(){
header = header + (liLeft-header)/10;
pic.style.left = header + 'px';
},20);
</script>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
談談js中的prototype及prototype屬性解釋和常用方法
prototype是javascript中筆記難理解的一部分內(nèi)容,下面通過幾個關鍵知識點給大家講解js中的prototype,對js中的prototype相關知識感興趣的朋友一起學習吧2015-11-11
JavaScript 中調(diào)用 Kotlin 方法實例詳解
這篇文章主要介紹了JavaScript 中調(diào)用 Kotlin 方法實例詳解的相關資料,需要的朋友可以參考下2017-06-06

