Home  >  Article  >  Web Front-end  >  jQuery method to achieve smooth scrolling to the specified anchor point_jquery

jQuery method to achieve smooth scrolling to the specified anchor point_jquery

WBOY
WBOYOriginal
2016-05-16 16:08:191374browse

The example in this article describes how jQuery implements smooth scrolling to the specified anchor point. Share it with everyone for your reference. The details are as follows:

Define the specified anchor point and call the following js code to smoothly scroll the page to the specified position. It is very practical, such as returning to the top of the page, going to the bottom of the page, etc.

// HTML:
// <h1 id="anchor">Lorem Ipsum</h1>
// <p><a href="#anchor" class="topLink">Back to Top</a></p>
$(document).ready(function() {
  $("a.topLink").click(function() {
    $("html, body").animate({
      scrollTop: $($(this).attr("href")).offset().top + "px"
    }, {
      duration: 500,
      easing: "swing"
    });
    return false;
  });
});

I hope this article will be helpful to everyone’s jQuery programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn