I’ve got a simple addClass function to add a class on hover so that I can apply a CSS transition to it. I also want the CSS transition to apply when the mouse leaves the container but It doesn’t work because I have .removeClass. I realize I can do it with CSS but I’m trying to learn JS.
I’ve tried the setTimeout like so:
$('.wp-container-20').hover(
function() {
$(this).addClass('homeServicesImageHover').bind(this)
},
function() {
setTimeout(
function() {
$(this).removeClass('homeServicesImageHover')
}, 5000)
}
)
I can’t get that to work.
3
Answers
You have two problems.
hover()
needs to be a function. You’re callingsetTimeout
immediately and passing the return value tohover()
.this
means something different when it is in a function called bysetTimeout
. See this questionTry to do this with css not jquery.
Use .yourClass:hover and .yourClass to add CSS correctly. For example the CSS that you should write in .yourClass:hover is the CSS that has the class .imageHover
Also I use this additional class (in your normal class not in :HOVER) that gives some effects on transition
I hope that my info help you :).
Kind regards.
you are adding and removing the same
class
(ImageHover) and it is not logical. You first add thisclass
to the element and then remove it from that same element, so that it has no class at the end!For transition after mouse leave, you can use
onmouseleave()
event, too.Good Luck