javascript AutoScroller 函數(shù)類
更新時(shí)間:2009年05月29日 20:33:01 作者:
javascript AutoScroller 自動(dòng)滾動(dòng)類代碼,學(xué)習(xí)類的朋友可以參考下。
復(fù)制代碼 代碼如下:
/*
* Copyright (C) 2007-2009 skylark
* Email:aohailin@gmail.com
* Version:2.1
* 原創(chuàng)程序,轉(zhuǎn)載請(qǐng)保留版權(quán)
*/
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;
//取得其他滾動(dòng)事件
var oldscroll=window.onscroll;
window.onscroll=function(){
//保護(hù)其他滾動(dòng)事件
if("function"==typeof oldscroll){
oldscroll();
}
//得到客戶端瀏覽器參數(shù),兼容IE,F(xiàn)F,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;i<me.obj.length;i++){
try{
this.style={};
//獲得客戶端位置,設(shè)置了7種位置
//為了讓最小化自動(dòng)適應(yīng)位置,這里動(dòng)態(tài)計(jì)算位置,所以滾動(dòng)事件觸發(fā)時(shí),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;j<me.obj.length-1;j++){
me.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];
//獲得當(dāng)前position
var oldposition=$(obj.id).style.position;
$(obj.id).style.position="absolute";
//不使用fixed,雖然高版本瀏覽器都已經(jīng)支持fixed
this.obj.push({
obj:$(obj.id),
oldposition:oldposition,
style:obj.style
});
};
AutoScroll.prototype.Remove=function(){
var obj=arguments[0];
for(var i=0;i<this.obj.length;i++){
if(this.obj[i].obj==$(obj.id)){
//還原初始狀態(tài)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;j<this.obj.length-1;j++){
this.obj[j]=this.obj[j+1];
}
this.obj.length=this.obj.length-1;
break;
}
}
};
var Scroller=new AutoScroll();
您可能感興趣的文章:
- 深入理解Android中Scroller的滾動(dòng)原理
- Android程序開發(fā)之UIScrollerView里有兩個(gè)tableView
- iOS中UIScrollerView的用法及基于AotoLayout的控件懸停
- 詳解Android應(yīng)用開發(fā)中Scroller類的屏幕滑動(dòng)功能運(yùn)用
- 詳解Android Scroller與computeScroll的調(diào)用機(jī)制關(guān)系
- jQuery滾動(dòng)條插件nanoscroller使用指南
- android開發(fā)之橫向滾動(dòng)/豎向滾動(dòng)的ListView(固定列頭)
- android實(shí)現(xiàn)上下滾動(dòng)的TextView
- Android中實(shí)現(xiàn)多行、水平滾動(dòng)的分頁的Gridview實(shí)例源碼
- Android Scroller完全解析
相關(guān)文章
javascript數(shù)組去重方法終極總結(jié)
這篇文章主要介紹了javascript數(shù)組去重終極總結(jié),本文列舉了3種javascript數(shù)組去重方法,并分別分析了它們的優(yōu)缺點(diǎn),需要的朋友可以參考下2014-06-06js canvas實(shí)現(xiàn)QQ撥打電話特效
這篇文章主要為大家詳細(xì)介紹了js canvas實(shí)現(xiàn)QQ撥打電話特效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05JavaScript中判斷原生函數(shù)檢查function是否是原生代碼
檢查某個(gè)function是否是原生代碼,要檢測(cè)這一點(diǎn),最簡(jiǎn)單的辦法當(dāng)然是判斷函數(shù)的 toString 方法返回的值2014-09-09JavaScript?字符串新增方法?trim()?的使用說明
這篇文章主要介紹了JavaScript字符串新增方法trim()的使用說明,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-09-09JavaScript模擬實(shí)現(xiàn)"雙11"限時(shí)秒殺效果
每年的“雙11”啊,都是大家的剁手節(jié)。大家都在晚上12點(diǎn),捧著手機(jī)看著倒計(jì)時(shí),在他倒數(shù)到0的時(shí)候瘋狂點(diǎn)擊下單。可是你有沒想過限時(shí)秒殺是怎么實(shí)現(xiàn)的呢?本文將為你揭秘如何用JavaScript實(shí)現(xiàn)限時(shí)秒殺,快來了解一下吧2022-03-03List the UTC Time on a Computer
List the UTC Time on a Computer...2007-06-06