Jonathan Wood
Jonathan Wood

Reputation: 67223

Scroll Browser Window with jQuery

I want to scroll the browser window in response to certain user actions.

I found out about scrollLeft in a stackoverflow response. From there, I was able to find scrollTop and ended up with the following:

$(window).scrollTop((Number($(window).scrollTop())+100)+'px');

This does in fact scroll, but to the top of the page. No matter what value I replace 100 with (I even tried negative numbers), it always just jumps to the top of the page. (Note: $(window).scrollTop() is returning 0.)

Can someone give me some tips to what I might be missing?

Upvotes: 2

Views: 3973

Answers (3)

Roko C. Buljan
Roko C. Buljan

Reputation: 206151

YOu don't need Number cause scrollTop returns a number

scrollTop will perform if there's some scrollHeight available that is higher than the element's height, and it's always a positive number.

and it should look like:

$(window).scrollTop( $(window).scrollTop()+100 );

you don't need the 'px'

Upvotes: 1

Anujith
Anujith

Reputation: 9370

No px required...

$(document).ready(function(){
    $(window).scrollTop(($(window).scrollTop()+600));
});

Fiddle

Upvotes: 1

ericponto
ericponto

Reputation: 736

scrollTop just takes a number, rather than a px value.

$(window).scrollTop($(window).scrollTop()+100);

That should be enough.

Upvotes: 3

Related Questions