How to scroll to top of a div using jQuery?

后端 未结 7 1589
耶瑟儿~
耶瑟儿~ 2020-12-23 17:43

I have a gridview inside a div.. I want to scroll to top of the div from the bottom of the div using jquery.. Any suggestion..

//
相关标签:
7条回答
  • 2020-12-23 17:55

    Use the following function

    window.scrollTo(xpos, ypos)
    

    Here xpos is Required. The coordinate to scroll to, along the x-axis (horizontal), in pixels

    ypos is also Required. The coordinate to scroll to, along the y-axis (vertical), in pixels

    0 讨论(0)
  • 2020-12-23 17:57

    This is my solution to scroll to the top on a button click.

    $(".btn").click(function () {
    if ($(this).text() == "Show options") {
    $(".tabs").animate(
      {
        scrollTop: $(window).scrollTop(0)
      },
      "slow"
     );
     }
    });
    
    0 讨论(0)
  • 2020-12-23 17:58

    Special thanks to Stoic for

       $("#miscCategory").animate({scrollTop: $("#miscCategory").offset().top});
    
    0 讨论(0)
  • 2020-12-23 18:02

    You could just use:

    <div id="GridDiv">
    // gridview inside...
    </div>
    
    <a href="#GridDiv">Scroll to top</a>
    
    0 讨论(0)
  • 2020-12-23 18:08

    Here is what you can do using jquery:

    $('#A_ID').click(function (e) { //#A_ID is an example. Use the id of your Anchor
        $('html, body').animate({
            scrollTop: $('#DIV_ID').offset().top - 20 //#DIV_ID is an example. Use the id of your destination on the page
        }, 'slow');
    });
    
    0 讨论(0)
  • 2020-12-23 18:08

    I don't know why but you have to add a setTimeout with at least for me 200ms:

    setTimeout( function() {$("#DIV_ID").scrollTop(0)}, 200 );
    

    Tested with Firefox / Chrome / Edge.

    0 讨论(0)
提交回复
热议问题