Trigger the css:hover event with js

前端 未结 2 508
旧时难觅i
旧时难觅i 2020-12-11 19:04

I have

and in css:

div.animate:hover{
//do stuff
}

But would also like to invoke this via javas

2条回答
  •  星月不相逢
    2020-12-11 19:39

    As described in Trigger css hover with JS this is not possible as-is (if you want it as described exactly at the time of the creation of this answer).

    But the main goal is achievable by:

    1. Setting a class hover (or whatever name) as well as the selector :hover in the CSS.
    2. Calling .addClass("hover") to trigger CSS, and .trigger("hover") or .trigger("mouseenter") to trigger the JS.
    3. Ensuring the mouseleave handler. or 2nd .hover() handler, clears the hover class if present.

提交回复
热议问题