如何改js让导航滑块能像这个网站的效果一样

JavaScript010

如何改js让导航滑块能像这个网站的效果一样,第1张

仅有滑块 效果~~~  原生JS 写的  希望采纳! 一个一个手打的!

<!DOCTYPE html>

<html>

<head>

<title></title>

<script>

window.onload=function(){

var oul=document.getElementById('oul')

var oli=oul.getElementsByTagName('li')

for(var i=0 i<oli.length i++){

oli[i].className=''

oli[0].className='on'

oli[i].onmouseover=function(){

for(var j=0 j<oli.length j++){

oli[j].className=''

}

this.className='on'

}

oli[i].onmouseout=function(){

for(var j=0 j<oli.length j++){

oli[j].className=''

oli[0].className='on'

}

}

}

}

</script>

<style type="text/css">

.nav{ 

width: 1000px 

height: 80px 

background: #fff 

margin:0px auto 

font-size:14px 

}

#oul{

list-style: none 

margin:0px 

padding: 0px  

width: 1000px 

height: 80px 

position: relative  

}

#oul li{

float:left 

height: 80px

line-height: 80px 

display: block

position: relative

cursor: pointer 

color:#000 transition:color 200ms

font-weight: bold

overflow: hidden

width: 100px

}

#oul li span{

position: absolute  

width: 100%  

background: red  

left: 0

height: 80px 

top: 80px 

transition:top 200ms 

z-index: 1

}

#oul li div{

position: absolute 

z-index: 2 

display: block 

left: 0

top: 0

margin:0px

padding:0px

width: 100%

height: 100%

line-height: 80px

text-align: center

white-space: nowrap text-overflow: ellipsis

}

#oul li:hover span{ 

top:0px 

transition:top 200ms 

}

#oul li.on{color:#fff transition:color 200ms}

#oul li.on .spanbkg{ 

position: absolute  

width: 100%  

background: red 

height: 100% 

left: 0 

top: 0px   

}

</style>

</head>

<body>

<div class="nav">

<ul id="oul">

<li><span class="spanbkg"></span><div>首页</div></li>

<li><span class="spanbkg"></span><div>万科集团</div></li>

<li><span class="spanbkg"></span><div>新闻中心</div></li>

<li><span class="spanbkg"></span><div>产品服务</div></li>

<li><span class="spanbkg"></span><div>投资者关系</div></li>

</ul>

</div>

</body>

</html>

1、输入代码,其中button是鼠标要滑动的地方,滑过button时,class为content的内容就要显示出来,鼠标滑出去的时候,这块内容就需要重新隐藏起来。

2、把黑色内容的部分初始状态写成隐藏。只需要在class为content里写上display:none,即可。

3、来看下代码和浏览器中的效果。可以看到黑色内容部分已经不见了。

4、把onmouseover和onmouseout两个事件作用在button上面,再写overShow()和overHide()两个 函数。可以看到在函数中通过改content的display属性来实现显示隐藏的效果。

5、在浏览器中就可以看到效果了。

这是自己封装的原生js方法,为了偷懒,用了电jqurey。调用方法如下:

touchs._left(object)//往左滑动,

touchs._right(object)//往右滑动

touchs._top(object)//往上滑动

touchs._bottom(object)//往下滑动

说明:object的取值可以是一个id("#id")表达式,或者是一个样式表达式(".class"),或者标签的属性表达式("div[id=id]")等,只要可以用jqurey通过这个表达式获取到dom对象,这个表达式怎么写都可以。