Nasir
Nasir

Reputation: 4865

Scroll function not working properly, using JQuery?

I have this code & when I click #down the page scrolls down by 20, which is correct. When I click #up the page scrolls right to the top which is wrong as I want the page scroll up by 20 only. Here's the code I'm working with:

$('#up').click(function(){
    $('html, body').animate({
        scrollTop: $(this).offset().top -= 20
    });
    return false;
});

$('#down').click(function(){
    $('html, body').animate({
        scrollTop: $(this).offset().top += 20
    });
    return false;
}); 

Any input would be greatly appreciated, Thanks

Upvotes: 0

Views: 157

Answers (1)

Samuli Hakoniemi
Samuli Hakoniemi

Reputation: 19049

Correct syntax is -=, not =- (which causes value to be -20, hence the scrolling to top).

Upvotes: 2

Related Questions