JQuery implements smooth scrolling between the same page anchor links

Source: Internet
Author: User

JQuery implements smooth scrolling between the same page anchor links

The front-end of web development has been using JQuery, and it is only possible that JQuery is much more powerful than I thought, or more powerful than I think, in particular, the compatibility is good, so I used all the fun and cool ones that can replace JavaScript.

Introduce the current question from JQuery, and use JQuery to implement smooth scrolling between anchor links. Previously, I introduced a special page anchor jump Buffer Effect implemented by JS. The effect is quite good and smooth scrolling can be achieved between the anchor links of the same page, but the JS Code is relatively lengthy, now, as long as JQuery has been loaded, we can use a relatively short code to achieve the same effect.

The usage is as follows:

1. Load the JQuery library;

2. Key code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

$ (Document). ready (function (){

$ ('A [href * = #] '). click (function (){

If (location. pathname. replace (/^ \/, '') = this. pathname. replace (/^ \ //, '') & location. hostname = this. hostname ){

Var $ target = $ (this. hash );

$ Target = $ target. length & $ target | $ ('[name =' + this. hash. slice (1) + ']');

If ($ target. length ){

Var targetOffset = $ target. offset (). top;

$ ('Html, body'). animate ({

ScrollTop: targetOffset

},

1000 );

Return false;

}

}

});

});

I still need to emphasize the loading sequence. I will reference the JQuery Class Library first. By the way, it has been tested that the rolling effect is compatible and applicable in various browsers. It is only a little weird in Opera and remains to be improved.

Related Article

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.