Scroll div top on page load then set scroll to work normally

独自空忆成欢 提交于 2020-01-07 09:14:25

问题


I have a chat system on my site and defaulted the message div position to always display the last message on page load. I accomplished this by using the following line of code:

  msgDiv = document.getElementById('message_row_large');
  msgDiv.scrollTop = msgDiv.scrollHeight;

However, this code sets the scroll position to be equal to the div height at all times, which doesn't allow users to scroll up and see other messages.

I need to re-enable scroll to its default functionality after the page loads. ANy help is welcome.

Thank you!

P.S. I am using ajax to load chat messages. When user clicks on a name on the left hand panel, the chat between him/her and the other person loads on the right hand panel.


回答1:


Try

    $display = $('#message_row_large');
    $display.animate({scrollTop: $display[0].scrollHeight }, 'fast');

Working Fiddle:

https://jsfiddle.net/cshanno/3bo48dxj/1/



来源:https://stackoverflow.com/questions/32318922/scroll-div-top-on-page-load-then-set-scroll-to-work-normally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!