Javascript follows the scroll bar to learn onscroll events

Source: Internet
Author: User

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)

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.