CSS关键帧,用于重复闪烁效果(wifi)

时间:2018-12-17 21:23:39

标签: css css3 css-animations keyframe

所以我试图为一个项目创建一个Wifi眨眼效果。首先,圆圈应该可见,然后上方的圆圈,依此类推。所有符号可见后,应等待2秒钟,然后重新开始。您可以在此处查看我的当前状态:

http://jsfiddle.net/6jhgfdv0/

我在CSS中使用的是

animation: blink 3s infinite;
animation-delay: 1s;

@keyframes blink {
    0% {opacity: 0}
    49%{opacity: 0}
  50%{opacity: 1}
}

我认为只需更改延迟和其他值,我就可以使它看起来像这样,但它不起作用。我该怎么办?

body {
  margin: 0;
  padding: 0;
}
  
  #body {
    margin: 200px auto 0px;
    width: 280px;
    height: 84px;
    background-color: #c9e3ed;
    border-radius: 30px 30px 10px 10px / 20px 20px 10px 10px;
    position: relative;
 }
      
 #wifi .signal {
    border-right: 12px solid #ee7230;
    border-bottom: 12px solid #ee7230;
    border-top: 1px solid #fff;
    border-left: 1px solid #fff;
    transform: rotate(-135deg);
    position: absolute;
}
  
 #wifi .signal:first-child {
	left: 50%;
	top: -160px;
	margin-left: -56px;  
	width: 100px;
	height: 100px;
	border-radius: 0 0 100px 0;
	animation: blink 3s infinite;
	animation-delay: 1s;
}

 #wifi .signal:nth-child(2) {
	left: 50%;
	top: -130px;
	margin-left: -43px;  
	width: 74px;
	height: 74px;
	border-radius: 0 0 74px 0;
	animation: blink 3s infinite;
	animation-delay: 2s;
	}   
  
 #wifi .signal:last-child {
	left: 50%;
	top: -100px;
	margin-left: -30px;  
	width: 48px;
	height: 48px;
	border-radius: 0 0 48px 0;
	animation: blink 3s infinite;
	animation-delay: 3s;
}

 #wifi .signal:last-child:after {
	content: '';
	position: absolute;
	right: 35%;
	bottom: 35%;
	height: 30px;
	width: 30px;
	background-color: #ee7230;
	border-radius: 50%;
	margin-left: -25px;
	animation: blink 3s infinite;
	animation-delay: 4s;
}


@keyframes blink {
	0% {opacity: 0}
	49%{opacity: 0}
  50%{opacity: 1}
}
<div id="router">
  <div id="body">
    <div id="wifi">
      <div class="signal"></div>
      <div class="signal"></div>
      <div class="signal"></div>
    </div>
  </div>
</div>

2 个答案:

答案 0 :(得分:2)

创建多个动画,每个动画将以不同的百分比开始动画,然后您可以跳过动画延迟。

body {
  margin: 0;
  padding: 0;
}

#body {
  margin: 200px auto 0px;
  width: 280px;
  height: 84px;
  background-color: #c9e3ed;
  border-radius: 30px 30px 10px 10px / 20px 20px 10px 10px;
  position: relative;
}

#wifi .signal {
  border-right: 12px solid #ee7230;
  border-bottom: 12px solid #ee7230;
  border-top: 1px solid #fff;
  border-left: 1px solid #fff;
  transform: rotate(-135deg);
  position: absolute;
}

#wifi .signal:first-child {
  left: 50%;
  top: -160px;
  margin-left: -56px;
  width: 100px;
  height: 100px;
  border-radius: 0 0 100px 0;
  animation: blink4 4s infinite;
}

#wifi .signal:nth-child(2) {
  left: 50%;
  top: -130px;
  margin-left: -43px;
  width: 74px;
  height: 74px;
  border-radius: 0 0 74px 0;
  animation: blink3 4s infinite;
}

#wifi .signal:last-child {
  left: 50%;
  top: -100px;
  margin-left: -30px;
  width: 48px;
  height: 48px;
  border-radius: 0 0 48px 0;
  animation: blink2 4s infinite;
}

#wifi .signal:last-child:after {
  content: '';
  position: absolute;
  right: 35%;
  bottom: 35%;
  height: 30px;
  width: 30px;
  background-color: #ee7230;
  border-radius: 50%;
  margin-left: -25px;
  animation: blink1 4s infinite;
}

