jquery enables navigation to be automatically fixed at the top of the screen outside the display range _jquery

Source: Internet
Author: User
In fact, it is not difficult to achieve, we first the implementation process to think about, first of all, if the navigation in the scope of the display, you do not have to make changes. When the navigation is out of range, that is, the distance from the top of the screen is less than 0, we have to float it on the top of the screen, and then more than 0, to do a recovery operation, the principle is so simple, roughly look at the effect of the picture

Copy Code code as follows:

$ (). Ready (function () {
Navigation distance from top of screen
var _defautltop = $ ("#navigator"). Offset (). Top-$ (window). scrolltop ();
Navigation distance from the left side of the screen
var _defautlleft = $ ("#navigator"). Offset (). Left-$ (window). scrollleft ();
Navigating default Style records, restoring the initial style requires
var _position = $ ("#navigator"). CSS (' position ');
var _top = $ ("#navigator"). CSS (' top ');
var _left = $ ("#navigator"). CSS (' left ');
var _zindex = $ ("#navigator"). CSS (' Z-index ');
Mouse scrolling Events
$ (window). Scroll (function () {
if ($ (this). ScrollTop () > _defautltop) {
IE6 do not know position:fixed, alone with Position:absolute simulation
if ($.browser.msie && $.browser.version== "6.0") {
$ ("#top"). css ({' position ': ' absolute ', ' top ': eval (document.documentElement.scrollTop), ' left ': _defautlleft, ' Z-index ': 99999});
Prevent Jitter from appearing
$ ("Html,body"). css ({' background-image ': ' url (about:blank) ', ' background-attachment ': ' Fixed '});
}else{
$ ("#navigator"). css ({' position ': ' fixed ', ' top ': 0, ' left ': _defautlleft, ' Z-index ': 99999});
}
}else{
$ ("#navigator"). css ({' position ': _position, ' top ': _top, ' left ': _left, ' Z-index ': _zindex});
}
});
});

Not too much to speak of, need to pay attention to IS, IE6 don't know position:fixed, need to use Position:absolute to simulate, and then calculate top value in real time, also need to add two styles to HTML and body, prevent rolling when jitter occurs, Specific to understand the "perfect solution IE6 does not support the position:fixed bug."

Also note that the width of the navigation must be fixed value, not auto or 100% because fixed and absolute do not know, of course, you can also manually get the width of the navigation, and then write to the floating navigation style, but there is a prerequisite, The original style of navigation can not be: position:relative, the situation may be more, the simplest way is to set the navigation width dead.

The above code can be copied to the background settings of the HTML footer code, if encountered floating navigation width problems, refer to the treatment I have just done to solve it.

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.