JQuery fixed floating Sidebar (jQuery fixed Sidebar), jquerysidebar
This function is now widely used. If the page is relatively high, when the scroll bar is dragged to the bottom of the page, a fixed DIV box following the browser will appear on the sidebar. The idea is as follows: first, obtain the distance from the DIV to be followed to the top of the page, and then determine that when the browser's rolling distance is greater than the distance from the DIV itself to the top, add the CSS attribute 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;
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('inclusidebar'{.css ('margintop'). replace (/auto/, 0 ));
Var footTop = $ ('# footer'). offset (). top-parsefloat('character footer'character .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 {
Certificate ('inclusidebar'mirror.removeclass('fixed'mirror.css ({
Position: 'absolute ',
Top: (maxY-top) + 'px'
});
}
} Else {
$ ('# Sidebar'). removeClass ('fixed ');
}
});
});
From: http://www.111cn.net/wy/jquery/66377.htm
The jquery effect is added, and the previous onclick is invalid.
Indeed, there should be no conflict with jQuery;
You may wish to use $ () to convert it to a jquery object instead of a common var obj object, and then use the jquery object to rewrite the function.
JQUERY issues floating menus
You can use the position: fixed; attribute of css to fix the div in a place, but you cannot roll it to the end and stop it at the end.
Complete code at a fixed position:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "www.w3.org/..al.dtd">
<Html xmlns = "www.w3.org/5o/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> IE6 fixed </title>
<Style type = "text/css">
*
{
Margin: 0px;
Padding: 0px;
}
Body {
_ Background-image: url (about: blank);/* use a blank browser page as the background */
_ Background-attachment: fixed;/* prevent screen flash in IE6 ensure that elements do not flash when the scroll bar is rolling */
}
# TopNav {
Width: 980px;
Z-index: 100;/* Set floating level */
Overflow: visible;
Position: fixed;
Top: 50px;/* locate in other browsers. You can set coordinates here */
_ Position: absolute;/* IE6 simulate fixed with absolute */
_ Top: expression (documentElement. scrollTop + 50 + "px");/* IE6 dynamically sets the top position */
/* DocumentElement. scrollTop always sets the floating element at the top of the browser. You can add a value to achieve the typographical effect */
Background-color: # 0000FF;
Height: 31px;
}
. Show {
Position: absolute;
Top: 500px;
Left: 400px;
Border: # ff0000 1 ...... remaining full text>