|
|
憨厚的火腿肠 · 潮头浪尖的创新“排头兵”-国务院国有资产监督 ...· 1 年前 · |
|
|
胡子拉碴的椰子 · Delphi打開任何Windows支持文件_ ...· 2 年前 · |
|
|
求醉的松鼠 · 中招报考志愿 带你了解南阳市四中· 2 年前 · |
|
|
刚毅的长颈鹿 · 无锡市氢能和储能产业发展三年行动计划(202 ...· 2 年前 · |
|
|
大方的长颈鹿 · 使用css变量实现更改字体大小功能-CSDN博客· 2 年前 · |
我正在尝试使用display: none隐藏一个div,然后使用jQuery通过单击一个按钮从 Animation.css 切换fadeIn动画。
但是,当我尝试切换动画时,它可以工作,但隐藏它不会播放淡出动画。
下面是一个有效的 example
HTML:
<div class="window animated"></div>
<button style="margin: 100px;">Toggle Fade Window</button>
CSS:
.window{
background: blue;
width: 200px;
height: 200px;
margin: 20px;
display: none;
}
JS:
$(function() {
$("button").click(function() {
$(".window").toggleClass("fadeIn");
})
谢谢!
发布于 2018-08-12 09:11:46
有一些与您的代码相关的问题,以及您对
animate.css
库的误解:
fadeIn
中删除
div
,它不会自动淡出,您必须添加类
fadeOut
in.
fadeIn
会将元素的
opacity
从
0
转换为
1
,因此在
1
样式中使用
display: none
时,它将不起作用。<代码>H213<代码>F214
这就是解决问题的方法:
div
没有同时具有
fadeIn
和
fadeOut
类,这意味着当您第一次呈现视图时,div处于初始状态,因此您需要在中添加
fadeIn
类。
div
>D25类,这意味着以前的div已经淡入,现在需要淡出。因此,只需从
div
中删除
fadeIn
并添加
fadeOut
类。
div
具有
fadeOut
类,则需要添加
fadeIn
并删除
fadeOut
。<代码>H235<代码>F236
以下是代码
$(function() {
$("button").click(function() {
if ($(".window").hasClass("fadeIn")) {
// if the div has fadeIn class, remove it and add fadeOut
$(".window").toggleClass("fadeIn").toggleClass("fadeOut")
else if ($(".window").hasClass("fadeOut")) {
// if the div has fadeOut class, remove it and add fadeIn
$(".window").toggleClass("fadeOut").toggleClass("fadeIn")
} else {
// if the div has neither of fadeIn nor fadeOut, add fadeIn class in
$(".window").toggleClass("fadeIn")
})
.window{
background: blue;
width: 200px;
height: 200px;
margin: 20px;