平滑滚动代码



具有以下代码结构,需要什么代码才能以流畅的动画方式进行滚动?

我在这里保存了一个JSFiddle:https://jsfiddle.net/v7qhzo8q/

代码为:

<nav id="nav">
 <ul id="top">
    <li><a href="#">Home</a></li>
    <li><a href="#vinedo">Viñedo</a></li> 
    <li><a href="#elaboracion">Elaboracion</a></li>
    <li><a href="#contacto">Contacto</a></li>
 </ul>
</nav>
<div id='vinedo' class="div1">
  Vinedo DIV
  <a href="#top">Back to top!</a>
</div>
<div id="elaboracion" class="div2">
  Elaboracion DIV
  <a href="#top">Back to top!</a>
</div>
<div id="contacto" class="div3">
  Contacto DIV
  <a href="#top">Back to top!</a>
</div>

.CSS:

.div1 {background:red;height:1000px;}
.div2 {background:green;height:1000px;}
.div3 {background:blue;height:1000px;}

试一试。 如果我正确理解你,它应该做你想做的事。 要更改滚动所需的时间,只需更改返回 false 代码之前的数字即可。 它当前设置为 600 毫秒。

    $(function() {
      $('a[href*="#"]:not([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) {
            $('html, body').animate({
              scrollTop: target.offset().top
            }, 600);  //change this number here to modify how long the scroll takes
            return false;
          }
        }
      });
    });
嗨,

为了易于使用的平滑滚动,请使用以下链接,并按照自述文件中的说明进行设置,希望您是正确的。网站的平滑滚动

最新更新