user2412555
user2412555

Reputation: 1055

Scroll a scrollable div to its top programatically

I have a scrollable div with a fixed height and a long list inside it. I want to scroll to top in the scrollable div when I scroll down with the long list. how should I start with this.

I tried to find any answer but all refer to using scrollTop or offset, but I just can't get this to work. I tried scrollTop on the div that is scrollable, but it is always undefined

I am using JQuery-mobile.

I have the following setup

<div id="scrollable">
  <div id="inner">
    <div id="content_wrapper">
       <div>...</div>
       <div>...</div>
       ...
    </div>
  </div>
<div>

scrollable is the scrollable container, which overflow-y is applied to.

I tried

$('#scrollable').scrollTop(fixedvalue);
$('#scrollable').scrollTop($('$scrollable').offset());
$('#scrollable').scrollTop($('firstdivelement').offset());
$('#scrollable').scrollTop($('firstdivelement').position().top);

I am not very good with javascript.

Upvotes: 1

Views: 9385

Answers (1)

Murat Yıldız
Murat Yıldız

Reputation: 12040

For scrolling to top on page body :

$('html, body').animate({ scrollTop: (0) }, 'slow');

For scrolling to top on div :

$('#yourDivId').animate({ scrollTop: (0) }, 'slow');

Upvotes: 6

Related Questions