从各方面增加CSS圈

时间:2017-01-29 16:31:14

标签: javascript jquery html css

我试图通过使用javascript增加百分比来实现背景显示。

效果应该从中间开始,它应该从所有尺寸增加。

问题:百分比从中间到底部增加,实际上并没有影响到顶部。

替代:如果有人有另一种方法可以做到这一点,这样做会更有帮助,因为我希望在圆圈内显示背景图片。

尝试

window.onload = function() {
  var counting = false;

  function start(count) {
    //console.log(counting);
    if (!counting) {
      counting = true;
      $('.preloader_meter').width(count + '%');
      $('.preloader_meter').height(count + '%');
      var timer = setInterval(function() {
        if (count >= 0) {
          $('.preloader_meter').width(count + '%');
          $('.preloader_meter').height(count + '%');
          count++;
        } else {
          clearInterval(timer);
          count = arguments[0];
          counting = false;
        }
      }, 100);
    }
  }

  start(0);

};
body.preloader {
  background: none;
  visibility: hidden;
}
#preloader {
  font-family: Arial;
  font-size: 12px;
  visibility: visible;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  height: auto;
  margin: 0;
  z-index: 9999999999;
}
#preloader.preloader_number:before,
#preloader.preloader_progress:before {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-image: -webkit-radial-gradient(circle, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.01));
  background-image: -moz-radial-gradient(circle, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.01));
  background-image: -ms-radial-gradient(circle, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.01));
  background-image: -o-radial-gradient(circle, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.01));
  background-image: radial-gradient(circle, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.01));
}
#preloader.complete {
  opacity: 0;
  -webkit-transition: opacity 0.2s linear 0.5s;
  -moz-transition: opacity 0.2s linear 0.5s;
  -ms-transition: opacity 0.2s linear 0.5s;
  -o-transition: opacity 0.2s linear 0.5s;
  transition: opacity 0.2s linear 0.5s;
}
#preloader.preloader_line {
  height: 2px;
  bottom: auto;
}
/* Number Mode */

#preloader.preloader_number .preloader_percentage {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
  width: 100px;
  height: 100px;
  border-width: 1px;
  border-style: solid;
  border-radius: 50%;
  line-height: 100px;
  font-size: 20px;
  font-family: Impact, Arial;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
  text-align: center;
}
#preloader.preloader_number .preloader_percentage > div {
  position: absolute;
  top: -2px;
  right: -2px;
  bottom: -2px;
  left: -2px;
  border: 4px solid transparent;
  border-left-color: #FFFFFF;
  border-radius: 50%;
  -webkit-animation: rotate 0.8s linear infinite;
  -moz-animation: rotate 0.8s linear infinite;
  -ms-animation: rotate 0.8s linear infinite;
  -o-animation: rotate 0.8s linear infinite;
  animation: rotate 0.8s linear infinite;
}
/* Line Mode */

#preloader.preloader_line .preloader_loader {
  position: absolute;
  height: 100%;
  left: 0;
}
#preloader.preloader_line .preloader_peg {
  position: absolute;
  right: 0;
  height: 100%;
  width: 100px;
  opacity: 0.5;
}
/* Text Mode */

#preloader.preloader_text .preloader_loader {
  color: #FFFFFF;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  height: 30px;
  line-height: 30px;
  margin: auto;
  letter-spacing: 3px;
  font-size: 16px;
  white-space: nowrap;
}
#preloader.preloader_text .preloader_loader div {
  position: absolute;
  top: 0px;
  right: 0px;
  bottom: 0px;
  left: 0%;
  background-color: #000000;
  opacity: 0.7;
  filter: alpha(opacity=70);
}
/* Scale Text Mode */

#preloader.preloader_scale_text .preloader_loader {
  color: #FFFFFF;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  height: 30px;
  line-height: 30px;
  margin: auto;
  letter-spacing: 3px;
  font-size: 16px;
  white-space: nowrap;
}
#preloader.preloader_scale_text .preloader_loader span {
  display: inline-block;
  -webkit-transform: scale(0);
  -moz-transform: scale(0);
  -ms-transform: scale(0);
  -o-transform: scale(0);
  transform: scale(0);
}
#preloader.preloader_scale_text .preloader_loader span.loaded {
  -webkit-animation: scale 0.2s forwards;
  -moz-animation: scale 0.2s forwards;
  -ms-animation: scale 0.2s forwards;
  -o-animation: scale 0.2s forwards;
  animation: scale 0.2s forwards;
}
/* Logo Mode */

