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

95
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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