Single page page content more, page length is larger, need convenient and fast in the different position of the page to locate, so the floating menu gradually became popular, the following figure men, women, beauty makeup and so on.
This menu function is divided into two parts:
1, click the menu item, the page scrolling to the corresponding position, can be simply achieved through the anchor point;
2. When scrolling the page, the selected state of the menu item must follow the change, this needs to monitor the scrolling event of the webpage and realize by a little computation;
Calculates the size relationship of the scrolltop and the offsettop of each Div, determines where the page is now displayed, and then adds a style to the corresponding menu item based on the results of the calculation. For example, the second Div's offset (). top = 300, offset () of the third Div (). top = 600, at this point, ScrollTop = 400, which shows that most of the present is the position of the second Div, and 700 is the third Div. The following is a simple example:
Copy Code code as follows:
<div id= "Menu" >
<ul>
<li><a href= "#item1" class= "current" >1f menswear </a></li>
<li><a href= "#item2" >2f women </a></li>
<li><a href= "#item3" >3f Beauty makeup </a></li>
<li><a href= "#item4" >4f digital </a></li>
<li><a href= "#item5" >5f maternal </a></li>
</ul>
</div>
<div id= "Content" >
<div id= "item1" class= "Item" >
<ul>
<li><a href= "#" ></a></li>"
<!--several Li-->
</ul>
</div>
<!--a number of item-->
</div>
Copy Code code as follows:
* {margin:0;padding:0;}
body {font-size:12px;line-height:1.7;}
Li {list-style:none;}
#content {width:800px;margin:0 auto;padding:20px;}
#content H1 {color: #0088bb;}
#content. Item {padding:20px;margin-bottom:20px;border:1px dotted #0088bb;}
#content. Item H2 {font-size:16px;font-weight:bold;border-bottom:2px solid #0088bb; margin-bottom:10px;}
#content. Item Li {display:inline;margin-right:10px;}
#content. Item Li a img {width:230px;height:230px;border:none;}
#menu {position:fixed;left:50%;margin-left:400px;top:100px;}
#menu ul Li a {
Display:block;
margin:5px 0;
font-size:14px;
Font-weight:bold;
Color: #333;
width:80px;
height:50px;
line-height:50px;
Text-decoration:none;
Text-align:center;
}
#menu ul Li A:hover, #menu ul li a.current {color: #fff; background: #0088bb;}
Copy Code code as follows:
$ (function () {
$ (window). Scroll (function () {
var scrolltop = $ (document). ScrollTop ();
var Contentitems = $ ("#content"). Find (". Item");
var currentitem = "";
Contentitems.each (function () {
var ContentItem = $ (this);
var offsettop = Contentitem.offset (). Top;
if (ScrollTop > offsetTop-200) {//Here 200 is set to the specific situation, because if you do not subtract a value, the menu will go wrong when it is just scrolling to the edge of a div, for example, when the page just scrolls to the bottom of the first Div. , the page has a second Div displayed, and the first option is selected in the menu
CurrentItem = "#" + contentitem.attr ("id");
}
});
if (currentitem&¤titem!=$ ("#menu"). Find (". Current"). attr ("href")) {
$ ("#menu"). Find ('. Current '). Removeclass ("current");
$ ("#menu"). Find ("[href=" + CurrentItem + "]"). AddClass ("current");
}
});
});