gpt4 book ai didi

javascript - Animate.css - 将 onmouseover 应用于 div 以添加类

转载 作者:行者123 更新时间:2023-11-30 11:22:27 25 4
gpt4 key购买 nike

我已经导入了 animate.css 类,它在给出类名时工作得很好,但是当悬停在 div 上时使用应用函数来更改类时它不起作用

  <div id="result" style="width: 100px; height: 100px; background-color: blue; color: white" >
animated bounce
</div>

这是脚本

<script>
function animationHover(element, animation) {
element = $(element);
element.hover(
function () {
element.addClass('animated ' + animation);
},
function () {
//wait for animation to finish before removing classes
window.setTimeout(function () {
element.removeClass('animated ' + animation);
}, 2000);
}
);
};
</script>

然后最后调用方法:

animationHover('#result', 'tada');

但这并没有做任何事情

感谢所有帮助

最佳答案

你可以这样简化:

function animationHover(element, animation) {
$(element).hover(
function() {
$(this).addClass('animated ' + animation);
},
function() {
//wait for animation to finish before removing classes
window.setTimeout(function() {
$(element).removeClass('animated ' + animation);
}, 1000);
}
);
};
animationHover('#result', 'tada');
.animated {
animation: anime 2s forwards;
}

.tada {
background: blue;
color: red;
}

@keyframes anime {
to {
transform: rotate(90deg);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="result" style="width: 100px; height: 100px;">
animated bounce
</div>

或者像这样:

function animationHover(element, animation) {
$(element).hover(
function() {
$(this).addClass('animated ' + animation)
.on('animationend',function() {
$(this).removeClass('animated ' + animation)
});
}
);
};
animationHover('#result', 'tada');
.animated {
animation: anime 2s forwards;
}

.tada {
background: blue;
color: red;
}

@keyframes anime {
to {
transform: rotate(90deg);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="result" style="width: 100px; height: 100px;">
animated bounce
</div>

关于javascript - Animate.css - 将 onmouseover 应用于 div 以添加类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49298057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com