css - 流动电路动画

标签 css

enter image description here

我的网站有一个只有边距的 div,当页面加载时,我想要一个电路效果(我让电池重叠在 div 上)所以从正极开始,电路的颜色变为黄色,然后黄色继续去负极端子。这对 CSS 是否可行?我不完全确定 CSS 的强大功能。

这是我的 div 标签的代码。

#circuit {
  width: 80%;
  border: 10px solid navy;
  margin: 25px;
  height: 80%;
  margin-left: auto;
  margin-right: auto;
}
html,
body {
  height: 100%;
  margin: 0;
}
<div id="circuit"></div>

最佳答案

一系列关键帧动画可以完成这项工作。您需要修改持续时间和累积延迟以获得您想要的速度。

Fiddle demo

    html, body {
    height: 100%;
    padding: 0;
    margin: 0;
}
* {
    box-sizing: border-box;
}
.circuit-outer {
    position: relative;
    width: 80vw;
    height: 80vh;
    margin: 5% 10% 0;
    padding: 3px;
    background: #eee;
    border: 1px solid #ddd;
    overflow: hidden;
}
.circuit-inner {
    position: absolute;
    top: 3px;
    bottom: 3px;
    left: 3px;
    right: 3px;
    background: #fff;
    border: 1px solid #ddd;
}
.current {
    background: orange;
    position: absolute;
    -webkit-animation-iteration-count: 1;
    -webkit-animation-timing-function: linear;
    -webkit-animation-fill-mode: forwards;
    animation-iteration-count: 1;
    animation-timing-function: linear;
    animation-fill-mode: forwards;
}
.current.bottom-left {
    bottom: 0;
    right: 80%;
    width: 20%;
    height: 3px;
    -webkit-animation-name: zap1;
    -webkit-animation-duration: .2s;
    animation-name: zap1;
    animation-duration: .2s;
}
.current.left {
    bottom: 0;
    left: 0;
    width: 3px;
    height: 0;
    -webkit-animation-name: zap2;
    -webkit-animation-delay: .2s;
    -webkit-animation-duration: .5s;
    animation-name: zap2;
    animation-delay: .2s;
    animation-duration: .5s;
}
.current.top {
    top: 0;
    width: 0;
    height: 3px;
    -webkit-animation-name: zap3;
    -webkit-animation-delay: .7s;
    -webkit-animation-duration: 1s;
    animation-name: zap3;
    animation-delay: .7s;
    animation-duration: 1s;
}
.current.right {
    top: 0;
    right: 0;
    width: 3px;
    height: 0;
    -webkit-animation-name: zap2;
    -webkit-animation-delay: 1.7s;
    -webkit-animation-duration: .5s;
    animation-name: zap2;
    animation-delay: 1.7s;
    animation-duration: .5s;
}
.current.bottom-right {
    bottom: 0;
    right: 0;
    width: 0%;
    height: 3px;
    -webkit-animation-name: zap4;
    -webkit-animation-delay: 2.2s;
    -webkit-animation-duration: .8s;
    animation-name: zap4;
    animation-delay: 2.2s;
    animation-duration: .8s;
}
@-webkit-keyframes zap1 {
    0% {
        width: 0;
    }
    100% {
        width: 20%;
    }
}
@keyframes zap1 {
    0% {
        width: 0;
    }
    100% {
        width: 20%;
    }
}
@-webkit-keyframes zap2 {
    0% {
        height: 0;
    }
    100% {
        height: 100%;
    }
}
@keyframes zap2 {
    0% {
        height: 0;
    }
    100% {
        height: 100%;
    }
}
@-webkit-keyframes zap3 {
    0% {
        width: 0;
    }
    100% {
        width: 100%;
    }
}
@keyframes zap3 {
    0% {
        width: 0;
    }
    100% {
        width: 100%;
    }
}
@-webkit-keyframes zap4 {
    0% {
        width: 0;
    }
    100% {
        width: 80%;
    }
}
@keyframes zap4 {
    0% {
        width: 0;
    }
    100% {
        width: 80%;
    }
}
<div class="circuit-outer">
  <div class="current bottom-left"></div>
  <div class="current left"></div>
  <div class="current top"></div>
  <div class="current right"></div>
  <div class="current bottom-right"></div>
  <div class="circuit-inner"></div>
</div>

关于css - 流动电路动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30968795/

相关文章:

css - 在固定宽度布局中,我应该再次定义包装内元素的宽度,如页眉、页脚、横幅等吗?

javascript - HTML/CSS : display circular divs in horizontal scrolling lineup? Div 出现在彼此之上?

html - 在图片右上角设置关闭图标

javascript使用选择框填充其他选择框

HTML/CSS : how to force text within a tag to be styled with font-style: italic ! 重要;

html - Firefox 中的负定位绝对元素

javascript - 播放嵌入的 YT 时停止 CSS 动画

html - 悬停时避免改变我的旗帜的颜色

javascript - 使下拉菜单在单击 JavaScript 时消失

html - 100 个表单元素水平展开 bootstrap