javascript - Scroll Browser Window with jQuery - Stack Overflow

I want to scroll the browser window in response to certain user actions.I found out about scrollLeft in

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?

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?

Share Improve this question edited May 23, 2017 at 12:04 CommunityBot 11 silver badge asked Jan 11, 2013 at 3:53 Jonathan WoodJonathan Wood 67.5k82 gold badges305 silver badges533 bronze badges 2
  • If you are using an anchor link, you should prevent the default action of the event. event.preventDefault(). – Ram Commented Jan 11, 2013 at 3:57
  • It's in response to the mouse moving over an element. (It's not implemented yet but it will be a timer handler when I'm done.) – Jonathan Wood Commented Jan 11, 2013 at 3:58
Add a ment  | 

3 Answers 3

Reset to default 3

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

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

That should be enough.

No px required...

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

Fiddle

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'

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745466891a4628953.html

相关推荐

  • javascript - Scroll Browser Window with jQuery - Stack Overflow

    I want to scroll the browser window in response to certain user actions.I found out about scrollLeft in

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信