[英]`display: none` not working in css animation?
I wrote a little code to have cards fade out and then be set to display: none;
我写了一个小代码让卡片淡出,然后设置为display: none;
once clicked so it's not shown in the DOM after it has faded out.一旦点击,它就不会在它淡出后显示在 DOM 中。 Although animation-fill-mode: forwards;
虽然animation-fill-mode: forwards;
works, but only the change on the display
element seems to have no effect;有效,但只有display
元素上的更改似乎没有效果; if you hover over the area the cursor is still there, and the element is still shown in dev tools.如果 hover 在该区域上方 cursor 仍然存在,并且该元素仍显示在开发工具中。 Is there a way to make sure that the display: none;
有没有办法确保display: none;
property is set properly?属性设置正确?
HTML <div></div>
HTML <div></div>
CSS CSS
body {
background: black;
margin-top: 4rem;
}
div {
display: block;
background: red;
width: 200px;
height: 200px;
margin: 0 auto;
}
.hidden {
animation: hiddenTransition 300ms ease-in;
animation-fill-mode: forwards; // DOESN'T WORK :C
will-change: opacity, transform, display;
cursor: pointer;
}
@keyframes hiddenTransition {
0% {
opacity: 1;
transform: none;
display: block;
}
99% {
opacity: 0;
transform: translateY(20%);
display: block;
}
100% {
opacity: 0;
transform: translateY(20%);
display: none;
}
}
JS JS
var square = document.querySelector("div");
square.addEventListener("click", function(){
this.classList.toggle("hidden");
});
Even if you set display: none
, the element still will be present in devtools. 即使您设置display: none
,该元素仍将出现在devtools中。 Regarding the cursor, just change cursor display to default in the hidden class. 关于光标,只需在隐藏类中将光标显示更改为默认值即可。 Like so: cursor: default;
像这样: cursor: default;
working code pen here 工作代码笔在这里
Note:-display property can't be animated 注意:-display属性不能设置动画
body {
background: black;
margin-top: 4rem;
}
div {
display: block;
background: red;
width: 200px;
height: 200px;
margin: 0 auto;
}
.hidden {
animation: hiddenTransition 300ms ease-in;
animation-fill-mode: forwards; // DOESN'T WORK :C
will-change: opacity, transform, display;
cursor: pointer;
}
@keyframes hiddenTransition {
0% {
transform: none;
visibility:visible;
}
99% {
transform: translateY(20%);
visibility:visible;
}
100% {
transform: translateY(20%);
visibility:hidden;
}
}
Seems like the only way to achieve this is with JS, which is what vitilok posted in this comment: 看来实现此目标的唯一方法是使用JS,这是vitilok在此评论中发布的内容:
Well, this is a bit tricky. 好吧,这有点棘手。 So in order to accomplish what you want do the following: 1. remove display from will-change as well as from all frames. 因此,为了完成您想要的操作,请执行以下操作:1.从更改和所有框架中删除显示。 2. add a new class .visuallyhidden { display: none; 2.添加一个新类.visuallyhidden {display:none; } 3. add: " setTimeout(function() { square.classList.add("visuallyhidden") }, 500) " inside the callback of click. } 3.在点击的回调内添加:“ setTimeout(function(){square.classList.add(“ visuallyhidden”)},500)“。 You can see working example here And read this link if you want explanation 您可以在此处查看工作示例,如果需要说明,请阅读此链接
Basically, he put a setTimeout
statement which changed the display
property to none
after the animation time. 基本上,他放置了setTimeout
语句,该语句在动画时间之后将display
属性更改为none
。
Their CSS 他们的CSS
.visuallyhidden {
display: none;
}
Their JS 他们的JS
setTimeout(function() {
square.classList.add("visuallyhidden")
}, 500);
As others said the display
css property not work in css animation, so I have to set the property by js code.正如其他人所说, display
css 属性在 css animation 中不起作用,所以我必须通过 js 代码设置属性。
img.classList.add('content-img') // css animation class name
img.onanimationend = () => {
img.style.display='none' // hide the element after animation
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.