[英]Trigger the css:hover event with js
I have <div class="animate">
and in css: 我有
<div class="animate">
并在CSS中:
div.animate:hover{
//do stuff
}
But would also like to invoke this via javascript. 但也想通过javascript调用它。
Is it possible? 可能吗?
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). 如使用JS触发CSS悬停中所述,这是不能原样进行的(如果要按照创建此答案时的确切说明进行操作)。
But the main goal is achievable by: 但主要目标是可以达到的:
hover
(or whatever name) as well as the selector :hover
in the CSS. hover
(或任何名称)以及选择器:hover
。 .addClass("hover")
to trigger CSS, and .trigger("hover")
or .trigger("mouseenter")
to trigger the JS. .addClass("hover")
触发CSS,并.trigger("hover")
或.trigger("mouseenter")
触发JS。 mouseleave
handler. mouseleave
处理程序。 or 2nd .hover()
handler, clears the hover
class if present. .hover()
处理函数,清除hover
类(如果存在)。 Instead of doing it this way, I suggest you just add a class to the other tag. 建议您不要在其他标签中添加一个类,而不要这样做。 In jQuery it would be:
在jQuery中,它将是:
$(window).load(function() {
$('.trigger-animate').hover(function(){
$('.animate').addClass('hover');
});
}
I'd 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 so desired using this syntax: 我建议使用此方法,因为它同时处理onMouseOver和onMouseOut(这样,如果您愿意,可以使用以下语法在鼠标离开$('。trigger-animate')时删除该类:
.hover( handlerIn(eventObject), handlerOut(eventObject) )
checking out the documentation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.