@keyframes blink1 {
  0% {
    opacity: 0
  }
  19% {
    opacity: 0
  }
  20% {
    opacity: 1
  }
}

@keyframes blink2 {
  0% {
    opacity: 0
  }
  39% {
    opacity: 0
  }
  40% {
    opacity: 1
  }
}

@keyframes blink3 {
  0% {
    opacity: 0
  }
  59% {
    opacity: 0
  }
  60% {
    opacity: 1
  }
}

@keyframes blink4 {
  0% {
    opacity: 0
  }
  79% {
    opacity: 0
  }
  80% {
    opacity: 1
  }
}
<div id="router">
  <div id="body">
    <div id="wifi">
      <div class="signal"></div>
      <div class="signal"></div>
      <div class="signal"></div>
    </div>
  </div>
</div>

答案 1 :(得分:2)

我将使用一个元素来执行此操作,以使控件更易于使用动画并减少代码量:

.wifi {
  margin:20px;
  width:290px;
  height:290px;
  display:inline-block;
  background:
    /*the center*/
    radial-gradient(circle at center, orange 20px,transparent 21px),
    /*some white to hide part of the circles*/
    linear-gradient( 45deg, #fff 50%,transparent 0),
    linear-gradient(-45deg, #fff 50%,transparent 0),
    /*--*/
    /*1*/
    radial-gradient(circle at center,
      transparent 40px,orange 41px,
      orange 61px,transparent 62px),
    /*2*/
    radial-gradient(circle at center,
      transparent 80px,orange 81px,
      orange 101px,transparent 102px),
    /*3*/
    radial-gradient(circle at center,
      transparent 120px,orange 121px,
      orange 141px,transparent 142px);
  animation:change 5s linear infinite;
}

@keyframes change {
  0%,20% {
    background-size: 0,auto, auto, 0, 0,0;
  }
  20%,40% {
    background-size: auto,auto, auto, 0, 0,0;
  }
  40%,60% {
    background-size: auto,auto, auto, auto, 0,0;
  }
  60%,80% {
    background-size: auto,auto, auto, auto, auto,0;
  }
  80%,100% {
    background-size: auto,auto, auto, auto, auto,auto;
  }
}
<div class="wifi"></div>

并带有一些CSS变量,以便更轻松地调整值:

.wifi {
  --d:20px; /*the distance between signals*/
  --l:20px; /*the lenght of the signals*/
  --s:calc(var(--l) + var(--d));
  width:calc(7*var(--s) + var(--l));
  height:calc(7*var(--s) + var(--l));
  display:inline-block;
  background:
    /*the center*/
    radial-gradient(circle at center, 
    orange var(--l),transparent calc(var(--l) + 1px)),
    /*some white to hide part of the circles*/
    linear-gradient( 45deg, #fff 50%,transparent 0),
    linear-gradient(-45deg, #fff 50%,transparent 0),
    /*--*/
    /*1*/
    radial-gradient(circle at center,
      transparent calc(1*var(--s)),
      orange      calc(1*var(--s) + 1px),
      orange      calc(1*var(--s) + var(--l)),
      transparent calc(1*var(--s) + var(--l) + 1px)),
    /*2*/
    radial-gradient(circle at center,
      transparent calc(2*var(--s)),
      orange      calc(2*var(--s) + 1px),
      orange      calc(2*var(--s) + var(--l)),
      transparent calc(2*var(--s) + var(--l) + 1px)),
    /*3*/
    radial-gradient(circle at center,
      transparent calc(3*var(--s)),
      orange      calc(3*var(--s) + 1px),
      orange      calc(3*var(--s) + var(--l)),
      transparent calc(3*var(--s) + var(--l) + 1px));
  animation:change 5s linear infinite;
}

@keyframes change {
  0%,20% {
    background-size: 0,auto, auto, 0, 0,0;
  }
  20%,40% {
    background-size: auto,auto, auto, 0, 0,0;
  }
  40%,60% {
    background-size: auto,auto, auto, auto, 0,0;
  }
  60%,80% {
    background-size: auto,auto, auto, auto, auto,0;
  }
  80%,100% {
    background-size: auto,auto, auto, auto, auto,auto;
  }
}
<div class="wifi"></div>

<div class="wifi" style="--l:10px;"></div>

<div class="wifi" style="--l:10px;--d:5px"></div>