How to scroll HTML page to given anchor?

后端 未结 17 1779
醉梦人生
醉梦人生 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:16
    $(document).ready ->
      $("a[href^='#']").click ->
        $(document.body).animate
          scrollTop: $($(this).attr("href")).offset().top, 1000
    
    0 讨论(0)
  • 2020-11-22 09:17

    You can use jQuerys .animate(), .offset() and scrollTop. Like

    $(document.body).animate({
        'scrollTop':   $('#anchorName2').offset().top
    }, 2000);
    

    example link: http://jsbin.com/unasi3/edit

    If you don't want to animate use .scrollTop() like

    $(document.body).scrollTop($('#anchorName2').offset().top);
    

    or javascripts native location.hash like

    location.hash = '#' + anchorid;
    
    0 讨论(0)
  • 2020-11-22 09:23

    jQuery("a[href^='#']").click(function(){
        jQuery('html, body').animate({
            scrollTop: jQuery( jQuery(this).attr('href') ).offset().top
        }, 1000);
        return false;
    });

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

    2018-2020 Pure js:

    There is a very convenient way to scroll to the element:

    el.scrollIntoView({
      behavior: 'smooth', // smooth scroll
      block: 'start' // the upper border of the element will be aligned at the top of the visible part of the window of the scrollable area.
    })
    

    But as far as I understand he does not have such good support as the options below.

    Learn more about the method.


    If it is necessary that the element is in the top:

    const element = document.querySelector('#element')
    const topPos = element.getBoundingClientRect().top + window.pageYOffset
    
    window.scrollTo({
      top: topPos, // scroll so that the element is at the top of the view
      behavior: 'smooth' // smooth scroll
    })
    

    Demonstration example on Codepen


    If you want the element to be in the center:

    const element = document.querySelector('#element')
    const rect = element.getBoundingClientRect() // get rects(width, height, top, etc)
    const viewHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
    
    window.scroll({
      top: rect.top + rect.height / 2 - viewHeight / 2,
      behavior: 'smooth' // smooth scroll
    });
    

    Demonstration example on Codepen


    Support:

    They write that scroll is the same method as scrollTo, but support shows better in scrollTo.

    More about the method

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

    This is a working script that will scroll the page to the anchor. To setup just give the anchor link an id that matches the name attribute of the anchor that you want to scroll to.

    <script>
    jQuery(document).ready(function ($){ 
     $('a').click(function (){ 
      var id = $(this).attr('id');
      console.log(id);
      if ( id == 'cet' || id == 'protein' ) {
       $('html, body').animate({ scrollTop: $('[name="' + id + '"]').offset().top}, 'slow'); 
      }
     }); 
    });
    </script>
    
    0 讨论(0)
提交回复
热议问题