手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >JS 实现导航栏悬停效果
JS 实现导航栏悬停效果
摘要:JS-实现导航栏悬停先布个局:复制代码代码如下:Test1tab1tab2tab3tab4234567添加简单的样式:复制代码代码如下:di...

JS-实现导航栏悬停

先布个局:

复制代码 代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<head>

<script type="text/javascript" src="test.js"></script>

<link rel="stylesheet" type="text/css" href="test.css"></link>

<title>Test</title>

</head>

<body>

<div>

<div>1</div>

<div id="nav">

<div>tab1</div>

<div>tab2</div>

<div>tab3</div>

<div>tab4</div>

</div>

<div>2</div>

<div>3</div>

<div>4</div>

<div>5</div>

<div>6</div>

<div>7</div>

</div>

</body>

</html>

添加简单的样式:

复制代码 代码如下:

div.main{

width: 800px;

background: #CCC;

margin: 10px auto 0;

position: relative;

}

div.content{

width: 800px;

height: 400px;

background: yellow;

margin: 10px auto 0;

}

div.navigation{

width: 800px;

height: 40px;

background: red;

margin: 4px auto 0;

top: 400px;

left: 0px;

position: absolute;

}

div.tab{

width: 195px;

height: 40px;

background: blue;

float: left;

margin-left: 5px;

}

JS:

复制代码 代码如下:

//记录导航条原来在页面上的位置

var naviga_offsetTop = 0;

//IE7不识别getElementsByClassName,为了兼容自定义一个

function my_getElementsByClassName(class_name) {

var el = [];

//获取所有元素

_el = document.getElementsByTagName('*');

//通过className刷选

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

if (_el[i].className == class_name ) {

el[el.length] = _el[i];

}

}

return el;

}

//导航条,悬停在顶部

function naviga_stay_top(){

var a_navigation_bar = [];

if(document.getElementsByClassName){//Chrome, FF

a_navigation_bar = document.getElementsByClassName("navigation");

} else {//IE

a_navigation_bar = my_getElementsByClassName("navigation");

}

var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;

if( scrollTop > naviga_offsetTop){

a_navigation_bar[0].style.top = scrollTop + "px";

} else {

a_navigation_bar[0].style.top = naviga_offsetTop + "px";

}

}

//给导航条上四个tab,加上点击事件。

window.onload=function(){

var a_tabs = [];

if( document.getElementsByClassName ){//Chrome, FF

a_tabs = document.getElementsByClassName("tab");

}else{ //IE

a_tabs = my_getElementsByClassName("tab");

}

var a_contents = [];

if( document.getElementsByClassName ){//Chrome, FF

a_contents = document.getElementsByClassName("content");

}else{//IE

a_contents = my_getElementsByClassName("content");

}

a_tabs[0].onclick=function(){

window.scrollTo(0, a_contents[2].offsetTop);

}

a_tabs[1].onclick=function(){

window.scrollTo(0, a_contents[3].offsetTop);

}

a_tabs[2].onclick=function(){

window.scrollTo(0, a_contents[4].offsetTop);

}

a_tabs[3].onclick=function(){

window.scrollTo(0, a_contents[5].offsetTop);

}

//获取页面上,导航条到顶部的位置

var a_navigation_bar = [];

if(document.getElementsByClassName){//Chrome, FF

a_navigation_bar = document.getElementsByClassName("navigation");

} else {//IE

a_navigation_bar = my_getElementsByClassName("navigation");

}

naviga_offsetTop = a_navigation_bar[0].offsetTop;

//给滚动条以及鼠标加上滚动事件

// window.onscroll= naviga_stay_top;

// document.onmousewheel= naviga_stay_top;

if( window.attachEvent) //IE

{

window.attachEvent("onmousewheel", naviga_stay_top);

window.attachEvent("onscroll", naviga_stay_top);

document.attachEvent("onmousewheel", naviga_stay_top);

document.attachEvent("onscroll", naviga_stay_top);

} else {//Chrome ,FF

window.addEventListener("mousewheel", naviga_stay_top,false);

window.addEventListener("scroll", naviga_stay_top,false);

document.addEventListener("mousewheel", naviga_stay_top,false);

document.addEventListener("scroll", naviga_stay_top,false);

}

}

不知道什么原因,在IE上,滚动滚动条或者鼠标滚轮的时候,导航栏会抖动;而在Chrome和FF上有没有任何问题。还望高人指点。

【JS 实现导航栏悬停效果】相关文章:

JS实现浏览器菜单命令

js+html5实现canvas绘制圆形图案的方法

JS实现简单路由器功能的方法

Javascript实现飞动广告效果的方法

javascript实现可全选、反选及删除表格的方法

对联浮动广告效果

JQuery实现动态添加删除评论的方法

JS实现模拟风力的雪花飘落效果

javascript实现带下拉子菜单的导航菜单效果

js实现精美的图片跟随鼠标效果实例

精品推荐
分类导航