jquery fixed floating sidebar (jquery fixed Sidebar)

Source: Internet
Author: User

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

Code to copy code 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;
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;
Position:absolute;
}
#header {
height:200px;
Marg (www.111cn.net) in-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 ');
}
});
});

From:http://www.111cn.net/wy/jquery/66377.htm

jquery fixed floating sidebar (jquery fixed Sidebar)

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.