javascript - 循环进度条未停在所需的百分比

标签 javascript html css progress-bar

我正在为我的元素创建一个循环进度条。

Desired Progress Circle

我已经在 html css 和 jquery 中完成了以下工作,但问题是我不知道如何以所需的百分比停止它。它总是完成到 100%。我想在特定点上停止它。

HTML/CSS JS

function setProgress(elem, percent) {
    var
      degrees = percent * 3.6,
      transform = /MSIE 9/.test(navigator.userAgent) ? 'msTransform' : 'transform';
    elem.querySelector('.counter').setAttribute('data-percent', Math.round(percent));
    elem.querySelector('.progressEnd').style[transform] = 'rotate(' + degrees + 'deg)';
    elem.querySelector('.progress').style[transform] = 'rotate(' + degrees + 'deg)';
    if (percent >= 50 && !/(^|\s)fiftyPlus(\s|$)/.test(elem.className))
        elem.className += ' fiftyPlus';
}

(function () {
    var
      elem = document.querySelector('.circlePercent'),
      percent = 0;
    (function animate() {
        setProgress(elem, (percent += .25));
        if (percent < 100)
            setTimeout(animate, 15);
    })();
})();
.circlePercent {
  position: relative;
  top: 26px;
  left: 26px;
  width: 96px;
  height: 96px;
  border-radius: 50%;
  background: orange;
}
.circlePercent:before,
.circlePercent > .progressEnd {
  position: absolute;
  z-index: 3;
  top: 2px;
  left: 45px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: white;
  -ms-transform-origin: 3px 46px;
  transform-origin: 3px 46px;
  content: "";
}
.circlePercent:after,
.circlePercent > .progress {
  position: absolute;
  -ms-transform-origin: 48px 48px;
  transform-origin: 48px 48px;
  z-index: 0;
  top: 0;
  left: 0;
  width: 48px;
  height: 96px;
  border-radius: 48px 0 0 48px;
  background: orange;
  content: "";
}
.circlePercent.fiftyPlus:after {
  background: white;
  -ms-transform: rotate(180deg);
  transform: rotate(180deg);
}
.circlePercent > .progress.progress {
  background: white;
}
.circlePercent > .counter {
  position: absolute;
  box-sizing: border-box;
  z-index: 2;
  width: 100px;
  height: 100px;
  margin-top: -2px;
  margin-left: -2px;
  border-radius: 50%;
  border: 4px solid orange;
}
.circlePercent > .counter:before {
  position: absolute;
  z-index: 1;
  top: 50%;
  margin-top: -13px;
  width: 100%;
  height: 26px;
  font-size: 26px;
  line-height: 26px;
  font-family: sans-serif;
  text-align: center;
  color: white;
  content: attr(data-percent) "%";
}
.circlePercent > .counter:after {
  position: absolute;
  width: 80px;
  height: 80px;
  top: 6px;
  left: 6px;
  border-radius: 50%;
  background: orange;
  content: "";
}
.circlePercent > .counter[data-percent="100"] {
  background: white;
}
<div class="circlePercent">
            <div class="counter" data-percent="0"></div>
            <div class="progress"></div>
            <div class="progressEnd"></div>
        </div>
        <script src="scripts/radialloader.js"></script>

请帮我摆脱困境。

最佳答案

更新了您的代码,现在我已将变量 stopPercent 设置为 50。所以加载将在 50% 时停止。这是您需要的吗?

function setProgress(elem, percent) {
    var
      degrees = percent * 3.6,
      transform = /MSIE 9/.test(navigator.userAgent) ? 'msTransform' : 'transform';
    elem.querySelector('.counter').setAttribute('data-percent', Math.round(percent));
    elem.querySelector('.progressEnd').style[transform] = 'rotate(' + degrees + 'deg)';
    elem.querySelector('.progress').style[transform] = 'rotate(' + degrees + 'deg)';
    if (percent >= 50 && !/(^|\s)fiftyPlus(\s|$)/.test(elem.className))
        elem.className += ' fiftyPlus';
}

   (function () {
var elem = document.querySelector('.circlePercent'),percent = 0,stopped = false,stopPercent = 50;
(function animate() {
    setProgress(elem, (percent += .25));
    if (percent < 100 && !stopped)
        setTimeout(animate, 15);
		if(percent == stopPercent){
			stopped = true;
		}
})();
})();
.circlePercent {
  position: relative;
  top: 26px;
  left: 26px;
  width: 96px;
  height: 96px;
  border-radius: 50%;
  background: orange;
}
.circlePercent:before,
.circlePercent > .progressEnd {
  position: absolute;
  z-index: 3;
  top: 2px;
  left: 45px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: white;
  -ms-transform-origin: 3px 46px;
  transform-origin: 3px 46px;
  content: "";
}
.circlePercent:after,
.circlePercent > .progress {
  position: absolute;
  -ms-transform-origin: 48px 48px;
  transform-origin: 48px 48px;
  z-index: 0;
  top: 0;
  left: 0;
  width: 48px;
  height: 96px;
  border-radius: 48px 0 0 48px;
  background: orange;
  content: "";
}
.circlePercent.fiftyPlus:after {
  background: white;
  -ms-transform: rotate(180deg);
  transform: rotate(180deg);
}
.circlePercent > .progress.progress {
  background: white;
}
.circlePercent > .counter {
  position: absolute;
  box-sizing: border-box;
  z-index: 2;
  width: 100px;
  height: 100px;
  margin-top: -2px;
  margin-left: -2px;
  border-radius: 50%;
  border: 4px solid orange;
}
.circlePercent > .counter:before {
  position: absolute;
  z-index: 1;
  top: 50%;
  margin-top: -13px;
  width: 100%;
  height: 26px;
  font-size: 26px;
  line-height: 26px;
  font-family: sans-serif;
  text-align: center;
  color: white;
  content: attr(data-percent) "%";
}
.circlePercent > .counter:after {
  position: absolute;
  width: 80px;
  height: 80px;
  top: 6px;
  left: 6px;
  border-radius: 50%;
  background: orange;
  content: "";
}
.circlePercent > .counter[data-percent="100"] {
  background: white;
}
<div class="circlePercent">
            <div class="counter" data-percent="0"></div>
            <div class="progress"></div>
            <div class="progressEnd"></div>
        </div>
        <script src="scripts/radialloader.js"></script>

关于javascript - 循环进度条未停在所需的百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36324285/

相关文章:

html - 我有一个表单设计问题。我做错了什么,下边缘不齐平

javascript - React.js Bootstrap 添加或删除输入字段

javascript - WordPress 的 wp_enqueue_script 问题

javascript - javascript中特色轮播 slider 的响应式设计

javascript - 如何在 SJCL 中实现公钥/私钥加密?

javascript - 使用 "<select></select>"表单调整 html5 Canvas 的大小

html - 如何通过单击按钮来改变按钮的图像?

css - 将 webkit 滚动条样式应用于指定元素

JavaScript:未捕获类型错误:无法读取 null 的属性 'style'

javascript - 如何使用php查找用户一次登录不同的浏览器或不同的系统