Home >Web Front-end >JS Tutorial >Javascript implementation of navigation anchor scrolling effect example

Javascript implementation of navigation anchor scrolling effect example

零下一度
零下一度Original
2017-05-08 10:29:013686browse

This article mainly introducesJavascriptimplementation of intelligent positioning when scrolling the pageNavigation, which has certain reference value. Interested friends can refer to it

Common development pages may have such a requirement. There will be multiple modules in the page, and each module corresponds to a navigation. When the page scrolls to a certain module, the corresponding module navigation needs to be added. A class used to distinguish the area that the current user is browsing.

Assume the structure is as follows:

<p class="container">
  <p class="wrapper">
    <p class="section" id="section1">section1</p>
    <p class="section" id="section2">section2</p>
    <p class="section" id="section3">section3</p>
    <p class="section" id="section4">section4</p>
    <p class="section" id="section5">section5</p>
  </p>
  <nav>
    <a href="#section1" rel="external nofollow" class="current">section1</a>
    <a href="#section2" rel="external nofollow" >section2</a>
    <a href="#section3" rel="external nofollow" >section3</a>
    <a href="#section4" rel="external nofollow" >section4</a>
    <a href="#section5" rel="external nofollow" >section5</a>
  </nav>
</p>

Navigation positioning when the page scrolls

jsThe code is as follows:

var $navs = $(&#39;nav a&#39;),          // 导航
  $sections = $(&#39;.section&#39;),       // 模块
  $window = $(window),
  navLength = $navs.length - 1;
  
$window.on(&#39;scroll&#39;, function() {
  var scrollTop = $window.scrollTop(),
    len = navLength;

  for (; len > -1; len--) {
    var that = $sections.eq(len);
    if (scrollTop >= that.offset().top) {
       $navs.removeClass(&#39;current&#39;).eq(len).addClass(&#39;current&#39;);
       break;
    }
  }
});

The effect is as follows:

It is not difficult to see that the basic principle is to traverse the modules from back to front when the window is scrolling. If the scroll height of the window is greater than or equal to the distance of the current module from the top of the page, then the navigation corresponding to the current module will be highlighted and the traversal will no longer continue

Click the navigation to locate the page

In addition to this requirement, there is another requirement, which is to click on the navigation to locate the top of the module corresponding to the navigation.

The code is as follows:

$navs.on(&#39;click&#39;, function(e) {
  e.preventDefault();
  $(&#39;html, body&#39;).animate({
    &#39;scrollTop&#39;: $($(this).attr(&#39;href&#39;)).offset().top
  }, 400);
});

The effect is as follows:

##[Related recommendations]

1.

Free js online video tutorial

2.

JavaScript Chinese Reference Manual

3.

php.cnDugu Jiujian (3)-JavaScript Video Tutorial

The above is the detailed content of Javascript implementation of navigation anchor scrolling effect example. For more information, please follow other related articles on the PHP Chinese website!

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