2
In my code, I am having add an eventListener for each HTML element of the class "animation".
For this I use "addeventlistener", passing the changeBar function as Listener, to be triggered in case of click. However, when loading the page, the changeBar function is already executed without clicking. This should not be happening:
const initial = 10
const end = innerWidth
const step = 5
function setBarAnimation(){
let increase = true
const elementos = document.querySelectorAll(".animacao")
for (let item of elementos){
increase = true
item.addEventListener("click", changeBar(item, increase, 0))
}
}
function changeBar(element, increase, timeOutId){
const newBegining = element.offsetWidth+step
if (newBegining < end && increase == true){
element.style.width = newBegining+"px"
timeOutId = setTimeout(() => changeBar(element, increase, timeOutId), 1000)
}
}
setBarAnimation()
<body>
<div id="Barra1" class="animacao"
style="background-color: royalblue;">
</div>
<div id="Barra2" class="animacao"
style="background-color: seagreen;">
</div>
<div id="Barra3" class="animacao"
style="background-color: red;">
</div>
<style>
.animacao {
border-bottom: 10px;
border-top: 10px;
height: 30px;
width: 10px;
}
</style>
</body>
Hi Lucas, I gave an answer that solves your problem. However there are strange things like this variable
increase
that never changes and the argument0
static that you pass to the function. I imagine it is the result of code simplification to be able to put here in the question... if you should not review it. But regarding your doubt I clarified in the answer.– Sergio
Hello. Yes, thank you very much. Yes, this Crease is a "gambiarra". In fact, what I was trying to do with it, is to try for the "setTimeOut" which increases the width of the Divs, when the mouse comes off of them. This is because, once clicked, the Divs are being increased while the condition (newBegining < end && Increase == true). But I wanted to stop this setTime out when I had an event onmouseout,and I was using this Crease to find out if, who called the "changeBar" function was a click or mouseover event. I think I’ll post a question about that, because I’ve been locked in there for hours
– Lucas Pletsch