#preloader.preloader_logo .preloader_loader {
  position: absolute;
  left: 50%;
  top: 50%;
  margin: 0px;
  overflow: hidden;
  background-position: 50% 50%;
  background-repeat: no-repeat;
  background-size: 100%;
  border-radius: 5px;
}
#preloader.preloader_logo .preloader_loader div {
  position: absolute;
  bottom: 0%;
  left: 0px;
  right: 0px;
  height: 100%;
  opacity: 0.7;
  filter: alpha(opacity=70);
}
#preloader.preloader_logo .preloader_percentage {
  position: absolute;
  top: 50%;
  left: 50%;
  height: 40px;
  line-height: 40px;
  margin: 0px;
  color: #072E77;
  text-align: center;
  font-family: Arial;
  font-size: 12px;
  font-weight: bold;
}
/* Progress Mode */

#preloader.preloader_progress .preloader_percentage {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  color: #AAAAAA;
  color: rgba(255, 255, 255, 0.1);
  font-family: Impact, Arial;
  font-size: 20px;
  text-align: center;
}
#preloader.preloader_progress .preloader_loader {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  width: 100%;
  height: 100%;
  margin: -10px auto auto auto;
  background-color: rgba(0, 0, 0, 0.1);
}
#preloader.preloader_progress .preloader_meter {
  width: 0;
  height: 100%;
  margin: auto;
  padding: 0;
  background-color: #C76363;
  border-radius: 50%;
  position: absolute;
  top: 40%;
  left: 0;
  right: 0;
}
@-webkit-keyframes rotate {
  0% {
    -webkit-transform: rotate(0);
  }
  100% {
    -webkit-transform: rotate(360deg);
  }
}
@-moz-keyframes rotate {
  0% {
    -moz-transform: rotate(0);
  }
  100% {
    -moz-transform: rotate(360deg);
  }
}
@-ms-keyframes rotate {
  0% {
    -ms-transform: rotate(0);
  }
  100% {
    -ms-transform: rotate(360deg);
  }
}
@-o-keyframes rotate {
  0% {
    -o-transform: rotate(0);
  }
  100% {
    -o-transform: rotate(360deg);
  }
}
@keyframes rotate {
  0% {
    transform: rotate(0);
  }
  100% {
    transform: rotate(360deg);
  }
}
@-webkit-keyframes scale {
  0% {
    -webkit-transform: scale(0);
    opacity: 0;
  }
  50% {
    -webkit-transform: scale(2);
    opacity: 0.5;
  }
  100% {
    -webkit-transform: scale(1);
    opacity: 1;
  }
}
@-moz-keyframes scale {
  0% {
    -moz-transform: scale(0);
    opacity: 0;
  }
  50% {
    -moz-transform: scale(2);
    opacity: 0.5;
  }
  100% {
    -moz-transform: scale(1);
    opacity: 1;
  }
}
@-ms-keyframes scale {
  0% {
    -ms-transform: scale(0);
    opacity: 0;
  }
  50% {
    -ms-transform: scale(2);
    opacity: 0.5;
  }
  100% {
    -ms-transform: scale(1);
    opacity: 1;
  }
}
@-o-keyframes scale {
  0% {
    -o-transform: scale(0);
    opacity: 0;
  }
  50% {
    -o-transform: scale(2);
    opacity: 0.5;
  }
  100% {
    -o-transform: scale(1);
    opacity: 1;
  }
}
@keyframes scale {
  0% {
    transform: scale(0);
    opacity: 0;
  }
  50% {
    transform: scale(2);
    opacity: 0.5;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div id="preloader" class="preloader_progress" style="background-color: rgb(38, 45, 51);">
  <div class="preloader_loader">
    <div class="preloader_meter" style="width: 0%; height: 0%;"></div>
  </div>
  <div class="preloader_percentage" id="log">85%</div>
</div>

1 个答案:

答案 0 :(得分:1)

您需要根据其大小更改top圈。

$('.preloader_meter').css('top', 50 - count/2 + '%');

window.onload = function() {
  var counting = false;

  function start(count) {
    //console.log(counting);
    if (!counting) {
      counting = true;
      $('.preloader_meter').width(count + '%');
      $('.preloader_meter').height(count + '%');
      var timer = setInterval(function() {
        if (count >= 0 && count <= 100) {
          $('.preloader_meter').width(count + '%');
          $('.preloader_meter').css('top', 50 - count/2 + '%');
          $('.preloader_percentage').html(count+'%');
          $('.preloader_meter').height(count + '%');
          count++;
        } else {
          clearInterval(timer);
          count = arguments[0];
          counting = false;
        }
      }, 100);
    }
  }

  start(0);

};
body.preloader {
  background: none;
  visibility: hidden;
}
#preloader {
  font-family: Arial;
  font-size: 12px;
  visibility: visible;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  height: auto;
  margin: 0;
  z-index: 9999999999;
}
#preloader.preloader_number:before,
#preloader.preloader_progress:before {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-image: -webkit-radial-gradient(circle, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.01));
  background-image: -moz-radial-gradient(circle, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.01));
  background-image: -ms-radial-gradient(circle, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.01));
  background-image: -o-radial-gradient(circle, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.01));
  background-image: radial-gradient(circle, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.01));
}
#preloader.complete {
  opacity: 0;
  -webkit-transition: opacity 0.2s linear 0.5s;
  -moz-transition: opacity 0.2s linear 0.5s;
  -ms-transition: opacity 0.2s linear 0.5s;
  -o-transition: opacity 0.2s linear 0.5s;
  transition: opacity 0.2s linear 0.5s;
}
#preloader.preloader_line {
  height: 2px;
  bottom: auto;
}
/* Number Mode */

