我希望在标题中有一条直线,然后在页面加载几秒钟后,我希望这些行慢慢向下移动,直到它们看起来像下面的图像中的那样:

我想过使用css transform属性来旋转两个旋转的两个div,但这似乎不是一个解决方案,因为您可以在我的这里笔中看到结果。
HTML:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<div id="big">
<div class="arrow-box">
<div class="line line-1"></div>
<div class="line line-2"></div>
</div>
</div>CSS:
#big{
background: red;
height: 200px;
}
.arrow-box{
max-width: 200px;
margin: 0 auto;
padding-top: 10px;
}
.line{
background: white;
width: 60px;
height: 1px;
}
.line-1{
transform: rotate(20deg);
}
.line-2{
transform: rotate(-20deg);
}在页面加载后,如何使/这一行看起来像图像上的图标?
发布于 2019-06-11 07:54:09
您可以使用css动画来完成此操作。您可以使用rotateZ转换来创建箭头形状,也可以使用scale来保持线条的宽度随着动画的发展而增加。
对于这两个部分,您还需要使用transform-origin在正确的位置进行转换。
.line {
position: relative;
width: 100px;
}
.line:after,
.line:before {
background: black;
position: absolute;
content: "";
height: 2px;
width: 50%;
bottom: 0;
}
.line:before {
left: 0;
animation: moveBefore 1s linear forwards;
transform-origin: center left;
}
.line:after {
right: 0;
animation: moveAfter 1s linear forwards;
transform-origin: center right;
}
@keyframes moveBefore {
0% {
transform: rotateZ(0) scale(1, 1);
}
50% {
transform: rotateZ(15deg) scale(1.05, 1);
}
100% {
transform: rotateZ(30deg) scale(1.16, 1);
}
}
@keyframes moveAfter {
0% {
transform: rotateZ(0) scale(1, 1);
}
50% {
transform: rotateZ(-15deg) scale(1.05, 1);
}
100% {
transform: rotateZ(-30deg) scale(1.16, 1);
}
}<div class="line"></div>
您还可以使用svg使用line元素和一些javascript来移动y位置的左行和右行部件。要逐渐增加角度,可以使用setInterval方法。
let step = 0;
const left = document.querySelector('.left-line');
const right = document.querySelector('.right-line');
function move(el, prop, size) {
el.setAttribute(prop, +el.getAttribute(prop) + size);
}
setInterval(() => {
if (step <= 40) {
move(left, 'y2', 0.8);
move(right, 'y1', 0.8)
step += 1;
}
}, 30)<svg xmlns="http://www.w3.org/2000/svg">
<line class="left-line" x1="0" y1="20" x2="40" y2="20" stroke="black" />
<line class="right-line" x1="40" y1="20" x2="80" y2="20" stroke="black" />
</svg>
发布于 2019-06-11 13:38:02
虽然公认的答案工作得很好,艺术家在我不能采取重叠的线在中心,因为缩放。这里有几个备选方案:
备选方案1- clip-path
使用剪辑路径,动画的中点的矩形,以转换多边形为雪佛龙.这是通过屏蔽动画形状之外的元素的背景色来实现的。
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.line {
display: inline-block;
width: 200px;
height: 50px;
background-color: black;
clip-path: polygon(0 0, 100% 0, 100% 2px, 0 2px);
animation: 2s infinite linear;
}
.line.down {
animation-name: chevron-down;
}
.line.up {
animation-name: chevron-up;
}
@keyframes chevron-down {
from {
clip-path: polygon(0 0, 50% 0, 100% 0, 100% 2px, 50% 2px, 0 2px);
}
to {
clip-path: polygon(0 0, 50% 48px, 100% 0, 100% 2px, 50% 50px, 0 2px);
}
}
@keyframes chevron-up {
from {
clip-path: polygon(0 48px, 50% 48px, 100% 48px, 100% 50px, 50% 50px, 0 50px);
}
to {
clip-path: polygon(0 0, 50% 48px, 100% 0, 100% 2px, 50% 50px, 0 2px);
}
}<div class="line down"></div>
<div class="line up"></div>
然而,对clip-path的支持却参差不齐。
选项2-伪元素
如果您不能使用clip-path或更喜欢使用伪元素,请将它们的位置和转换的起源更改为来自中心(而不是上角):
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.line {
position: relative;
width: 200px;
height: 50px;
overflow: hidden;
}
.line::before,
.line::after {
position: absolute;
content: '';
display: block;
bottom: 0;
height: 2px;
width: 50%;
background-color: black;
animation: 2s linear infinite;
}
.line::before {
transform-origin: bottom right;
left: 0;
animation-name: before;
}
.line::after {
transform-origin: bottom left;
right: 0;
animation-name: after;
}
@keyframes before {
to { transform: rotateZ(30deg); }
}
@keyframes after {
to { transform: rotateZ(-30deg); }
}<div class="line"></div>
https://stackoverflow.com/questions/56538726
复制相似问题