W
W
Westoura2021-06-30 21:11:14
JavaScript
Westoura, 2021-06-30 21:11:14

How to calculate the dimensions of a js element?

There is an element on the page, I calculate the width and height of the element, multiply them. The resulting piece from the element (highlighted in red) I want to use in the future as a response zone for the script. Tell me how to hang a click event on half of this element (sq)?

<div class="el">

</div>

.el{
width: 800px;
height: 600px;
}

 let el= document.querySelector('.el'),
           w = el.offsetWidth * 0.5,
           h = el.offsetHeight,
           sq = w* h;


60dcb244bf377505793718.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2021-06-30
@Westoura

Options:

  • hang on the entire element, check the coordinates when clicking, and return without action if X is more than half
  • programmatically create a new one inside the element, half-width, and hang a listener on it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question