How to scroll HTML page to given anchor?

后端 未结 17 1778
醉梦人生
醉梦人生 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:09
    function scrollTo(hash) {
        location.hash = "#" + hash;
    }
    

    No jQuery required at all!

    0 讨论(0)
  • 2020-11-22 09:10

    Great solution by jAndy, but the smooth scroll seems to be having issues working in firefox.

    Writing it this way works in Firefox as well.

    (function($) {
        $(document).ready(function() {
             $('html, body').animate({
               'scrollTop':   $('#anchorName2').offset().top
             }, 2000);
        });
    })(jQuery);
    
    0 讨论(0)
  • 2020-11-22 09:10

    A pure javascript solution without JQuery. Tested on Chrome & I.e, not tested on IOS

    function ScrollTo(name) {
      ScrollToResolver(document.getElementById(name));
    }
    
    function ScrollToResolver(elem) {
      var jump = parseInt(elem.getBoundingClientRect().top * .2);
      document.body.scrollTop += jump;
      document.documentElement.scrollTop += jump;
      if (!elem.lastjump || elem.lastjump > Math.abs(jump)) {
        elem.lastjump = Math.abs(jump);
        setTimeout(function() { ScrollToResolver(elem);}, "100");
      } else {
        elem.lastjump = null;
      }
    }
    

    demo: https://jsfiddle.net/jd7q25hg/12/

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-22 09:13

    Way simpler:

    var element_to_scroll_to = document.getElementById('anchorName2');
    // Or:
    var element_to_scroll_to = document.querySelectorAll('.my-element-class')[0];
    // Or:
    var element_to_scroll_to = $('.my-element-class')[0];
    // Basically `element_to_scroll_to` just have to be a reference
    // to any DOM element present on the page
    // Then:
    element_to_scroll_to.scrollIntoView();
    
    0 讨论(0)
  • 2020-11-22 09:13

    a vue2 solution ... add simple data property to simply force the update

      const app = new Vue({ 
      ... 
    
      , updated: function() {
               this.$nextTick(function() {
               var uri = window.location.href
               var anchor = ( uri.indexOf('#') === -1 ) ? '' : uri.split('#')[1]
               if ( String(anchor).length > 0 && this.updater === 'page_load' ) {
                  this.updater = "" // only on page-load !
                  location.href = "#"+String(anchor)
               }
             })
            }
         });
         app.updater = "page_load"
    
     /* smooth scrolling in css - works in html5 only */
     html, body {
         scroll-behavior: smooth;
     }
    
    0 讨论(0)
提交回复
热议问题