When site is loaded I want to automatically press the down arrow key every few seconds without any user interactions. Any ideas to do this with JS or jQuery?
Asked
Active
Viewed 265 times
0
-
do you want to scroll? Or why you need that? – baklazan May 22 '19 at 11:26
-
Yep, I have sections which are full height of window. I need make them scroll automatically. It can be done by pressing "down arrow" or mouse wheel. – Denis Melamed May 22 '19 at 11:28
-
1In that case you would be better off updating the `scrollTop` property of the window instead of faking keyboard inputs – Rory McCrossan May 22 '19 at 11:29
-
Thank you for comment. If you have any idea how to do this, can you provide a code or similar topics on StackOverflow? Thank you. – Denis Melamed May 22 '19 at 11:37
2 Answers
0
Try it with this snippet:
$(document).ready(() => {
let e = $.Event("keyup");
e.which = 40; // keycode for down arrow
// Fire event every second
setInterval(()=>{
$(document).trigger(e);
}, 1000);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
It is possible with pure JavaScript Code too, but its quite a hassle.

MauriceNino
- 6,214
- 1
- 23
- 60
0
So I used onepage-scroll library (https://github.com/peachananr/onepage-scroll) for section scroll and here is answer of my question (now it works):
if( $('.onepage-pagination li').length > 1 ){
var steps = $('.onepage-pagination li').length;
//console.log( 'steps', steps );
var i = 2;
var autoplay = setInterval(function(){}, 4000);
autoplay = setInterval(function(){
$('[data-index="'+i+'"]').click();
//console.log( 'step click', i );
if(i == 5){
i = 1
}else{
i++
}
}, 7000);
$(document).bind('mousewheel DOMMouseScroll touchstart', function(event) {
clearInterval(autoplay);
console.log( 'slide stopped');
});
}
But if anyone have any other ideas I would like to see them just to know new features in case.
Thank you.

Denis Melamed
- 42
- 5