首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将一条线转换成两条线并使用css旋转?

如何将一条线转换成两条线并使用css旋转?
EN

Stack Overflow用户
提问于 2019-06-11 07:37:37
回答 2查看 2.5K关注 0票数 3

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

我想过使用css transform属性来旋转两个旋转的两个div,但这似乎不是一个解决方案,因为您可以在我的这里笔中看到结果。

HTML:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
#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);
}

在页面加载后,如何使/这一行看起来像图像上的图标?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-11 07:54:09

您可以使用css动画来完成此操作。您可以使用rotateZ转换来创建箭头形状,也可以使用scale来保持线条的宽度随着动画的发展而增加。

对于这两个部分,您还需要使用transform-origin在正确的位置进行转换。

代码语言:javascript
复制
.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);
  }
}
代码语言:javascript
复制
<div class="line"></div>

您还可以使用svg使用line元素和一些javascript来移动y位置的左行和右行部件。要逐渐增加角度,可以使用setInterval方法。

代码语言:javascript
复制
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)
代码语言:javascript
复制
<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>

票数 2
EN

Stack Overflow用户

发布于 2019-06-11 13:38:02

虽然公认的答案工作得很好,艺术家在我不能采取重叠的线在中心,因为缩放。这里有几个备选方案:

备选方案1- clip-path

使用剪辑路径,动画的中点的矩形,以转换多边形为雪佛龙.这是通过屏蔽动画形状之外的元素的背景色来实现的。

代码语言:javascript
复制
* {
  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);
  }
}
代码语言:javascript
复制
<div class="line down"></div>
<div class="line up"></div>

然而,对clip-path的支持却参差不齐。

选项2-伪元素

如果您不能使用clip-path或更喜欢使用伪元素,请将它们的位置和转换的起源更改为来自中心(而不是上角):

代码语言:javascript
复制
* {
  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); }
}
代码语言:javascript
复制
<div class="line"></div>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56538726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档