Jquery enables webpage positioning and navigation

Source: Internet
Author: User

Jquery enables webpage positioning and navigation

I don't know if you have noticed that some webpages use the page positioning and navigation to redirect due to the huge page information. Click a page next to the menu to jump to the corresponding content, in addition, when the scroll bar is rolled to a specific item, the item corresponding to the next menu is highlighted. Today I will talk about how this effect is achieved.

First paste the display effect:



Implementation:
The page layout is very simple. If you try more, you will be OK, it is worth noting that you need to add the label to each li in the menu layout and assign the corresponding id number to the href of the a label, so that it can click to jump to the corresponding content of the id immediately.

Then there is how jquery implements it:

1. We need to use $ (document). scrollTop () to get the height of the scroll bar relative to the top.

2. by $ ('# content '). find ('. item ') [index]. offset (). top: Get the height of each piece of content relative to the top. (In the div with id as content, five class as item, each item corresponds to the content of one section)

3. then compare the $ (document) of each section ). whether scrollTop () is greater than $ ('# content '). find ('. item ') [index]. offset (). top. If the value is greater than that, the page has already come to the corresponding section.

4. Remove the previous highlight using removeClass and add addClass to the corresponding item.

Code:

Below is a specific demo I wrote for your reference:
Index.html

<! DOCTYPE html> 

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.