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 ()