Fixed position with height

Source: Internet
Author: User

varjquery = require ("jquery");(function($) {JQuery.fn.scrollFix=function(height, dir) {height= Height | | 0; Height= height = = "Top"? 0: Height; return  This. each (function() {            if(height = = "Bottom") {Height= Document.documentElement.clientHeight- This. scrollheight; } Else if(Height < 0) {Height= Document.documentElement.clientHeight- This. scrollheight +height; }            varthat = $ ( This), OldHeight=false, P, R, L=That.offset (). Left; Dir= Dir = = "Bottom"? Dir: "Top";//default scrolling direction down            if(Window. XMLHttpRequest) {//fixed for non-IE6                functionGetHeight () {//>=0 indicates that the above scrolling height is greater than or equal to the target height                    return(Document.documentElement.scrollTop | | document.body.scrollTop) + height-That.offset (). Top; } $ (window). Scroll (function() {                    if(OldHeight = = =false) {                        if((getheight () >= 0 && dir = = "Top") | | (getheight () <= 0 && dir = = "Bottom") ) {OldHeight= That.offset (). Top-height; That.css ({position:"Fixed", Top:height, left:l}); }                    } Else {                        if(dir = = "Top" && (Document.documentElement.scrollTop | | document.body.scrollTop) <oldheight) {That.css ({position:"Static"                            }); OldHeight=false; } Else if(dir = = "Bottom" && (Document.documentElement.scrollTop | | document.body.scrollTop) >oldheight) {That.css ({position:"Static"                            }); OldHeight=false;            }                    }                }); } Else{//For IE6$ (window). Scroll (function() {                    if(OldHeight = = =false) {//perform only once before recovery, reducing reflow                        if((getheight () >= 0 && dir = = "Top") | | (getheight () <= 0 && dir = = "Bottom") ) {OldHeight= That.offset (). Top-height; R= Document.createelement ("span"); P= That[0].parentnode; P.replacechild (R, that[0]); Document.body.appendChild (that[0]); that[0].style.position = "Absolute"; }                    } Else if(dir = = "Top" && (Document.documentElement.scrollTop | | document.body.scrollTop) < oldheight) | | (dir = = "Bottom" && (Document.documentElement.scrollTop | | document.body.scrollTop) > OldHeight)) {//EndThat[0].style.position = "Static"; P.replacechild (that[0], R); R=NULL; OldHeight=false; } Else{//scrollingthat.css ({left:l, top:height+Document.documentElement.scrollTop})});    }        }); };}) (jQuery);

Calling methods

$ ("#nav"). Scrollfix ("top");

Non-compliant compact version

varPosition = $ ('. Pub-banner '). Height () + $ ('. Wgt-searchbar '). Outerheight ();//the position of the element above it,$ (window). On (' scroll ',function() {console.log (position); //Console.log ($ (window). scrolltop ());    if($ (window). scrolltop () >position) {        //alert (' 1 ');$ (' #nav '). css ({' Position ': ' Fixed ',            ' Top ': ' 0 '        }); } Else {        $(' #nav '). css ({' Position ': ' Static ',        }); }});

Fixed position with height

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.