The page length is large and needs to be easily and quickly located in different locations on the page, so the floating menu is becoming popular, the following shows the jQuery floating navigation menu, which is suitable for shopping websites with a large number of page content and a large page length. It is convenient to quickly locate the page in different locations, as a result, floating menus gradually become 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 the offsetTop of each p, determine where the current page is displayed, and add a style to the corresponding menu item based on the calculation result. For example, offset () of the second p (). top = 300, the offset () of the third p (). top = 600, scrollTop = 400 at this time, indicating that most of the displayed position is the second p, and 700 is the third p. The following is a simple example:
- 1F men's wear
- 2F women's clothing
- 3F makeup
- 4F digital
- 5F maternal and child
Online Shopping
1F men's wear
* {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;}
$ (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 p, an error occurs when the menu is selected. For example, when the page just scrolls to the bottom of the first p, the second p is displayed on the page, currentItem = "#" + c OntentItem. attr ("id") ;}}); if (currentItem & currentItem! = $ ("# Menu "). find (". current "). attr ("href") {$ ("# menu "). find (". current "). removeClass ("current"); $ ("# menu "). find ("[href =" + currentItem + "]"). addClass ("current ");}});});