Skip to content
Advertisement

Stop Div scrolling to the top at Page Refresh using Jquery

I have two pages with the same css class and the same id, but since they are on different pages, the id is the separator.

Page 1) Got a Div being refreshed every 5 seconds. The div has scrolling set to scroll so that user can scroll, but when the refresh happens the div scrolling goes back to the top. Any ideas/written script on how I can keep the scroll position after the div is refreshed?

Page 2) This is similar to page 1, other from, When users come to this page the first time, I want the scroll position to be at the bottom, then when the refresh happens, I want to keep the scroll position of the div wherever I have scrolled to after its refreshed

Bottom Line: I’m relatively new to Front-End design, so Please, your contribution should be something I would understand easily or close to easy This is my css

JavaScript

This is my java script for loading and refreshing

JavaScript

Advertisement

Answer

See this Fiddle : http://jsfiddle.net/tu6fj8jq/7/

Using element.scrollTop you can store the scroll position in the div before you refresh it

This outputs the scroll position in another element when div is scrolled :

JavaScript

For the second part, you can scroll the div to the bottom at page load like this :

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement