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

206
Visualizações
Stop and resume loop not working - mouseover/mouseout

I wrote a script that changes the picture every 1 second.

But I have a problem, the loop does not stop when hovering and leaving the mouse on the child_block.

That is, when you hover the mouse over the picture, the pictures should stop changing. And when the mouse moves away from the picture, the pictures should change again.

How to fix this problem?

I have marked problem areas in the code with comments.

let main = document.querySelector('.main_block');
let child_block = document.querySelector('.child_block');

let images = document.createElement('img');              
images.className = 'img_s';
child_block.prepend(images);

const colors = [                 
{name: 'https://i.ibb.co/JkTVvVP/1.png', interval: 1000},
{name: 'https://i.ibb.co/GPZRcL4/2.jpg', interval: 1000},
{name: 'https://i.ibb.co/9wdcLz8/3.png', interval: 1000},
]

let count =0;   

function change(){  

if(count === colors.length){     
count = 0;  
}

images.src = colors[count].name;
  
cycle = setTimeout(change, colors[count].interval);
count = count + 1;
}


let cycle = setTimeout(change,1000);

main.addEventListener('mouseover', function(e){  // ******** loop stop not working =( ********
let child = e.target.className;

if(child == 'child_block'){
clearTimeout(cycle);
}       
})


 main.addEventListener('mouseout', function(e){  // ******** loop restart doesn't work either =( ********
let child = e.target.className;

if(child == 'child_block'){
cycle = setTimeout(change, 1000);
}        
})
body{
display:grid;
place-items:center;     
user-select:none;
}


.main_block{
display:grid;
place-items:center; 
border:1px solid black;
width: 50%;
height:500px;
margin:50px;
}

.img_s{
width:100%;
height:100%;    
border:1px solid black;
z-index:-1;
}

.child_block{
border:1px solid red;
width:50%;
height:80%;
z-index:1;      
}
<div class = "main_block">

<div class = "child_block">


</div>

</div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The Following Behaviour Happens Because The event.target returns the innermost child element which is being hovered/clicked on. In your case since the parent div child_block has comparatively less space left out than img_s so even when you hover over most of the times the target will be img_s and not child_block So you need to check if either img_s or child_block has been hovered over or not for this program to work.

let main = document.querySelector('.main_block');
let child_block = document.querySelector('.child_block');

let images = document.createElement('img');              
images.className = 'img_s';
child_block.prepend(images);

const colors = [                 
{name: 'https://i.ibb.co/JkTVvVP/1.png', interval: 1000},
{name: 'https://i.ibb.co/GPZRcL4/2.jpg', interval: 1000},
{name: 'https://i.ibb.co/9wdcLz8/3.png', interval: 1000},
]

let count =0;   

function change(){  

if(count === colors.length){     
count = 0;  
}

images.src = colors[count].name;
  
cycle = setTimeout(change, colors[count].interval);
count = count + 1;
}


let cycle = setTimeout(change,1000);

main.addEventListener('mouseover', function(e){  // ******** loop stop not working =( ********
let child = e.target.className;

if(child == 'child_block' || child == 'img_s'){
clearTimeout(cycle);
}       
})


 main.addEventListener('mouseout', function(e){  // ******** loop restart doesn't work either =( ********
let child = e.target.className;

if(child == 'child_block' || child == 'img_s'){
cycle = setTimeout(change, 1000);
}        
})
body{
display:grid;
place-items:center;     
user-select:none;
}


.main_block{
display:grid;
place-items:center; 
border:1px solid black;
width: 50%;
height:500px;
margin:50px;
}

.img_s{
width:100%;
height:100%;    
border:1px solid black;
z-index:-1;
}

.child_block{
border:1px solid red;
width:50%;
height:80%;
z-index:1;      
}
<div class = "main_block">

<div class = "child_block">


</div>

</div>

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