and in css: div.animate:hover{ //do stuff } But also would like to call it via jav...">

Fire css: hover event with js

I have <div class="animate"> and in css:

 div.animate:hover{ //do stuff } 

But also would like to call it via javascript.

Is it possible?

+4
source share
2 answers

Instead of doing it this way, I suggest you just add the class to another tag. In jQuery, this will be:

  $(window).load(function() { $('.trigger-animate').hover(function(){ $('.animate').addClass('hover'); }); } 

I would recommend using this method because it handles both onMouseOver and onMouseOut (this way you can also remove the class when your mouse leaves $ ('. Trigger-animate') if you want to use this syntax:

 .hover( handlerIn(eventObject), handlerOut(eventObject) ) checking out the documentation 
+2
source

As described in the css hang with JS , this is not possible as-is (if you want it to be described exactly while creating this answer).

But the main goal is achievable:

  • Setting the hover class (or any other name), as well as the :hover selector in CSS.
  • Call .addClass("hover") to launch CSS and .trigger("hover") or .trigger("mouseenter") to launch JS.
  • Providing a mouseleave handler. or 2 .hover() , clears the hover class, if any.
+4
source

Source: https://habr.com/ru/post/1413471/


All Articles