I would like to run a function that would run when the box width reaches certain pixel, lets say 80px. lets say I want to alert "hello" and console.log "hey". How would i be able to accomplish this? And I can't use jQuery, so just vanilla Javascript please.
Edit: I wonder if I could insert something like a function that runs the alert or console log when redbox.style.width == "80px"
?
const redbox = document.querySelector("#redbox");
const coolbutton = document.querySelector("#coolbutton");
coolbutton.addEventListener("click", animator);
function animator() {
if (redbox.style.width == "50px") {
redbox.style.width = "100px";
redbox.style.transition = "2s";
}
else {
redbox.style.width = "50px";
redbox.style.transition = "2s";
}
}
#redbox {
background: red;
height: 50px;
width: 50px;
}
<div id="redbox"></div>
<button id="coolbutton">click</button>