How to get position in vh and vw instead of in pixel?

so i'm doing some testing to see the position of en of an object in a frame by using this code

function showCoords(event) { var x = event.clientX; var y = event.clientY; var coords = "X coords: " + x + ", Y coords: " + y; document.getElementById("demo").innerHTML = coords;
}

But it show the position in pixels from top and left, is there a way to show it in different format like vw from left and vh from top instead of in pixels?

1 Answer

window.innerHeight and window.innerWidth are the size of your browser window. You should be able to divide x / window.innerWidth and y / window.innerHeight to get the fractional size of the viewport.

Alternately, you could use clientWidth: window.innerWidth vs document.documentElement.clientWidth

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like