The source interpretation of JQuery scrollleft () and ScrollTop ()

Source: Internet
Author: User

The implementations here are also easy to understand, adding scrollleft and ScrollTop methods to the jquery prototype through the static method of jquery.

It distinguishes the window from the ordinary element when the value is taken.

If it is window, use Window.pagexoffset window.pageyoffset to get

If it is an element, it is obtained by Element.scrollleft Element.scrolltop.

About PAGEXOFFSET,MDN Introduction:

var x = (window.pagexoffset!== undefined)  ? window.pagexoffset  | | Document.body.parentNode | | document.body). scrollleft; var y = (window.pageyoffset!== undefined)  ? window.pageyoffset  | | Document.body.parentNode | | document.body). scrolltop;

Jquery.each ({scrollleft: "Pagexoffset", ScrollTop: "pageYOffset"},function(method, prop) {vartop = "pageYOffset" = = =prop; Jquery.fn[Method]=function(val) {//Call the Access global method        //method (Elem[0],val) is called Inside Access        returnAccess This,function(Elem, method, Val) {varWin = GetWindow (elem);//is not window            if(val = = = undefined) {//if Val is undefined                //if win exists, return Window[prop] prop is the key value of the object                //return Elem[method] method is the key name of the object if it does not exist                returnWin? win[prop]: Elem[method];//Window.pagexoffset or Elem.scrollleft            }            //here is the setting value            if(Win) {//$ (window) $ (document)Win.scrollto (!top?Val:window.pageXOffset, Top?val:window.pageYOffset); } Else{elem[Method]=Val; }}, method, Val, Arguments.length,NULL ); };});

The source interpretation of JQuery scrollleft () and ScrollTop ()

Related Article

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.