Refactoring scrolling code and scroll to the selected element when resizing.