Today I have studied the scroll bar event, which I can see without worry. By the way, I will record it @
For example, to make a floating advertisement effect, the principle is to set the position of a timer 0.1 s detection layer and specify it to the position equivalent to the window.
CoreCodeAs follows:
Code
< Script Type = "Text/JavaScript" >
Function Scrollimg (){
VaR Posx, Posy;
If (Window. innerheight ){// Compatibility judgment
Posx = Window. pagexoffset;
Posy = Window. pageyoffset;
}
Else If (Document.doc umentelement && Document.doc umentelement. scrolltop ){// Compatibility judgment
Posx = Document.doc umentelement. scrollleft;
Posy = Document.doc umentelement. scrolltop;
}
Else If (Document. Body ){// Compatibility judgment
Posx = Document. Body. scrollleft;
Posy = Document. Body. scrolltop;
}
VaR Ad = Document. getelementbyid ( " Ad " );
Ad. style. Top = (Posy + 100 ) + " Px " ;
Ad. style. Left = (Posx + 50 ) + " Px " ;
SetTimeout ( " Scrollimg () " , 100 );
}
</ Script >
Important:
On the XHTML page, document. body. scrolltop is always 0, that is, this attribute is invalid. Therefore, it must be determined by other attributes. to be compatible with the new and old standards, the availability of the attributes should be determined.
Applying web standards will invalidate the scrolltop attribute !!! <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> with this section,Document. Body. scrolltop is always equal to 0Body onscroll = "alert (document. Body. scrolltop);" will never be triggered.
Solution:Usage:
Document.doc umentelement. scrolltop
There are two examples below
Example 1:VaR scrollpos; If (typeof window. pageyoffset! = 'Undefined '){// NetscapeScrollpos = Window. pageyoffset ;}
Else if (typeof document. compatmode! = 'Undefined' & document. compatmode! = 'Background') {scrollpos = document.doc umentelement. scrolltop ;}
Else if (typeof document. Body! = 'Undefined') {scrollpos = Document. Body. scrolltop ;}
Alert (scrollpos );Example 2:Function webform_getscrollx (){If (_ nonmsdombrowser) {return window. pagexoffset;} else {If (document.doc umentelement & document.doc umentelement. scrollleft) {return document.doc umentelement. scrollleft;} else if (document. body) {return document. body. scrollleft ;}}Return 0 ;}@ pageyoffset is the document.body.scrolltopand document.doc umentelement. scrolltop of Netscape are IE, but I don't know the real difference between them,
Only the documentelement. scrolltop is XHTML compatible (the author uses strict)