【问题标题】:Hyperlink scroll down screen on click [duplicate]单击时超链接向下滚动屏幕[重复]
【发布时间】:2016-02-24 14:37:28
【问题描述】:

我正在我的网站上创建一个简单的链接:http://jsfiddle.net/xserjaqx/

我希望用户单击链接并优雅地导航到灰色框,而不是通常的突然移动。

如何使用 jQuery 来做到这一点?

#hello {
    margin-top:200px;
    padding:40px;
    background:#eaeaea
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<a href="#hello">Click me and I'll move you down slowly</a>

<div id="hello">
    
    Hello!
    
</div>

【问题讨论】:

  • 根据经验,链接(锚点)用于从页面导航,按钮用于处理页面上的操作。

标签: javascript jquery html css


【解决方案1】:

您可以使用本页描述的 jQuery 技术:https://css-tricks.com/snippets/jquery/smooth-scrolling/

将此添加到您的主 javacript 文件中:

$(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
        }, 1000);
        return false;
      }
    }
  });
});

【讨论】:

  • 哇!这是一种享受。在此处更新小提琴:jsfiddle.net/xserjaqx/1 Question: 我如何将其应用于 某些 链接,而不是所有链接?
  • 在链接上添加smoothclass,并将jQuery选择器修改为$('a[href*=#]:not([href=#]).smooth')
【解决方案2】:

如果您想要更慢的滚动,只需将 1000 位更改为 1000 位以上

$('a').click(function(){
    $('html, body').animate({
        scrollTop: $( $(this).attr('href') ).offset().top
    },1000);
    return false;
});
<a href="#myAnchor" rel="" id="anchor1" class="anchorLink">Link to the anchor</a>

    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

    Why do we use it?</p>



<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>

 

<p>Where does it come from?</p>



<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>



<p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>

    

<p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>



<p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>





<p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>



<a name="myAnchor" id="myAnchor"><strong>Where i want to go</strong></a>

<p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p><p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>    

    

    <p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>

        <p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>

        <p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>

        <p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>

        <p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>

    

    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

    Why do we use it?</p>

    

    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

    Why do we use it?</p>

    

    

    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

    Why do we use it?</p>

    

    

    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

    Why do we use it?</p>

    

    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

    Why do we use it?</p>

    

【讨论】:

    【解决方案3】:

    我想贡献一个非 jQuery 版本:

    const EASING = BezierEasing(0, .1, .1, 1); // Example values.
    const DURATION = 1000; // ms.
    
    document.addEventListener("DOMContentLoaded", 
      () =>  {
        document.querySelector('#foo')
          .addEventListener('click', e => {
            scroll(400); // px.
            e.preventDefault();
          }, false);
    });
    
    function scroll(distance) {
      var start = performance.now();
      var travelled = 0;
      var html = document.querySelector('html');
      
      (function move(now) {
        var fractionDone, dy, toMove;
        
        fractionDone = (now - start) / DURATION;
    
        if((1 - fractionDone) <= 0) {
          window.scrollBy(0, (distance - travelled)); // Scroll any remaining distance.
          return; // Done!
        }
        
        if(window.scrollY + window.innerHeight 
          === html.offsetHeight) {
          return; // At bottom of page.
        }
    
        dy = ((EASING.get(fractionDone)) * distance);
        toMove = Math.floor((dy - travelled)); // `scrollBy` only accepts integers.
        
        if(toMove > 0) {
          window.scrollBy(0, toMove);
          travelled += toMove;
        }
        
        requestAnimationFrame(move);
      }(start));
    } 
    <!DOCTYPE html>
    <html>
      <head>
        <script src="https://rawgit.com/gre/bezier-easing/master/build.js"></script>
      </head>
      <body>
        <a href id="foo">Click Me!</a>
        <script>
          /* print some numbers to
               the DOM to help visualize 
               the scrolling */
          var body = document.querySelector('body');
          for(var x = 0; x < 50; x++) {  
            var div = document.createElement("div"); 
            var text = document.createTextNode(x);
            div.appendChild(text);
            body.appendChild(div);
          }
        </script>
      </body> 
    </html>

    【讨论】:

      猜你喜欢
      • 2020-03-23
      • 2013-05-02
      • 2014-01-20
      • 2015-09-08
      • 2012-10-27
      • 2014-04-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      相关资源
      最近更新 更多