【发布时间】:2019-08-24 04:08:45
【问题描述】:
我正在尝试使用纯 CSS 获得类似环绕的边框动画。
我现在的做法是改变:before 和:after 伪元素的大小。一个用于顶部和右侧边框,一个用于底部和左侧。
但是,由于宽度和高度的不同,我得到了一个奇怪的效果,因为每一边都需要相同的时间,但由于宽度比高度大得多,看起来它走得更快。
如果事先不知道 div 的大小,你会如何修复它?
也欢迎使用 SCSS/vanilla CSS 获得相同动画的任何不同方法。
似乎我无法更改 SO 上 sn-p 的大小,但如果你想玩,这里有一个 codepen:https://codepen.io/lollobaldo2000/pen/KKPazNw?editors=1100
* {
box-sizing: border-box;
}
body {
background: black;
}
.square {
background: black;
display: block;
width: 500px;
height: 200px;
position: absolute;
top: 50%;
left: 50%;
margin: -100px auto auto -250px;
}
.square:before, .square:after {
content: '';
width: 0%;
height: 0%;
position: absolute;
border: 1px solid #FB0;
animation-fill-mode: forwards;
}
.square:before {
left: 0;
top: 0;
border-bottom: 0;
border-left: 0;
animation: btm 2s ease-in forwards;
}
.square:after {
visibility: hidden;
right: 0;
bottom: 0;
border-top: 0;
border-right: 0;
animation: btm 2s 2s ease-out forwards;
}
@keyframes btm {
0% {
visibility: visible;
width: 0;
height: 0;
}
50% {
width: 100%;
height: 0;
}
100% {
width: 100%;
height: calc(100% - 1px);
visibility: visible;
}
}
<div class="square">
</div>
【问题讨论】:
标签: html css css-animations