【问题标题】:CSS Animation width from right to left and height from bottom to topCSS动画宽度从右到左,高度从下到上
【发布时间】:2016-12-22 11:39:43
【问题描述】:

我正在尝试解决从右到左扩展 div 的宽度和从下到上扩展不同容器的高度的问题。我正在尝试创建将以正方形旋转并模仿边框的 CSS 动画,如果有帮助,请链接到我的 CodePen https://codepen.io/Archezi/pen/xReqvq?editors=0100

这是我的 HTML .container 是一个主要包装 .circle 是一个动画 line1-line4 是我正在尝试制作动画的正方形边框。

<div class="container">
    <div class="circle "></div>
    <div class="line1  "></div>
    <div class="line2  "></div>
    <div class="line3  "></div>
    <div class="line4  "></div>
</div>

这是我的 CSS

body {
    margin: 0 auto;
}
.container {
    position:relative;
    margin: 50px auto;
    width: 800px;
    height:800px;
    border:1px solid #000;
}
.circle {
    display:inline-block;
    width: 25px;
    height: 25px;
    border-radius:50%;
    position: absolute;
    top:100px;
    left:100px;
    background:#000;
    animation: myframes 4s ease-in-out 0s infinite;

    /* animation-name: myanimation;
    animation-duration:5s;
    animation-timing-function:ease-in-out;
    animation-delay: 0s;
    animaiton-iteration-count: infinite;
    animation-direction: normal;
    animation-fill-mode: forwards;
    animation-play-state: running; */
}
.line1 {
    height:15px;
    width:15px;
    position:absolute;
    top:105px;
    left:105px;
    background:red;
    animation: squerframe 2s ease-in-out 0s infinite;
}
.line2 {
    height:15px;
    width:15px;
    position:absolute;
    top:105px;
    left:205px;
    background:green;
    animation: squerframe2 2s ease-in-out 1s infinite;
}
.line3 {
    height:15px;
    width:15px;
    position:absolute;
    top:205px;
    left:205px;
    background-color:red;
    animation: squerframe3 2s ease-in-out 2s infinite;
}

.line4 {
    height:15px;
    width:15px;
    position:absolute;
    top:205px;
    left:105px;
    background:green;
    animation: squerframe4 2s ease-in-out 3s infinite;
}
@Keyframes squerframe 
{
    0%  {  width:15px; opacity: 1; }
    50% {  width:115px; }
    100%{  width:115px; opacity: 0; }
}
@Keyframes squerframe2 
{
    0%  { height:15px;  opacity: 1; }
    50% { height:115px;             }
    100%{ height:115px;  opacity: 0;  }
}
@Keyframes squerframe3 
{
    0%  { width:115px; opacity: 0;}
    50% { width:115px; }
    100%{ width:15px; opacity: 1; }
}
@Keyframes squerframe3 
{
    0%  { width:15px; opacity: 1;}
    50% { width:115px;  }
    100%{ width:115px; opacity: 0; }
}
@Keyframes squerframe4 
{
    0%  {  height:15px; opacity: 1;}
    50% {  height:115px;  }
    100%{  height:115px; opacity: 0; }
}

@keyframes myframes 
{
    0%   { top:100px; left:100px; }
    25%  { top:100px; left:200px; }
    50%  { top:200px; left:200px; }  
    75%  { top:200px; left:100px; }
    100% { top:100px; left:100px; }
}

请指导我在哪里可以找到解决方案或指出解决此问题的不同方法。谢谢你!

【问题讨论】:

  • 抱歉,我不明白有什么问题。代码笔不工作吗?为什么需要其他解决方案?

标签: javascript jquery css


【解决方案1】:

这里的问题是line3需要绝对右边,这样当宽度变化时,它会向左拉伸。

另外,第 4 行应该有一个绝对底部,这样它就会向上伸展。

我建议你为四行添加一个容器或更改当前 div.container 的尺寸(就像我在示例中所做的那样),并将 4 行用作该容器的极端。

以下是修改后的示例,作为如何继续的参考:

https://codepen.io/anon/pen/MbRvGM?editors=0100

body {
  margin: 0 auto;
}
.container {
  position:relative;
  margin: 50px auto;
  width: 115px;
  height:115px;
  border:1px solid #000;
}
.circle {
  display:inline-block;
  width: 25px;
  height: 25px;
  border-radius:50%;
  position: absolute;
  top:0px;
  left:0px;
  background:#000;
  animation: myframes 4s ease-in-out 0s infinite;

/*   animation-name: myanimation;
  animation-duration:5s;
  animation-timing-function:ease-in-out;
  animation-delay: 0s;
  animaiton-iteration-count: infinite;
  animation-direction: normal;
  animation-fill-mode: forwards;
  animation-play-state: running; */



}
.line1 {
  height:15px;
  width:15px;
  position:absolute;
  top:0px;
  left:0px;
  background:red;
  animation: squerframe 2s ease-in-out 0s infinite;
}
.line2 {
  height:15px;
  width:15px;
  position:absolute;
  top:0px;
  left:100px;
  background:green;
  animation: squerframe2 2s ease-in-out 1s infinite;
}
.line3 {
  height:15px;
  width:15px;
  position:absolute;
  top:100px;
  right:0px;
  float: right;
  background-color:red;
  animation: squerframe3 2s ease-in-out 2s infinite;
}

.line4 {
  height:15px;
  width:15px;
  position:absolute;  
  left:0px;
  bottom: 0;
  background:green;
  animation: squerframe4 2s ease-in-out 3s infinite;
}
@Keyframes squerframe 
{
  0%  {  width:15px; opacity: 1; }
  50% {  width:115px; }
  100%{  width:115px; opacity: 0; }
}
@Keyframes squerframe2 
{
  0%  { height:15px;  opacity: 1; }
  50% { height:115px;             }
  100%{ height:115px;  opacity: 0;  }
}
@Keyframes squerframe3 
{
  0%  { width:115px; opacity: 0;}
  50% { width:115px; }
  100%{ width:15px; opacity: 1; }
 }
@Keyframes squerframe3 
{
  0%  { width:15px; opacity: 1;}
  50% { width:115px;  }
  100%{ width:115px; opacity: 0; }
}
@Keyframes squerframe4 
{
  0%  {  height:15px; opacity: 1;}
  50% {  height:115px;  }
  100%{  height:115px; opacity: 0; }
}

 @keyframes myframes 
{
   0%   { top:0px; left:0px; }
   25%  { top:0px; left:100px; }
   50%  { top:100px; left:100px; }  
   75%  { top:100px; left:0px; }
   100% { top:0px; left:0px; }
}

【讨论】:

  • 感谢您提供此解决方案。我正在考虑它,但我想知道这个问题是否有其他解决方案。感谢您的帮助,我会尝试使用它。
【解决方案2】:

添加其他样式

animation: squerframe4 2s ease-in-out 3s infinite;
-webkit-animation: squerframe4 2s ease-in-out 3s infinite;
-moz-animation: squerframe4 2s ease-in-out 3s infinite;

对于关键帧

@Keyframes squerframe...
@-webkit-Keyframes squerframe...
@-moz-Keyframes squerframe...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 2021-11-16
    • 1970-01-01
    • 2018-07-22
    • 2017-12-01
    • 1970-01-01
    相关资源
    最近更新 更多