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:
<div id= "menu" > <ul> <li><a href= "#item1" class= "current" >1f Menswear </a></li> <li><a href= "#item2" >2f Women's wear </a></li> <li><a href= "#item3" >3f Beauty </a></li> <li><a href= "#item4" >4f digital </a></li> <li><a href= "# ITEM5 ">5f maternal </a></li> </ul> </div> <div id=" Content ">
* {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. ite M li {display:inline;margin-right:10px} #content. Item Li a img {width:230px;height:230px;border:none} #menu {Positi
on: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;}
$ (function () {$ (window). Scroll (function () {var scrolltop = $ (document). ScrollTop (); var c
Ontentitems = $ ("#content"). Find (". Item");
var currentitem = ""; Contentitems.each (function () {var ContentItem = $ (this), var offsettop = Contentitem.offset (). Top; if (ScrollTop > Offs ETTOP-200) {//Here 200 depends on the specific circumstances, because if you do not subtract a value, in just scrolling to the edge of a div, the menu will be checked state error, for example, the page just scroll to the bottom of the first Div, the page has shown a second Div,
And the first option in the menu is selected CurrentItem = "#" + contentitem.attr ("id");
}
}); if (currentitem&¤titem!=$ ("#menu"). Find ('. Current '). attr ("href")) {$ ("#menu"). Find (". Current").
Removeclass ("current");
$ ("#menu"). Find ("[href=" + CurrentItem + "]"). AddClass ("current");
}
});
});