Scroll back to the top of scrollable div

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


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

    2020 UPDATE

    You can use .scroll() to easily scroll elements or window. It has a built-in smooth scroll effect so basically the code couldn't be simpler.

    Standard properties:

    var options = {
        top:       0,        // Number of pixels along the Y axis to scroll the window or element
        left:      0,        // Number of pixels along the X axis to scroll the window or element.
        behavior:  'smooth'  // ('smooth'|'auto') - animate smoothly, or move in a single jump
    }
    

    DOCS: https://developer.mozilla.org/en-US/docs/Web/API/Window/scroll

    SEE ALSO: .scrollIntoView() https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView


    DEMO:

    document.getElementById('btn').addEventListener('click',function(){
    
      document.getElementById('container').scroll({top:0,behavior:'smooth'});
      
    });
    /*DEMO*/
    #container{
      width:300px;
      max-height:300px;
      padding:1rem;
      margin-left:auto;
      margin-right:auto;
      background-color:#222;
      color:#ccc;
      text-align:justify;
      overflow-y:auto;
    }
    #btn{
      width:100%;
      margin-top:1rem;
    }
    <div id="container">
      <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
      <button id="btn">Scroll to top</button>
    </div>

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

    This is the only way it worked for me, with smooth scrolling transition:

      $('html, body').animate({
        scrollTop: $('#containerDiv').offset().top,
      }, 250);
    
    0 讨论(0)
  • 2020-12-07 14:17

    This worked for me :

    document.getElementById('yourDivID').scrollIntoView();
    
    0 讨论(0)
  • 2020-12-07 14:17

    When getting element by class name, don't forget the return value is an array; Hence this code:

    document.getElementByClassName("dropdown-menu").scrollTop = 0
    

    Would not work. Use code below instead.

    document.getElementByClassName("dropdown-menu")[0].scrollTop = 0
    

    I figured other people might encounter a similar problem as I did; so this should do the trick.

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

    I tried the existing answers to this question, and none of them worked on Chrome for me. What did work was slightly different:

    $('body, html, #containerDiv').scrollTop(0);
    
    0 讨论(0)
  • 2020-12-07 14:19
    var myDiv = document.getElementById('containerDiv');
    myDiv.innerHTML = variableLongText;
    myDiv.scrollTop = 0;
    

    See the scrollTop attribute.

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