0

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?

Alex Welander
  • 89
  • 1
  • 7

1 Answers1

2

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

Josh
  • 1,997
  • 13
  • 21