how to reset scroll position in a div using javascript

前端 未结 3 1149
遇见更好的自我
遇见更好的自我 2021-02-06 01:31

I am working on a mobile hybrid application.

In my html page, I have 3 tabs. When clicking a tab, the content of the scrollable div gets changed. My problem is when I sc

相关标签:
3条回答
  • 2021-02-06 02:01

    Without seeing code, i can just guess. If you want to reset the scroll position you can simply use

    window.scrollTo(0,0); 
    

    add this code to your each tab click functions so that when ever you click any tab, it resets to top.

    If you have any specific div that has overflow property

    var myDiv = document.getElementById('specificDiv');
    myDiv.scrollTop = 0;
    
    0 讨论(0)
  • 2021-02-06 02:08

    It is easy

     <div id="test" style="height:150px; width:600px;overflow-y:auto;background-color:gray;">
         <div style="width:150px;height:500px; background-color:green;"></div>
     </div>
    
    document.getElementById('test').scrollTop =0;
    
    0 讨论(0)
  • 2021-02-06 02:12

    Finally this worked for me

    function resetScrollPos(selector) {
      var divs = document.querySelectorAll(selector);
      for (var p = 0; p < divs.length; p++) {
        if (Boolean(divs[p].style.transform)) { //for IE(10) and firefox
          divs[p].style.transform = 'translate3d(0px, 0px, 0px)';
        } else { //for chrome and safari
          divs[p].style['-webkit-transform'] = 'translate3d(0px, 0px, 0px)';
        }
      }
    }
    resetScrollPos('.mblScrollableViewContainer');
    

    Calling this function after transition between view ,will reset my scroll position.

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