1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > html侧边导航栏跟随页面滚动 javascript练习:导航随内容滚动而跟随变化

html侧边导航栏跟随页面滚动 javascript练习:导航随内容滚动而跟随变化

时间:2018-11-17 15:36:01

相关推荐

html侧边导航栏跟随页面滚动 javascript练习:导航随内容滚动而跟随变化

window.οnlοad=function(){

nav();

};

//nav导航

function nav(){

var arr = [pos($('div1')).top,pos($('div2')).top,pos($('div3')).top,pos($('div4')).top,pos($('div5')).top,pos($('div6')).top,pos($('div7')).top,pos($('div8')).top];

var oUl=getByClass('nav')[0];

var aLi = oUl.getElementsByTagName('li');

console.log(aLi.length);

(function(){

aLi[0].style.background ='#fd8200';

aLi[0].s=true;

})();

for(var i=0; i

aLi[i].onmouseover = function(){

if(!this.s){

this.style.background='#fd8200'

}

}

aLi[i].onmouseout = function(){

if(!this.s){

this.style.background='';

}

}

aLi[i].index=i;

aLi[i].οnclick=function(){

num = this.index;

for(var i=0; i

aLi[i].style.background='';

aLi[i].s='';

}

this.style.background='#fd8200';

this.s=true;

window.οnscrοll=function(){

}

}

}

document.onmousewheel=fnMouse;//滚轮事件

if(document.addEventListener){

document.addEventListener('DOMMouseScroll',fnMouse,false);

}

function fnMouse(){

for(var i=0;i

aLi[i].style.background='';

console.log("y "+scrollY());

console.log("arr "+arr[i]);

if(scrollY()>arr[i] && scrollY()

aLi[i].style.background='#fd8200';

};

if(scrollY()>arr[arr.length-1]){

aLi[arr.length-1].style.background='#fd8200';

};

}

}

}

// 滚动条高

function scrollY() {

return document.documentElement.scrollTop || document.body.scrollTop;

};

//获取className

function getByClass(sClass,parent){

var aEles=(parent || document).getElementsByTagName('*');

var re = new RegExp('\b'+ sClass +'\b');

var arr=[];

for(var i=0;i

if(re.test(aEles[i].className)){

arr.push(aEles[i])

}

}

return arr;

}

//获取ID

function $(id){

return document.getElementById(id);

}

//obj到页面顶部实际距离

function pos(obj){

var result = {left:0,top:0};

while(obj){

result.top +=obj.offsetTop;

result.left +=obj.offsetLeft;

obj=obj.offsetParent;

}

return result;

}

演示

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。