#preloader.preloader_number .preloader_percentage {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
  width: 100px;
  height: 100px;
  border-width: 1px;
  border-style: solid;
  border-radius: 50%;
  line-height: 100px;
  font-size: 20px;
  font-family: Impact, Arial;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
  text-align: center;
}
#preloader.preloader_number .preloader_percentage > div {
  position: absolute;
  top: -2px;
  right: -2px;
  bottom: -2px;
  left: -2px;
  border: 4px solid transparent;
  border-left-color: #FFFFFF;
  border-radius: 50%;
  -webkit-animation: rotate 0.8s linear infinite;
  -moz-animation: rotate 0.8s linear infinite;
  -ms-animation: rotate 0.8s linear infinite;
  -o-animation: rotate 0.8s linear infinite;
  animation: rotate 0.8s linear infinite;
}
/* Line Mode */

#preloader.preloader_line .preloader_loader {
  position: absolute;
  height: 100%;
  left: 0;
}
#preloader.preloader_line .preloader_peg {
  position: absolute;
  right: 0;
  height: 100%;
  width: 100px;
  opacity: 0.5;
}
/* Text Mode */

#preloader.preloader_text .preloader_loader {
  color: #FFFFFF;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  height: 30px;
  line-height: 30px;
  margin: auto;
  letter-spacing: 3px;
  font-size: 16px;
  white-space: nowrap;
}
#preloader.preloader_text .preloader_loader div {
  position: absolute;
  top: 0px;
  right: 0px;
  bottom: 0px;
  left: 0%;
  background-color: #000000;
  opacity: 0.7;
  filter: alpha(opacity=70);
}
/* Scale Text Mode */

#preloader.preloader_scale_text .preloader_loader {
  color: #FFFFFF;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  height: 30px;
  line-height: 30px;
  margin: auto;
  letter-spacing: 3px;
  font-size: 16px;
  white-space: nowrap;
}
#preloader.preloader_scale_text .preloader_loader span {
  display: inline-block;
  -webkit-transform: scale(0);
  -moz-transform: scale(0);
  -ms-transform: scale(0);
  -o-transform: scale(0);
  transform: scale(0);
}
#preloader.preloader_scale_text .preloader_loader span.loaded {
  -webkit-animation: scale 0.2s forwards;
  -moz-animation: scale 0.2s forwards;
  -ms-animation: scale 0.2s forwards;
  -o-animation: scale 0.2s forwards;
  animation: scale 0.2s forwards;
}
/* Logo Mode */

#preloader.preloader_logo .preloader_loader {
  position: absolute;
  left: 50%;
  top: 50%;
  margin: 0px;
  overflow: hidden;
  background-position: 50% 50%;
  background-repeat: no-repeat;
  background-size: 100%;
  border-radius: 5px;
}
#preloader.preloader_logo .preloader_loader div {
  position: absolute;
  top: 0%;
  left: 0px;
  right: 0px;
  height: 100%;
  opacity: 0.7;
  filter: alpha(opacity=70);
}
#preloader.preloader_logo .preloader_percentage {
  position: absolute;
  top: 50%;
  left: 50%;
  height: 40px;
  line-height: 40px;
  margin: 0px;
  color: #072E77;
  text-align: center;
  font-family: Arial;
  font-size: 12px;
  font-weight: bold;
}
/* Progress Mode */

#preloader.preloader_progress .preloader_percentage {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  color: #AAAAAA;
  color: rgba(255, 255, 255, 0.1);
  font-family: Impact, Arial;
  font-size: 20px;
  text-align: center;
}
#preloader.preloader_progress .preloader_loader {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  width: 100%;
  height: 100%;
  /* margin: -10px auto auto auto; */
  background-color: rgba(0, 0, 0, 0.1);
}
#preloader.preloader_progress .preloader_meter {
  width: 0;
  height: 100%;
  margin: auto;
  padding: 0;
  background-color: #C76363;
  border-radius: 50%;
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
}
@-webkit-keyframes rotate {
  0% {
    -webkit-transform: rotate(0);
  }
  100% {
    -webkit-transform: rotate(360deg);
  }
}
@-moz-keyframes rotate {
  0% {
    -moz-transform: rotate(0);
  }
  100% {
    -moz-transform: rotate(360deg);
  }
}
@-ms-keyframes rotate {
  0% {
    -ms-transform: rotate(0);
  }
  100% {
    -ms-transform: rotate(360deg);
  }
}
@-o-keyframes rotate {
  0% {
    -o-transform: rotate(0);
  }
  100% {
    -o-transform: rotate(360deg);
  }
}
@keyframes rotate {
  0% {
    transform: rotate(0);
  }
  100% {
    transform: rotate(360deg);
  }
}
@-webkit-keyframes scale {
  0% {
    -webkit-transform: scale(0);
    opacity: 0;
  }
  50% {
    -webkit-transform: scale(2);
    opacity: 0.5;
  }
  100% {
    -webkit-transform: scale(1);
    opacity: 1;
  }
}
@-moz-keyframes scale {
  0% {
    -moz-transform: scale(0);
    opacity: 0;
  }
  50% {
    -moz-transform: scale(2);
    opacity: 0.5;
  }
  100% {
    -moz-transform: scale(1);
    opacity: 1;
  }
}
@-ms-keyframes scale {
  0% {
    -ms-transform: scale(0);
    opacity: 0;
  }
  50% {
    -ms-transform: scale(2);
    opacity: 0.5;
  }
  100% {
    -ms-transform: scale(1);
    opacity: 1;
  }
}
@-o-keyframes scale {
  0% {
    -o-transform: scale(0);
    opacity: 0;
  }
  50% {
    -o-transform: scale(2);
    opacity: 0.5;
  }
  100% {
    -o-transform: scale(1);
    opacity: 1;
  }
}
@keyframes scale {
  0% {
    transform: scale(0);
    opacity: 0;
  }
  50% {
    transform: scale(2);
    opacity: 0.5;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div id="preloader" class="preloader_progress" style="background-color: rgb(38, 45, 51);">
  <div class="preloader_loader">
    <div class="preloader_meter" style="width: 0%; height: 0%;"></div>
  </div>
  <div class="preloader_percentage" id="log">85%</div>
</div>