JS實現(xiàn)滑動導航效果
更新時間:2020年01月14日 15:48:50 作者:qq_41557320
這篇文章主要為大家詳細介紹了JS實現(xiàn)滑動導航效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了JS實現(xiàn)滑動導航效果的具體代碼,供大家參考,具體內(nèi)容如下
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
ul {
list-style-type: none;
height: 42px;
position: absolute;
}
img {
vertical-align: middle;
}
li,
a {
cursor: pointer;
}
a {
text-decoration: none;
color: #000000;
display: block;
}
li {
width: 83px;
height: 42px;
/*background-color: pink;*/
float: left;
text-align: center;
line-height: 42px;
}
.box {
position: relative;
width: 530px;
height: 42px;
border: 1px solid deepskyblue;
margin: 100px auto;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
}
.box img#cloud {
width: 83px;
height: 42px;
position: absolute;
left: 0;
top: 0;
}
.box img#xinhao {
position: absolute;
right: 0;
top: 50%;
transform: translateY(-50%);
}
</style>
</head>
<body>
<div class="box" id="box">
<img id="cloud" src="images/cloud.gif" alt="" />
<ul id="ull">
<li><a href="#">長沙校區(qū)</a></li>
<li><a href="#">長沙校區(qū)</a></li>
<li><a href="#">長沙校區(qū)</a></li>
<li><a href="#">長沙校區(qū)</a></li>
<li><a href="#">長沙校區(qū)</a></li>
<li><a href="#">長沙校區(qū)</a></li>
</ul>
<img id="xinhao" src="images/rss.png" alt="" />
</div>
<script src="js/common.js"></script>
<script src="js/changespead.js"></script>
<script>
//獲取每一個li
var list=my$('ull').children;
console.log(list);
//獲取到筋斗云圖片
var im=my$('cloud');
//獲取圖片的寬度
var imgWidth=im.offsetWidth;
console.log(imgWidth);
for(var i=0;i<list.length;i++){
//設置index值用來儲存移動的距離
list[i].setAttribute('index',i);
//遍歷所有l(wèi)i,每個li綁定三個時間指向同一個函數(shù)f1
list[i].onmouseover=f1;
list[i].onmouseout=f1;
list[i].onclick=f1;
//實現(xiàn)函數(shù)f1
function f1(e){
switch(e.type) {
case 'mouseover':
//獲取當前l(fā)i的index值
index=this.getAttribute('index');
//調(diào)用變速移動函數(shù)
movecs(im,imgWidth*index);
break;
case 'click':
index=this.getAttribute('index');
//點擊之后固定
im.style.left=imgWidth*index+'px';
//點擊了其中一個li之后的操作,移開鼠標之后不會彈回去
this.onmouseout=false;
for(var k=0;k<list.length;k++){
list[k].setAttribute('jndex',k);
list[k].onmouseover=function(){
jndex=this.getAttribute('jndex');
movecs(im,imgWidth*jndex);
};
list[k].onmouseout=function(){
movecs(im,imgWidth*index);
};
}
break;
case 'mouseout':
index=0;
movecs(im,imgWidth*index);
break;
}
}
}
/*list[i].onmouseover=function(){
index=this.getAttribute('index');
console.log(index);
movecs(im,imgWidth*index);
};
list[i].onmouseout=function(){
index=0;
// im.style.left='0px';
movecs(im,imgWidth*index);
};
list[i].onclick=function(){
index=this.getAttribute('index');
im.style.left=imgWidth*index+'px';
alert(index);
};*/
</script>
</body>
</html>
最終效果如下:云彩跟隨鼠標移動,點擊固定

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript ASCII和Hex互轉(zhuǎn)的實現(xiàn)方法
下面小編就為大家?guī)硪黄猨avascript ASCII和Hex互轉(zhuǎn)的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12
3種js實現(xiàn)string的substring方法
這篇文章主要介紹了3種javascript實現(xiàn)string的substring方法,需要的朋友可以參考下2015-11-11

