Copy codeThe Code is as follows:
/*
* Copyright (C) 2007-2009 skylark
* Email: aohailin@gmail.com
* Version: 2.1
* Original program, reprinted, Please retain the Copyright
*/
Var $ = function (o) {return typeof o = "string "? Document. getElementById (o): o ;};
Function AutoScroll (){
This. obj = [];
This. version = "2.1 ";
This. Build (); // Initialization
};
AutoScroll. prototype. Build = function (){
Var me = this;
// Obtain other rolling events
Var oldscroll = window. onscroll;
Window. onscroll = function (){
// Protect other rolling events
If ("function" = typeof oldscroll ){
Oldscroll ();
}
// Obtain the client browser parameters, which are compatible with IE, FF, and Chrome.
This. common = {
T: document.doc umentElement. scrollTop | document. body. scrollTop,
H: document.doc umentElement. clientHeight | document. body. clientHeight,
W: document.doc umentElement. clientWidth | document. body. clientWidth
};
This. position = [];
For (var I = 0; I <me. obj. length; I ++ ){
Try {
This. style = {};
// Obtain the client location. Seven locations are set.
// In order to minimize Automatic Location adaptation, the location is calculated dynamically here, So CPU consumption is high when a rolling event is triggered.
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}
];
// Process custom styles
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 };
// Locate
Me. obj [I]. obj. style. left = this. style. x + "px ";
Me. obj [I]. obj. style. top = this. style. y + "px ";
} Catch (e ){
// The function is to filter invalid 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;
}
}
}
};
// Initialization
Window. scroll (1, 1 );
};
AutoScroll. prototype. Add = function (){
Var obj = arguments [0];
// Obtain the current position
Var oldposition = $ (obj. id). style. position;
$ (Obj. id). style. position = "absolute ";
// No fixed is used. Although the later browsers support 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 )){
// Restore the initial position
This. obj [I]. obj. style. position = this. obj [I]. oldposition;
// Whether to actually remove
If (obj. remove ){
This. obj [I]. obj. innerHTML = "";
Document. body. removeChild (this. obj [I]. obj );
}
// Remove 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 ();