JQuery realizes side-side floating navigation menu effect _jquery

Source: Internet
Author: User

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&&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.