Scroll to a specific div

前端 未结 5 718
猫巷女王i
猫巷女王i 2021-01-19 00:44

I have few divs .posts which have a attr data-id which corresponds to the mysql DB id.

5条回答
  •  悲哀的现实
    2021-01-19 00:57

    You use link anchors and JQuery. Just give your link the class "scroll" and use the following code in the head:

    $(function() {
      // Listen for a click event on the anchor
      $('.scroll').click(function(event) {
      
        // Prevent the jump to target that is default browser behavior
        event.preventDefault();
        
        // Animate the scrollTop property of the scrollParent to the top offset 
        // of the target element. In this case, we have an animation duration of 1000ms(1 second).
        $('html').animate({
          scrollTop: $(this.hash).offset().top
        }, 1000);
      });
    });
    /* Just for demo purposes */
    .post {
      margin: 100vh 0;
      background: yellow;
    }
    
    
    Go To Div 8
    
    Scroll to me

提交回复
热议问题