1
I’m mounting a function for wordpress, in which one can navigate between the posts through the Keyboard.
With difficulties, I managed to create the following function:
<script>
window.onkeydown = function(event) {
if (event.keyCode === 38) { //Para cima
if($(window).scrollTop() == 0) {
var href = $("a[rel='next']").attr("href");
window.location.href = href;
}
}
if (event.keyCode === 40) { //Para baixo
if($(window).scrollTop() + $(window).height() == $(document).height()) {
var href = $("a[rel='prev']").attr("href");
window.location.href = href;
}
}
};
</script>
However, I needed to check first if there is a rel="Prev" or rel="next" in html to release or not the keydown function
for example:
If there is a rel="prev"
html, then run the page change function by pressing the button on the keyboard.
Thanks in advance for the help
Hello @Ucas coast, thank you, but I got it in a simpler way: if ($("a[rel='Prev']"). length > 0) {
– Thallyson Dias
Good! It’s basically the same thing @Thallysondias actually
if (prev.length)
is simpler thanif ($("a[rel='prev']").length > 0)
, as you can see here– BrTkCa