Jquery Div Scroll to top of browser position fixed

Source: Internet
Author: User

Gets the element (here positioned element a) from the top of the height, and then sets the scroll scrolling event, such as exceeding that height, the position of a is set to fixed, less than the height, modified back to relative.

Method One:

$ (function() {     var elm = $ ('. Nav ');      var startpos = $ (elm). Offset (). Top;      function () {         var p = $ (window). scrolltop ();         $ (Elm). css (' position ', ((P) > Startpos)? ' Fixed ': ' Static ');         $ (Elm). CSS (' Top ', ((P) > Startpos)? ' 0px ': ');     

Method Two:

$(function(){//gets the distance to locate the element from the top of the browservarNAVH = $ (". Nav")). Offset (). Top;//scroll bar Events$ (window). Scroll (function(){//get the sliding distance of the scroll barvarScroh = $ ( This). scrolltop ();//the sliding distance of the scrollbar is greater than or equal to the distance of the anchor element from the top of the browser, fixed, and not fixedif(scroh>=NAVH) {$(". Nav"). css ({"Position": "Fixed", "top": 0});}Else if(SCROH
    ". Nav"). css ({"position": "Static"});}})})

Cases:

Article from: http://blog.sina.com.cn/s/blog_3eba8f1c0101k94s.html

Jquery Div Scroll to top of browser position fixed

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.