Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

158
Vistas
Textblock following mousepointer

I want a piece of text to follow the mousepointer, but only when the mouse is hovering over an image. Until now it works partly: when the mouse accesses the image-area, the textblock jumps to the mousepointer, and remains attached to it. But unfortunately when the mouse leaves the image-area again, the text remains attached to the mouse and follows the mousepointer wherever the mouse is going. Instead it should go back to its starting position.

The relevant html:

<main id="landing">
    <div id='slider-block'>
        <img id='slider' class='my-image' src="img/my-image.jpg">
        <div class="my-text">
            Just some example text
        </div>
    </div>
</main>

And the relevant CSS:

#slider-block {
    background-color: orange;
}
.my-image {
    background-color: red;
    padding: 30px;
    width: 50%;
    height: auto;
}
.my-text {
    position: absolute;
    top: 120px;
    left: 120px;
    width: 20%;
} 

And the .js content:

let sliderbody = document.querySelector('#slider');
sliderbody.onmouseover = function () {
    document.addEventListener('mousemove', function (e) {
            let titleblock = document.getElementsByClassName('my-text')[0];
            titleblock.style.left = e.clientX + 'px';
            titleblock.style.top = e.clientY + 'px';
        }
    );

}

So the question is how to end the text following the mousepointer when the mouse leaves the slider-image.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Let me know if that fulfills your requirements:

HTML

<main id="landing">
  <div id='slider-block'>
      <img id='slider' width="500" class='my-image' src="https://images.unsplash.com/photo-1541363111435-5c1b7d867904?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8ZHVtbXl8ZW58MHx8MHx8&w=1000&q=80">
      <div class="my-text">
          Just some example text
      </div>
  </div>
</main>

CSS

#slider-block {
  position: relative;
  overflow: hidden;
}

.my-text {
  position: absolute;
}

JS

let sliderbody = document.querySelector('#slider');
sliderbody.onmouseover = function () {
    document.addEventListener('mousemove', function (e) {
      
            let titleblock = document.getElementsByClassName('my-text')[0];
            console.log(titleblock);
            titleblock.style.left = e.clientX + 'px';
            titleblock.style.top = e.clientY + 'px';
        }
    );

}
about 4 years ago · Juan Pablo Isaza Denunciar

0

As a very simplistic solution you could just sense when the mouse leaves the image and at that point remove the eventlistener so the x and y do not get updated.

let sliderbody = document.querySelector('#slider');

function mousemoved(e) {
  let titleblock = document.getElementsByClassName('my-text')[0];
  titleblock.style.left = e.clientX + 'px';
  titleblock.style.top = e.clientY + 'px';
}
sliderbody.onmouseover = function() {
  document.addEventListener('mousemove', mousemoved);
}
sliderbody.onmouseout = function() {
  document.removeEventListener('mousemove', mousemoved);
}
#slider-block {
  background-color: orange;
}

.my-image {
  background-color: red;
  padding: 30px;
  width: 50%;
  height: auto;
}

.my-text {
  position: absolute;
  top: 120px;
  left: 120px;
  width: 20%;
}

.my-text.follow {}
<main id="landing">
  <div id='slider-block'>
    <img id='slider' class='my-image' src="img/my-image.jpg">
    <div class="my-text">
      Just some example text
    </div>
  </div>
</main>

This just leaves the text at its final place.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda