css keyframe transform not rotating more than 180 degrees

旧时模样 提交于 2019-12-10 23:13:08

问题


I am trying to build a donut chart with css. I am observing that it is unable to rotate more than 180 degrees. Am I missing anything.

This stops me to show donut chart for any data which is more than 50%.

http://jsfiddle.net/BkJY7/80/

@-webkit-keyframes rotate-rt {
  0% {    -webkit-transform: rotate(0deg); }
  25% {   -webkit-transform: rotate(360deg); }
  100% {   -webkit-transform: rotate(360deg); }
}

回答1:


You are missing the keyframes for rotate-lt.

Also, some minor adjustments on the angles:

body {
  margin: 50px;
}
.spinner {
  width: 250px;
  height: 250px;
  background: #aaa;
  margin: 0 auto;
  position: relative;
}
.spinner:after {
  position: absolute;
  content: "";
  width: 0%;
  height: 0%;
  border-radius: 100%;
  background: #fff;
  top: 10%;
  left: 10%;
}

.spinner span em {
  background: #0e728e;
  -webkit-animation-duration: 6s; 
}


@-webkit-keyframes rotate-rt {
  0% {    -webkit-transform: rotate(0deg); }
  50% {   -webkit-transform: rotate(180deg); }
  100% {   -webkit-transform: rotate(180deg); }
}
@-webkit-keyframes rotate-lt {
  0% {    -webkit-transform: rotate(0deg); }
  50% {   -webkit-transform: rotate(0deg); }
  100% {   -webkit-transform: rotate(180deg); }
}


.spinner {
  border-radius: 100%;
  position: relative;
}

.spinner span {
  width: 50%;
  height: 100%;
  overflow: hidden;
  position: absolute;
}

.spinner span:first-child {
  left: 0;
}

.spinner span:last-child {
  left: 50%;
}

.spinner span em {
  border-radius: 250px;
  position: absolute;
  width: 100%;
  height: 100%;
  -webkit-animation-iteration-count: 1;
  -webkit-animation-timing-function: linear;
  -webkit-animation-fill-mode: forwards;
}

.spinner span:first-child em {
  left: 100%;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
  -webkit-animation-name: rotate-lt; 
  -webkit-transform-origin: 0 50%;
}

.spinner span:last-child em {
  left: -100%;
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
  -webkit-animation-name: rotate-rt; 
  -webkit-transform-origin: 100% 50%;
 }
<div class="spinner">
  <span><em></em></span>
  <span><em></em></span>
</div>



回答2:


I would try to use this from css-tricks to achieve what you want: https://codepen.io/HugoGiraudel/pen/BHEwo

Tutorial: https://css-tricks.com/css-pie-timer/

html:

<div class="wrapper">
  <div class="pie spinner"></div>
  <div class="pie filler"></div>
  <div class="mask"></div>
</div>

css:

  .wrapper {
  position: relative;
  margin: 40px auto;
  background: white;
}

.wrapper, .wrapper * {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

.wrapper {
  width: 250px;
  height: 250px;
}

.wrapper .pie {
  width: 50%;
  height: 100%;
  transform-origin: 100% 50%;
  position: absolute;
  background: #08C;
  border: 5px solid rgba(0,0,0,0.5);
}

.wrapper .spinner {
  border-radius: 100% 0 0 100% / 50% 0 0 50%;
  z-index: 200;
  border-right: none;
  animation: rota 5s linear infinite;
}

.wrapper:hover .spinner,
.wrapper:hover .filler,
.wrapper:hover .mask {
  animation-play-state: running;
}

.wrapper .filler {
  border-radius: 0 100% 100% 0 / 0 50% 50% 0;
  left: 50%;
  opacity: 0;
  z-index: 100;
  animation: opa 5s steps(1, end) infinite reverse;
  border-left: none;
}

.wrapper .mask {
  width: 50%;
  height: 100%;
  position: absolute;
  background: inherit;
  opacity: 1;
  z-index: 300;
  animation: opa 5s steps(1, end) infinite;
}

@keyframes rota {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
@keyframes opa {
  0% {
    opacity: 1;
  }
  50%, 100% {
    opacity: 0;
  }
}

Also you can check this out also nice tutorial: http://javabeat.net/pie-chart-css3-html/

Keep in mind I take no credit for writing this code, just helpin.




回答3:


you add keyframe only for rotate-rt that why its rotate half add a keyframe for rotate-lt so get the better result

@-webkit-keyframes rotate-lt {
  0% {    -webkit-transform: rotate(0deg); }
  25% {   -webkit-transform: rotate(180deg); }
  100% {   -webkit-transform: rotate(360deg); }
}


来源:https://stackoverflow.com/questions/46371468/css-keyframe-transform-not-rotating-more-than-180-degrees

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!