Search code examples
javascriptpositionmouse

how to show div on event.clientX mouse position 50vw


Hi all i am wondering if it is possible to find the mouse value of 50vw

i have figured out how to show and hide a div depending wether or not the mouse is so far on the X axis, what i want to achieve is only show or hide once past 50vw

any help would be appreciated, ive searched but cant find an answer

here is my code

document.body.addEventListener('mousemove', function (event) {

    if (event.clientX >= 500) {
        console.log("show div or addclass");
        
    } else {
        console.log("hide div or remove class");
    }
}, false);

update: i figured it out i added window.innerWidth/2 so used to css this never occurred to me


Solution

  • Is this what you need?

    const half_of_width = window.innerWidth / 2
    document.addEventListener("mousemove", (event) => {console.log(event.clientX > half_of_width)})