function mouseSlideIn() {
document.getElementById("box").style =
"background: blue; height: 50px; width: 50px;";
}
function mouseSlideOut() {
setTimeout(function() {
document.getElementById("box").style = "";
document.getElementById("eventDiv").style = "";
}, 2000);
}
body {
background: grey;
margin: 0;
}
#eventDiv {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 90vh;
background: powderblue;
border-radius: 20px;
transition: 0.8s;
transition-timing-function: ease;
}
#box {
position: relative;
background: red;
height: 150px;
width: 150px;
transition: 0.5s;
transition-timing-function: ease;
}
<br>
<center>
<div id="eventDiv" onmouseover="mouseSlideIn()" onmouseout="mouseSlideOut()">
<div id="box"></div>
<!--Above is the red box i.e. inside the main DIV-->
</div>
</center>
id="eventDIV"
(outer most div), the id="box"
must shrink and change color.id="eventDIV"
, the id="box"
must return to its original size & color.2000ms
).setTimeout
function.id="eventDIV"
before the setTimeout
runs out which is 2000ms
, it breaks the code.id="eventDIV"
, the mouseSlideOut()
function gets executed, which was supposed to be called on onmouseout
event; that is when I hover out.clearTimeout
function, but its doesn't seems to help.Note:
:hover
).You can achieve this with the clearTimeout-function. As you can also see in the docs, you need to set an ID for your timeout to be able to clear it.
This should do the trick:
let timeoutId;
function mouseSlideIn() {
document.getElementById("box").style =
"background: blue; height: 50px; width: 50px;";
clearTimeout(timeoutId);
}
function mouseSlideOut() {
timeoutId = setTimeout(function () {
document.getElementById("box").style = "";
document.getElementById("eventDiv").style = "";
}, 2000);
}