JQuery implements the floating navigation menu on the side, and jquery floating navigation menu
The content of a single page is large, and the page length is large, so it is convenient to quickly locate the page in different locations. As a result, floating menus are becoming popular, such as men's wear, women's wear, and beauty.
This menu function is divided into two parts:
1. Click the menu item and scroll the webpage to the corresponding position, which can be implemented simply by the anchor;
2. When scrolling the page, the selected menu item status should change accordingly. This requires listening to the page rolling events and performing some calculations;
Calculate the relationship between scrollTop and offsetTop of each div, determine where the current webpage is displayed, and add a style to the corresponding menu item based on the calculation result. For example, the offset () of the second div (). top = 300, the offset () of the third div (). top = 600, scrollTop = 400 at this time, indicating that most of the displayed position is the second div, and 700 is the third div. The following is a simple example:
Copy codeThe Code is as follows:
<Div id = "menu">
<Ul>
<Li> <a href = "# item1" class = "current"> 1F men's wear </a> </li>
<Li> <a href = "# item2"> 2F women's clothing </a> </li>
<Li> <a href = "# item3"> 3F makeup </a> </li>
<Li> <a href = "# item4"> 4F digital </a> </li>
<Li> <a href = "# item5"> 5F maternal and child </a> </li>
</Ul>
</Div>
<Div id = "content">
<H1> online shopping
<Div id = "item1" class = "item">
<H2> 1F men's wear <Ul>
<Li> <a href = "#"> </a> </li>
<! -- Several li -->
</Ul>
</Div>
<! -- Several items -->
</Div>
Copy codeThe Code is 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 {
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 codeThe Code is 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 based on the actual situation, because if you do not subtract a value, just scroll to the edge of a div, an error occurs when the menu is selected. For example, when the page just scrolls to the bottom of the first div, the second div is displayed on the page, and the first option in the menu is selected.
CurrentItem = "#" + contentItem. attr ("id ");
}
});
If (currentItem & currentItem! = $ ("# Menu"). find (". current"). attr ("href ")){
$ ("# Menu"). find (". current"). removeClass ("current ");
$ ("# Menu"). find ("[href =" + currentItem + "]"). addClass ("current ");
}
});
});