DEV Community

Jenuel Oras Ganawed
Jenuel Oras Ganawed

Posted on

 

JS: "CTR + Wheel" Event

This short read is for those who are searching how to get event for CTR + MOUSEWHEEL event.

Here is a sample. Lets say we are trying to change to font size of our text if we CTR + WHEEL.



/**
You can change the window to element you want this event to happen.
example: document.getElementById("view-chapter-verse").addEventListener()
*/
window.addEventListener("wheel", function (event) {
        event.preventDefault(); // this will prevent the scaling for browsers.
        if (event.ctrlKey) { // check if we are pressing the CTR key
            if (event.deltaY) {
                // Do what you want here. In this example
                // Im trying to change the Font Size of my article
                // event.deltaY is less than 0 it means we are scrolling up. If not scrolling down.
                fontSize = event.deltaY < 0 ? fontSize.value + 0.5 : fontSize.value - 0.5;
            }
        }
});

Enter fullscreen mode Exit fullscreen mode

Top comments (0)

11 Tips That Make You a Better Typescript Programmer

typescript

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!