progressBar.js
!function (window) {
"use strict";
var doc = window.document,
yzhui = {};
if (typeof define === 'function') {
define(yzhui);
} else {
window.YZHUI = yzhui;
}
}(window);
/**
* ProgressBar Plugin
* Refer to: https://github.com/kimmobrunfeldt/progressbar.js.git
*/
!function (window) {
"use strict";
var doc = window.document,
util = window.YZHUI.util;
function Circle (element, options) {
this.pathTemplate = 'M 50,50 m 0,-{radius} a {radius},{radius} 0 1 1 0,{2radius} a {radius},{radius} 0 1 1 0,-{2radius}';
ProgressBar.apply(this, arguments);
}
Circle.prototype = new ProgressBar();
Circle.prototype.getPathString = function (widthOfWider) {
var _this = this,
r = 50 - widthOfWider / 2;
return _this.render(_this.pathTemplate, {
radius: r,
'2radius': r * 2
});
};
Circle.prototype.initSvg = function (svg) {
svg.setAttribute('viewBox', '0 0 100 100');
svg.style.display = 'block';
svg.style.width = '100%';
};
function Line (element, options) {
this.pathTemplate = 'M 0,{center} L 100,{center}';
ProgressBar.apply(this, arguments);
}
Line.prototype = new ProgressBar();
Line.prototype.getPathString = function (widthOfWider) {
var _this = this;
return _this.render(_this.pathTemplate, {
center: widthOfWider / 2
});
};
Line.prototype.initSvg = function (svg, options) {
svg.setAttribute('viewBox', '0 0 100 ' + options.strokeWidth);
svg.setAttribute('preserveAspectRatio', 'none');
svg.style.width = '100%';
svg.style.height = '100%';
};
function ProgressBar (element, options) {
this.$element = $(element);
this.options = $.extend({}, ProgressBar.DEFAULTS, options || {});
}
ProgressBar.DEFAULTS = {
type: 'circle',
strokeWidth: 0,
strokeColor: '#E5E5E5',
trailWidth: 0,
trailColor: '#646464',
fill: '',
progress: 0,
delay: true,
binder: window
};
ProgressBar.prototype.set = function (progress) {
var _this = this,
length = _this.trailPath.getTotalLength();
if (!progress) progress = _this.options.progress;
if (progress > 1)progress = 1;
_this.trailPath.style.strokeDashoffset = length - progress * length;
};
ProgressBar.prototype.appendView = function () {
var _this = this,
options = _this.options,
progress = options.progress,
svgView = _this.createSvgView(),
$element = _this.$element;
_this.$binder = options.binder === window || options.binder == 'window' ? $(window) : $(options.binder);
var path = svgView.trailPath,
length = path.getTotalLength();
path.style.strokeDasharray = length + ' ' + length;
var $svg = $(svgView.svg);
$svg.one('appear.yzhui.progressbar', function () {
_this.set(progress);
});
$element.append($svg);
if (options.delay) {
_this.checkInView($svg);
_this.$binder.on('scroll.yzhui.progressbar', function () {
_this.checkInView($svg);
});
$(window).on('resize', function () {
_this.checkInView($svg);
});
} else {
$svg.trigger('appear.yzhui.progressbar');
}
return this;
};
ProgressBar.prototype.checkInView = function ($svg) {
var _this = this,
$binder = _this.$binder,
contentHeight = $binder.height(),
contentTop = $binder.get(0) === window ? $(window).scrollTop() : $binder.offset().top;
var post = $svg.offset().top - contentTop,
posb = post + $svg.height();
if ((post >= 0 && post < contentHeight) || (posb > 0 && posb <= contentHeight)) {
$svg.trigger('appear.yzhui.progressbar');
}
};
ProgressBar.prototype.createSvgView = function () {
var _this = this,
options = _this.options;
var svg = doc.createElementNS('http://www.w3.org/2000/svg', 'svg');
_this.initSvg(svg, options);
var path = _this.createPath(options);
svg.appendChild(path);
var trailPath = null;
if (options.trailColor || options.trailWidth) {
trailPath = _this.createTrailPath(options);
trailPath.style.strokeDashoffset = trailPath.getTotalLength();
svg.appendChild(trailPath);
}
_this.svg = svg;
_this.trailPath = trailPath;
return {
svg: svg,
trailPath: trailPath
}
};
ProgressBar.prototype.createTrailPath = function (options) {
var _this = this;
if (options.trailWidth == 0) {
options.trailWidth = options.strokeWidth;
}
var pathString = _this.getPathString(options.trailWidth);
return _this.createPathElement(pathString, options.trailColor, options.trailWidth);
};
ProgressBar.prototype.createPath = function (options) {
var _this = this,
width = options.strokeWidth;
if (options.trailWidth && options.trailWidth > options.strokeWidth) {
width = options.trailWidth;
}
var pathString = _this.getPathString(width);
return _this.createPathElement(pathString, options.strokeColor, options.strokeWidth, options.fill);
};
ProgressBar.prototype.createPathElement = function (pathString, color, width, fill) {
var path = doc.createElementNS('http://www.w3.org/2000/svg', 'path');
path.setAttribute('d', pathString);
path.setAttribute('stroke', color);
path.setAttribute('stroke-width', width);
if (fill) {
path.setAttribute('fill', fill);
} else {
path.setAttribute('fill-opacity', '0');
}
return path;
};
ProgressBar.prototype.render = function (template, vars) {
var rendered = template;
for (var key in vars) {
if (vars.hasOwnProperty(key)) {
var val = vars[key];
var regExpString = '\\{' + key + '\\}';
var regExp = new RegExp(regExpString, 'g');
rendered = rendered.replace(regExp, val);
}
}
return rendered;
};
function Plugin (option) {
var args = Array.prototype.slice.call(arguments, 1);
return this.each(function () {
var $this = $(this),
progressbar = $this.data('yzhui.progressbar');
if (!progressbar) {
if (option.type == 'line') {
$this.data('yzhui.progressbar', (progressbar = new Line(this, option)));
} else {
$this.data('yzhui.progressbar', (progressbar = new Circle(this, option)));
}
if (!option || typeof option == 'object') {
progressbar.appendView();
}
}
if (typeof option == 'string') {
progressbar[option] && progressbar[option].apply(progressbar, args);
}
});
}
$('[data-yzhui-progressbar]').each(function () {
var $this = $(this);
Plugin.call($this, util.parseOptions($this.data('yzhui-progressbar')));
});
$.fn.progressBar = Plugin;
}(window);
사용 방법
//cup
$('#js-ProgressCpu').progressBar({
type: 'circle',
strokeWidth:10,
strokeColor: '#dde2eb',
trailWidth: 10,
trailColor: '#59cd6f',
fill: '#f1f3f7',
progress: .4
});
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.