[英]CSS border-image keyframe animation works in Firefox but not in Chrome
I have a div
with a gradient border-image
and an animation
property which changes the gradient's deg
(degrees) in keyframes.我有一个带有渐变
border-image
的div
和一个animation
属性,它改变了关键帧中渐变的deg
(度数)。
This works perfectly in Firefox but not in Chrome.这在 Firefox 中完美运行,但在 Chrome 中却不行。 In fact, in Chrome, the element doesn't have a border at all.
事实上,在 Chrome 中,元素根本没有边框。 It's like the browser simply gives up when it sees the element has an
animation
property.就像浏览器在看到元素具有
animation
属性时简单地放弃了。
Here's a Fiddle:这是一个小提琴:
https://jsfiddle.net/0nymc9ej/2/ https://jsfiddle.net/0nymc9ej/2/
body { background: black; } div { color: white; width: 300px; height: 300px; border: 5px solid; border-image: linear-gradient(0deg, #ff0000, #00ff00, #0000ff) 1 / 5px; animation: move 1s infinite; } @keyframes move { 0% { border-image: linear-gradient(0deg, #ff0000, #00ff00, #0000ff) 1 / 5px; } 25% { border-image: linear-gradient(90deg, #ff0000, #00ff00, #0000ff) 1 / 5px; } 50% { border-image: linear-gradient(180deg, #ff0000, #00ff00, #0000ff) 1 / 5px; } 75% { border-image: linear-gradient(270deg, #ff0000, #00ff00, #0000ff) 1 / 5px; } 100% { border-image: linear-gradient(360deg, #ff0000, #00ff00, #0000ff) 1 / 5px; } }
<div>This has a nice moving border in Firefox but not in Chrome</div>
Here is an idea using mask:这是一个使用面具的想法:
body { background: black; }.box { color: white; width: 300px; height: 200px; position:relative; padding:5px; /* this will control the border width */ overflow:hidden; }.box div{ content:""; position:absolute; inset:0; /* shorthand of top right bottom left */ padding: inherit; /* make the gradient visible only inside the padding area */ -webkit-mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0) padding-box; -webkit-mask-composite:destination-out; mask-composite:exclude; /**/ } /* a rotating gradient layer */.box div::before{ content:""; position:absolute; inset:-50%; background:linear-gradient(#ff0000, #00ff00, #0000ff); animation: move 1s infinite; } @keyframes move { 100% { transform:rotate(360deg); } }
<div class="box"> <div></div> This has a nice moving border</div>
In the near future you can do it using CSS variable like below (works only on Chrome and edge for now)在不久的将来,您可以使用 CSS 变量来做到这一点,如下所示(目前仅适用于 Chrome 和边缘)
@property --a{ syntax: '<angle>'; inherits: false; initial-value: 0deg; } body { background: black; } div { color: white; width: 300px; height: 200px; border: 5px solid; --a:0deg; /* fallback for FF to see the border */ border-image: linear-gradient(var(--a), #ff0000, #00ff00, #0000ff) 1 / 5px; animation: move 1s infinite; } @keyframes move { 100%{--a:360deg} }
<div>This has a nice moving border</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.