How to scroll HTML page to given anchor?

后端 未结 17 1960
醉梦人生
醉梦人生 2020-11-22 08:55

I’d like to make the browser to scroll the page to a given anchor, just by using JavaScript.

I have specified a name or id attribute in my

17条回答
  •  温柔的废话
    2020-11-22 09:10

    This works:

    $('.scroll').on("click", function(e) {
    
      e.preventDefault();
    
      var dest = $(this).attr("href");
    
      $("html, body").animate({
    
        'scrollTop':   $(dest).offset().top
    
      }, 2000);
    
    });
    

    https://jsfiddle.net/68pnkfgd/

    Just add the class 'scroll' to any links you wish to animate

提交回复
热议问题