【问题标题】:Media queries not work in Firefox, when has animation媒体查询在 Firefox 中不起作用,何时有动画
【发布时间】:2022-08-02 16:03:45
【问题描述】:
屏幕宽度小于640px时,背景被覆盖,为什么高度没有被覆盖?而且,它适用于 Chrome 和 Edge。
.change{
background: #f00;
height: 100px;
animation: test1 1s linear forwards;
}
@keyframes test1 {
0% {
height: 100px;
}
100% {
height: 400px;
}
}
@media only screen and (max-width: 640px) {
.change{
background: #0f0;
}
@keyframes test1 {
0% {
height: 100px;
}
100% {
height: 200px;
}
}
}
jsfiddle
标签:
animation
firefox
media-queries
【解决方案1】:
查看该站点在 Chrome、Firefox 和 Safari 上的行为方式,似乎 Chrome 可能是唯一一个在 @media 条件更改后尝试重新激活 <div> 的浏览器。 Firefox 和 Safari 认为动画已经完成,不再需要重新设置动画。
作为一种解决方法,您可以强制所有 Web 浏览器通过重新设置 @media 范围下的 .change 动画属性并稍微更改它们的值来重新激活它,例如通过 1.000001 秒而不是 1 秒的动画。
.change{
background: #f00;
align-items: center;
font-family: sans-serif;
color: #fff;
height: 100px;
display: flex;
justify-content: center;
animation: test1 1s linear forwards;
}
@keyframes test1 {
0% {
height: 100px;
}
100% {
height: 400px;
}
}
@media only screen and (max-width: 640px) {
.change{
background: #0f0;
color: #00f;
animation: test1 1.000001s linear forwards;
}
@keyframes test1 {
0% {
height: 100px;
}
100% {
height: 200px;
}
}
}
<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0, minimum-scale=1.0' name='viewport' />
<body>
<div class="change">Some content</div>
</body>
</html>