jquery fixed floating sidebar (jquery fixed Sidebar)

Source: Internet
Author: User

This function is now widely used, if the page is relatively high, when the scroll bar dragged down to the bottom of the page, the sidebar will appear a fixed following the browser Div box, is the idea is this: first get the div need to follow the distance from the top of the page, and then judge, When the browser scrolls more than the distance from the top of the div itself, add the CSS property fixed.

This function is now widely used, if the page is relatively high, when the scroll bar dragged down to the bottom of the page, the sidebar will appear a fixed following the browser Div box, is the idea is this: first get the div need to follow the distance from the top of the page, and then judge, When the browser scrolls more than the distance from the top of the div itself, add the CSS property fixed.

The code is as follows

HTML code:

<div id= "Header" >header</div>
<div id= "Sidebarwrap" >
<div id= "sidebar" >Sidebar</div>
</div>
<div id= "main" >Main</div>
<div id= "Footer" >footer</div>

CSS code:
Body {
    margin:10px auto;
    Font-family:sans-serif;
    width:500px;
}
Div {
    border-radius:5px
    box-shadow:1px 2px 5px rgba (0,0,0,0.3) ;
    border:1px solid #ccc;
    padding:5px;
}
#sidebarWrap {
    height:400px;
    width:210px
  & nbsp Float:right;
    position:relative;
    Box-shadow:none;
    Border:none;
    margin:0;
    padding:0;
}
#main {
    width:270px;
    height:4000px;
}
#footer {
    clear:both
    margin:10px 0;
}
#sidebar {
    width:200px
    height:300px;
    Positionabsolute;
}
#header {
    height:200px;
    margin-bottom:10px;
}
#sidebar. Fixed {
    position:fixed
    top:0;
}
#footer {height:600px;}
#footer {height:600px;}

JavaScript code:

$ (function () {
var top = $ (' #sidebar '). Offset (). Top-parsefloat ($ (' #sidebar '). CSS (' margintop '). Replace (/auto/, 0));
var foottop = $ (' #footer '). Offset (). Top-parsefloat ($ (' #footer '). CSS (' margintop '). Replace (/auto/, 0));

var Maxy = foottop-$ (' #sidebar '). Outerheight ();

    $ (window). Scroll (function (evt) {
        var y = $ (this) . scrolltop ();
        if (Y > top) {
             if (Y < Maxy) {
                 $ (' #sidebar '). addclass (' fixed '). Removeattr (' style ');
           } else {
                 $ (' #sidebar '). Removeclass (' fixed '). css ({
                     Position: ' absolute ',
                     Top: (maxy-top) + ' px '
                });
           }
       } else {
             $ (' #sidebar '). Removeclass (' 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.