JQuery implements side floating navigation menu effect _ jquery

Source: Internet
Author: User
This article mainly introduces jQuery's implementation of the side floating navigation menu effect. if you need a friend, you can refer to the single page with a large page content and a large page length, it is necessary to conveniently and quickly locate different locations on the page, so floating menus gradually become popular, such as men's wear, women's wear, makeup, etc.

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:

The code is as follows:




  • 1F men's wear

  • 2F women's clothing

  • 3F makeup

  • 4F Digital

  • 5F maternal and child




Online shopping


1F men's wear








The 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 ;}

The 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 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, 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 ");
}
});
});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.