Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can i simulate mousewheel up or down with javascript

I have a web app where with an immersive view. In this view, i can zoom in or zoom out with the mouse wheel action.

What i would like to do is to create 2 buttons which call javascript function and simulate the mouse wheel actions. I tried with :

window.scrollTo(0,300);

but scrolling in my case doesn't zoom (seems like only mouse wheel action work).

Do you have any idea, how can i simulate the mouse wheel up or down within the javascript ?

thank you

like image 921
Sambo Avatar asked Sep 28 '17 16:09

Sambo


People also ask

What is Mousewheel JS?

About Mousewheel JS Mousewheel is a jQuery plugin that adds cross-browser mouse wheel support. Also, Scroll distance can be measured using Delta normalization.

What is wheel event?

The wheel event fires when the user rotates a wheel button on a pointing device (typically a mouse). This event replaces the non-standard deprecated mousewheel event.


1 Answers

So i solve my problem after some research.

Here is what i did :

I create a mouse event and pass it wheel event.

var evt = document.createEvent('MouseEvents');
evt.initEvent('wheel', true, true); 

Then i can set deltaY depending on if you want a wheel up or a wheel down.

evt.deltaY = +120;
// evt.deltaY = -120

And you can pass this event to your element by doing so :

element.dispatchEvent(evt);
like image 164
Sambo Avatar answered Sep 19 '22 22:09

Sambo