Home  >  Article  >  Web Front-end  >  javascript AutoScroller 函数类_javascript技巧

javascript AutoScroller 函数类_javascript技巧

WBOY
WBOYOriginal
2016-05-16 18:52:091140browse
复制代码 代码如下:

/*
* Copyright (C) 2007-2009 skylark
* Email:aohailin@gmail.com
* Version:2.1
* 原创程序,转载请保留版权
*/
var $=function(o){return typeof o=="string"?document.getElementById(o):o;};
function AutoScroll(){
this.obj=[];
this.version="2.1";
this.Build();//初始化
};
AutoScroll.prototype.Build=function(){
var me=this;
//取得其他滚动事件
var oldscroll=window.onscroll;
window.onscroll=function(){
//保护其他滚动事件
if("function"==typeof oldscroll){
oldscroll();
}
//得到客户端浏览器参数,兼容IE,FF,Chrome
this.common={
t:document.documentElement.scrollTop||document.body.scrollTop,
h:document.documentElement.clientHeight||document.body.clientHeight,
w:document.documentElement.clientWidth||document.body.clientWidth
};
this.position=[];
for(var i=0;itry{
this.style={};
//获得客户端位置,设置了7种位置
//为了让最小化自动适应位置,这里动态计算位置,所以滚动事件触发时,CPU消耗很大
this.position[i]=[
{x:0,y:this.common.t},
{x:this.common.w-me.obj[i].obj.offsetWidth,y:this.common.t},
{x:0,y:(this.common.h+this.common.t-me.obj[i].obj.offsetHeight)/2+(this.common.t)/2},
{x:this.common.w-me.obj[i].obj.offsetWidth,y:(this.common.h+this.common.t-me.obj[i].obj.offsetHeight)/2+(this.common.t)/2},
{x:(this.common.w-me.obj[i].obj.offsetWidth)/2,y:(this.common.h+this.common.t-me.obj[i].obj.offsetHeight)/2+(this.common.t)/2},
{x:0,y:this.common.h+this.common.t-me.obj[i].obj.offsetHeight},
{x:this.common.w-me.obj[i].obj.offsetWidth,y:this.common.h+this.common.t-me.obj[i].obj.offsetHeight}
];
//处理自定义样式
this.style="object"==typeof me.obj[i].style?{x:me.obj[i].style.left,y:me.obj[i].style.top+this.common.t}:{x:this.position[i][me.obj[i].style].x,y:this.position[i][me.obj[i].style].y};
//定位
me.obj[i].obj.style.left=this.style.x+"px";
me.obj[i].obj.style.top=this.style.y+"px";
}catch(e){
//功能是过滤无效obj
for(var j=i;jme.obj[j]=me.obj[j+1];
me.obj.length=me.obj.length-1;
}
}
}
};
//初始化
window.scroll(1,1);
};
AutoScroll.prototype.Add=function(){
var obj=arguments[0];
//获得当前position
var oldposition=$(obj.id).style.position;
$(obj.id).style.position="absolute";
//不使用fixed,虽然高版本浏览器都已经支持fixed
this.obj.push({
obj:$(obj.id),
oldposition:oldposition,
style:obj.style
});
};
AutoScroll.prototype.Remove=function(){
var obj=arguments[0];
for(var i=0;iif(this.obj[i].obj==$(obj.id)){
//还原初始状态position
this.obj[i].obj.style.position=this.obj[i].oldposition;
//是否真正移除
if(obj.remove){
this.obj[i].obj.innerHTML="";
document.body.removeChild(this.obj[i].obj);
}
//移除obj
for(var j=i;jthis.obj[j]=this.obj[j+1];
}
this.obj.length=this.obj.length-1;
break;
}
}
};
var Scroller=new AutoScroll();
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn