Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

175
Visualizações
Transform dom elements to match an image regardless of its deformations

I want to have an image on page with DIVs sitting at the same place relative to the image even when the image gets stretched. Basically, I know the position the stars should take in the original picture, however I dont know how to move them such that they stand in the right place.

Here I explain with a picture :

my current result

Here is my HTML markup. The .slide-point elements are the red stars:

let points = document.querySelectorAll(".slide-point");

points.forEach(point => {
  const x = point.dataset.x;
  const y = point.dataset.y;
  const h = point.dataset.h;
  const w = point.dataset.w;

  let image = point.closest(".slide").querySelector("img");
  let natH = image.naturalHeight;
  let natW = image.naturalWidth;

  let clientH = image.clientHeight;
  let clientW = image.clientWidth;


  point.style.left = x * clientW / natW + "px";
  point.style.top = y * clientH / natH + "px";
  point.style.width = w;
  point.style.height = h;
});
.slide-point {
  position: absolute;
  background: red;
}

.slide>img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: top left;
}
<ul data-slides>
  <li class="slide" data-active>
    <div class="slide-point" data-x="240" data-y="2295" data-h="165" data-w="165">&#9733;</div>
    <div class="slide-point" data-x="512" data-y="2155" data-h="165" data-w="165">&#9733;</div>
    <img id="slide-img" src="https://via.placeholder.com/800" alt="Photo1">
  </li>
</ul>

Obviously, as soon as I resize the page, the stars don't align with the image anymore.

disalignment

Thank you in advance !

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda