Effect:
Ideas:
First, load the onscroll control scroll bar. Then write the cache movement method. The buffer movement method is to calculate the DIV buffer speed first, and then round it, and then determine when the movement will reach the end point. Finally, return the parameters. Call this method in onscroll, and calculate the endpoint and assign the parameter to this method.
Code:
Copy codeThe Code is as follows:
<Head runat = "server">
<Title> </title>
<Style type = "text/css">
# Div1
{
Width: 200px;
Height: 200px;
Background: # 0000FF;
Position: absolute;
Right: 0;
Bottom: 0;
}
</Style>
<Script type = "text/javascript">
Window. onscroll = function (){
Var oDiv = document. getElementById ('div1 ');
Var DivScroll = document.doc umentElement. scrollTop | document. body. scrollTop; // get the moving height
// ODiv. style. top = (document.doc umentElement. clientHeight-oDiv. offsetHeight)/2 + DivScroll + 'px ';
Move(parseInt((document.doc umentElement. clientHeight-oDiv. offsetHeight)/2 + DivScroll); // call the parameter passing, where the parameter is the end point of the DIV. That is, (visible height-DIV height)/2 + moving height
};
Var timer = null;
Function move (end ){
ClearInterval (timer); // first, disable setInterval if it is enabled;
Timer = setInterval (function (){
Var oDiv = document. getElementById ('div1 ');
Var speed = (end-oDiv. offsetTop)/5; // calculate the speed at which the DIV is going. The speed at which the DIV is going is equal to (end-offsetTop height)/zoom factor
Speed = speed> 0? Math. ceil (speed): Math. floor (speed); // round it to avoid decimal places
If (oDiv. offsetTop = end) {// when the DIV ends, disable setInterval;
ClearInterval (timer );
}
Else {
ODiv. style. top = oDiv. offsetTop + speed + 'px'; // move the div
}
}, 30 );
}
</Script>
</Head>
<Body style = "height: 1000px;">
<Div id = "div1">
</Div>
</Body>