首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >一个一个地改变div不透明度来制作动画

一个一个地改变div不透明度来制作动画
EN

Stack Overflow用户
提问于 2016-10-05 11:09:51
回答 5查看 138关注 0票数 2

我试着用四个方框来创建一个动画,每个方块都应该一个一个地改变不透明度来制作一个加载类型的动画,我试过用CSS,但无法实现,有人能帮助这个CSS动画吗?

这是工作的JSfiddle

代码语言:javascript
复制
body, html {
  width: 100%;
  height: 100%;
}
body {
  background-color: #efefef;
}
.wrapper {
  height: 40px;
  width: 40px;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -22.5px;
  margin-left: -22.5px;
}
ul {
  list-style-type: none;
  margin: 0 auto;
  padding: 0;
  width: 80px;
  height: 80px;
  position: relative;
  -moz-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
  transform: rotate(45deg);
}
ul li {
  width: 2em;
  height: 2em;
  position: absolute;
  
  /*animation: dance 888ms infinite alternate;
  animation-timing-function: cubic-bezier(0.5, 0, 0.5, 1);*/
  -moz-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
  transform: rotate(45deg);
}
.block-1 {
  top: 0;
  left: 0;
  background:#0076aa;
}
.block-2 {
  top: 0;
  right: 0;
  background:#98bd81; 
}
.block-3 {
  bottom: 0;
  right: 0;
  background:#98bd81;
}
.block-4 {
  bottom: 0;
  left: 0;
  background:#0076aa;
}
li.block-1 {
  animation-delay: 222ms;
}
li.block-2 {
  animation-delay: 444ms;
}
li.block-3 {
  animation-delay: 666ms;
}
li.block-4 {
  animation-delay: 888ms;
}
@keyframes dance {
  to {
	-moz-transform: scale(1);
	-ms-transform: scale(1);
	-webkit-transform: scale(1);
	transform: scale(1);
	
  }
}
代码语言:javascript
复制
<div class='wrapper'>
  <ul class='blocks'>
    <li class='block-1'></li>
    <li class='block-2'></li>
    <li class='block-3'></li>
    <li class='block-4'></li>
  </ul>
</div>

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2016-10-05 11:20:06

这是在修复您的代码之后:

代码语言:javascript
复制
@-webkit-keyframes dance {
      from {
        opacity: 1
      }
      to {
        opacity: 0.1
      }
}

li.block-1 {
      animation: dance 1s infinite;
      animation-delay: 222ms;
      animation-direction: alternate;
    }

演示

您可以根据您的偏好微调该值:)

票数 3
EN

Stack Overflow用户

发布于 2016-10-05 11:22:32

您需要将动画调用添加到您的li元素,否则它将无法运行。

使用animation速记属性可以做到这一点。

代码语言:javascript
复制
body, html {
  width: 100%;
  height: 100%;
}

body {
  background-color: #efefef;
}

.wrapper {
  height: 40px;
  width: 40px;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -22.5px;
  margin-left: -22.5px;
}

ul {
  list-style-type: none;
  margin: 0 auto;
  padding: 0;
  width: 80px;
  height: 80px;
  position: relative;
  -moz-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
  transform: rotate(45deg);
}
ul li {
  width: 2em;
  height: 2em;
  position: absolute;
  
  /*animation: dance 888ms infinite alternate;
  animation-timing-function: cubic-bezier(0.5, 0, 0.5, 1);*/
  -moz-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
  transform: rotate(45deg);
  animation: dance 888ms infinite alternate;
}

.block-1 {
  top: 0;
  left: 0;
  background:#0076aa;
  
}

.block-2 {
  top: 0;
  right: 0;
  background:#98bd81;
  
  
}

.block-3 {
  bottom: 0;
  right: 0;
  background:#98bd81;
  
  
}

.block-4 {
  bottom: 0;
  left: 0;
  background:#0076aa;
}

li.block-1 {
  animation-delay: 222ms;
}

li.block-2 {
  animation-delay: 444ms;
}

li.block-3 {
  animation-delay: 666ms;
}

li.block-4 {
  animation-delay: 888ms;
}

@keyframes dance {
  from {
    opacity: 1;
  }
  to {
    opacity: 0.2;
  }
}
代码语言:javascript
复制
<div class='wrapper'>
  <ul class='blocks'>
    <li class='block-1'></li>
    <li class='block-2'></li>
    <li class='block-3'></li>
    <li class='block-4'></li>
  </ul>
</div>

票数 2
EN

Stack Overflow用户

发布于 2016-10-05 12:08:29

这对我有用..。

代码语言:javascript
复制
    body, html {
  width: 100%;
  height: 100%;
}

body {
  background-color: #efefef;
}

.wrapper {
  height: 40px;
  width: 40px;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -22.5px;
  margin-left: -22.5px;
}

ul {
  list-style-type: none;
  margin: 0 auto;
  padding: 0;
  width: 80px;
  height: 80px;
  position: relative;
  -moz-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
  transform: rotate(45deg);
}
ul li {
  width: 2em;
  height: 2em;
  position: absolute;

  -moz-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
  transform: rotate(45deg);
}

.block-1 {
  top: 0;
  left: 0;
  background:#0076aa;


}

.block-2 {
  top: 0;
  right: 0;
  background:#98bd81;


}

.block-3 {
  bottom: 0;
  right: 0;
  background:#98bd81;


}

.block-4 {
  bottom: 0;
  left: 0;
  background:#0076aa;

}

li.block-1 {
  animation: dance 1.5s ease-out infinite;
  animation-delay: 0.91s;


}

li.block-2 {
animation: dance 1.5s ease-out infinite;
animation-delay: 1.54s;

}

li.block-3 {
animation: dance 1.5s ease-out infinite;
animation-delay: 1.77s;

}

li.block-4 {
 animation: dance 1.5s ease-out infinite; 
 animation-delay: 1.99s;

}

@keyframes dance {
    50%{
        opacity: 0;
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39872370

复制
相关文章

相似问题

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