views:

1306

answers:

2

Hi guys,

I want to implement a scroll function.. So the default of scroll is disabled. And if the user use the scroll button, I want it to be set to the point I want.. How can I implement this function? window.scrollTop is not working.. I tried a lot of different methods but all were not working..

$(window).scroll(function() {
     $(body).scrollTop = 3000px;
})
+3  A: 

There is a plugin for JQuery called ScrollTo that might do exactly what you need.

Check it out here: http://plugins.jquery.com/project/ScrollTo

womp
Yes, I know that.. I want to write my own function so that I could learn..
Keira Nighly
Read the code then!
altCognito
Yes, I tried to read the code but I didn't understand any of it.. I'm still new to this programming thing
Keira Nighly
+2  A: 

The scrollTop property only accepts an integer (not pixels). Omit the px and it should be fine.

$(window).scroll(function() {
    $('body').get(0).scrollTop = 3000; // note that this does only work if body has overflow
    // if it hasn't, use window instead
});
moff
I don't understand.. The $('body') already selected the BODY why do I still need to use get(0) in order for that to work?Also. Is there a way to know if the user scrolled north / south?
Keira Nighly
$('body') returns a jQuery object; get(0) gives you access to the corresponding node (within the DOM) - this enables you to access DOM properties like 'scrollTop'
J-P