Question

I need that when the user presses the DOWN button (down) on the keyboard, it goes to a div in the same way as with a scrollTop. Has anyone done something like this using jQuery?

It would be something like this?

$(window).on('keydown', function (e) {
    if (e.which != 40) return false;
    var posicao = $('.homeBaixoRodapeTexto1').position().top;
    $('html, body').stop().animate({
        scrollTop: posicao
    }, 1500);
});
Était-ce utile?

La solution

I believe you want .offset().top instead of .position().top.

$(window).on('keydown', function (e) {

    if (e.which != 40) return true;

    e.preventDefault();

    var posicao = $('.homeBaixoRodapeTexto1').offset().top;

    $('html, body').stop().animate({
        scrollTop: posicao
    }, 1500);

});

You have to be real careful with this. You're essentially breaking navigation with the keyboard.

Here's a small demo: http://jsbin.com/xecapoyu/3/edit?js,output

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top