Scroll back to the top of scrollable div

后端 未结 15 1273
独厮守ぢ
独厮守ぢ 2020-12-07 14:02
#containerDiv          


        
相关标签:
15条回答
  • 2020-12-07 14:03

    As per François Noël's answer "For those who still can't make this work, make sure that the overflowed element is displayed before using the jQuery function."

    I had been working in a bootstrap modal that I repeatedly bring up with account permissions in a div that overflows on the y dimension. My problem was, I was trying to use the jquery .scrollTop(0) function and it would not work no matter how I tried to do it. I had to setup an event for the modal that didn't reset the scrollbar until the modal had stopped animating. The code that finally worked for me is here:

        $('#add-modal').on('shown.bs.modal', function (e) {
            $('div.border').scrollTop(0);
        });
    
    0 讨论(0)
  • 2020-12-07 14:03

    these two codes worked for me like a charm this first will take to scroll to the top of the page but if you want to scroll to some specific div use the second one with your div id.

    $('body, html, #containerDiv').scrollTop(0);
    document.getElementById('yourDivID').scrollIntoView();
    

    If you want to scroll to by a class name use the below code

    var $container = $("html,body");
    var $scrollTo = $('.main-content');
    
    $container.animate({scrollTop: $scrollTo.offset().top - $container.offset().top + $container.scrollTop(), scrollLeft: 0},300);
    
    0 讨论(0)
  • 2020-12-07 14:05

    If the html content overflow a single viewport, this worked for me using only javascript:

    document.getElementsByTagName('body')[0].scrollTop = 0;
    

    Regards,

    0 讨论(0)
  • 2020-12-07 14:06

    For me the scrollTop way did not work, but I found other:

    element.style.display = 'none';
    setTimeout(function() { element.style.display = 'block' }, 100);
    

    Did not check the minimum time for reliable css rendering though, 100ms might be overkill.

    0 讨论(0)
  • 2020-12-07 14:10

    scrollTo

    window.scrollTo(0, 0);
    

    is the ultimate solution for scrolling the windows to the top - the best part is that it does not require any id selector and even if we use the IFRAME structure it will work extremely well.

    The scrollTo() method scrolls the document to the specified coordinates.
    window.scrollTo(xpos, ypos);
    xpos Number Required. The coordinate to scroll to, along the x-axis (horizontal), in pixels
    ypos Number Required. The coordinate to scroll to, along the y-axis (vertical), in pixels

    jQuery

    Another option to do the same is using jQuery and it will give a smoother look for the same

    $('html,body').animate({scrollTop: 0}, 100);
    

    where 0 after the scrollTop specifies the vertical scrollbar position in the pixel and second parameter is an optional parameter which shows the time in microseconds to complete the task.

    0 讨论(0)
  • 2020-12-07 14:11

    If you want to scroll with a smooth transition, you could use this!

    (Vanilla JS)

    const tabScroll = document.getElementById("tabScroll");
    window.scrollTo({
      'behavior': 'smooth',
      'left': 0,
      'top': tabScroll.offsetTop - 80
    });
    

    If your target users are Chrome and Firefox, then this is good! But unfortunately this method isn’t supported well enough on all browsers. Check Here

    Hope this helps!!

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