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

92
Vistas
Image is not stopping after reaching max width of the screen

My question is: how do I stop the image from moving to the left when it reaches the user's maximum screen width? I tried the below method by using JavaScript but it is not working. Also when the image reaches the user's maximum width, it should change the direction and return to where it was before.

<img src="plane.jpg" width="100px" height="100px" id="plane">

<script type="text/javascript">

  var height = document.documentElement.clientHeight;
  var width  = document.documentElement.clientWidth;
  var plane = document.getElementById("plane");
  var leftpos = 0;


setInterval(function(){
  if (leftpos != width) {

    leftpos += 10;
    plane.style.marginLeft = leftpos + 'px' 
  }else {
    // change the direction
  }
      
  
}, 50) // run code every 50 milliseconds
;



</script>

Appreciate your time and Take care.

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

0

You can check if the marginLeft and the width of the image plus 10 is less than window.innerWidth.

var height = document.documentElement.clientHeight;
var width = window.innerWidth;
var plane = document.getElementById("plane");
var leftpos = 0;
var right = true;


var id = setInterval(function() {
  if (right && leftpos + plane.width + 10 <= width) {
    leftpos += 10;
  } else {
    right = false;
    leftpos -= 10;
    if (leftpos <= 0) clearInterval(id);
  }
  plane.style.marginLeft = leftpos + 'px';
}, 50);
<img src="plane.jpg" width="100px" height="100px" id="plane">

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