更改半径值时 SVG 进度不起作用.如何在不同大小的情况下使用此进度
SVG progress not working when change the radius value. how to use this progress in different sizes?
我想在我的产品中使用SVG Progress作为加载。我在代码笔中的 SVG 进度下方找到了这个。当它的半径值为 90 时,它可以正常工作。如果我更改半径值,它将不起作用。javascript中的计算有误。
谢谢。
$('#percent').on('change', function(){
var val = parseInt($(this).val());
var $circle = $('#svg #bar');
if (isNaN(val)) {
val = r;
}
else{
var r = $circle.attr('r');
var c = Math.PI*(r*2);
if (val < 0) { val = 0;}
if (val > 100) { val = 100;}
var pct = ((100-val)/100)*c;
$circle.css({ strokeDashoffset: pct});
$('#cont').attr('data-pct',val);
}
});
#svg circle {
stroke-dashoffset: 0;
transition: stroke-dashoffset 1s linear;
stroke: #666;
stroke-width: 5px;
}
#svg #bar {
stroke: #FF9F1E;
}
#cont {
display: block;
height: 200px;
width: 200px;
margin: 2em auto;
border-radius: 100%;
position: relative;
}
#cont:after {
position: absolute;
display: block;
height: 160px;
width: 160px;
left: 50%;
top: 50%;
content: attr(data-pct)"%";
margin-top: -80px;
margin-left: -80px;
border-radius: 100%;
line-height: 160px;
font-size: 2em;
text-shadow: 0 0 0.5em black;
}
input {
color: #000;
}
/* Make things perty */
html { height: 100%;}
body { font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; background: url(http://www.jmchristensendesign.com/wp-content/themes/jmcdsn/images/intro_default-background.jpg); color: #fff; height: 100%; padding-top: 2em; text-align: center;}
h1, h2{ margin: 0; text-transform: uppercase;text-shadow: 0 0 0.5em black;}
h2 { font-weight: 300}
input { border: 1px solid #666; background: #333; color: #fff; padding: 0.5em; box-shadow: none; outline: none !important; margin: 1em auto; text-align: center;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="cont" data-pct="100">
<svg id="svg" width="200" height="200" viewPort="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle r="90" cx="100" cy="100" fill="transparent" stroke-dasharray="565.48" stroke-dashoffset="0"></circle>
<circle id="bar" r="90" cx="100" cy="100" fill="transparent" stroke-dasharray="565.48" stroke-dashoffset="0"></circle>
</svg>
</div>
<label for="percent">Type a percent!</label>
<input id="percent" name="percent">
如果要修改r=""
还需要调整stroke-dasharray=""
例如,请参阅我的片段:"设置stroke-dasharray
是您的工作"
<circle r="30" cx="100" cy="100" fill="transparent" stroke-dasharray="200" stroke-dashoffset="0"></circle>
<circle id="bar" r="30" cx="100" cy="100" fill="transparent" stroke-dasharray="188.50" stroke-dashoffset="0"></circle>
$('#percent').on('change', function(){
var val = parseInt($(this).val());
var $circle = $('#svg #bar');
if (isNaN(val)) {
val = r;
}
else{
var r = $circle.attr('r');
var c = Math.PI*(r*2);
if (val < 0) { val = 0;}
if (val > 100) { val = 100;}
var pct = ((100-val)/100)*c;
$circle.css({ strokeDashoffset: pct});
$('#cont').attr('data-pct',val);
}
});
#svg circle {
stroke-dashoffset: 0;
transition: stroke-dashoffset 1s linear;
stroke: #666;
stroke-width: 5px;
}
#svg #bar {
stroke: #FF9F1E;
}
#cont {
display: block;
height: 200px;
width: 200px;
margin: 2em auto;
border-radius: 100%;
position: relative;
}
#cont:after {
position: absolute;
display: block;
height: 160px;
width: 160px;
left: 50%;
top: 50%;
content: attr(data-pct)"%";
margin-top: -80px;
margin-left: -80px;
border-radius: 100%;
line-height: 160px;
font-size: 2em;
text-shadow: 0 0 0.5em black;
}
input {
color: #000;
}
/* Make things perty */
html { height: 100%;}
body { font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; background: url(http://www.jmchristensendesign.com/wp-content/themes/jmcdsn/images/intro_default-background.jpg); color: #fff; height: 100%; padding-top: 2em; text-align: center;}
h1, h2{ margin: 0; text-transform: uppercase;text-shadow: 0 0 0.5em black;}
h2 { font-weight: 300}
input { border: 1px solid #666; background: #333; color: #fff; padding: 0.5em; box-shadow: none; outline: none !important; margin: 1em auto; text-align: center;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="cont" data-pct="100">
<svg id="svg" width="200" height="200" viewPort="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle r="30" cx="100" cy="100" fill="transparent" stroke-dasharray="200" stroke-dashoffset="0"></circle>
<circle id="bar" r="30" cx="100" cy="100" fill="transparent" stroke-dasharray="188.50" stroke-dashoffset="0"></circle>
</svg>
</div>
<label for="percent">Type a percent!</label>
<input id="percent" name="percent">
相关文章:
- 为什么jQuery悬停方法在这种情况下不起作用
- 为什么push方法没有'在这种情况下不起作用:[].推
- 倒计时在某些情况下不起作用
- res.download()在我的情况下不起作用
- 为什么getElementbyId在这种情况下不起作用
- $animate.removeClass 在没有 $evalAsync inside 指令的情况下不起作用
- 为什么 array.indexOf(undefined) 在数组稀疏的情况下不起作用
- 多次点击功能在不重新加载页面的情况下不起作用
- 数据绑定在特定情况下不起作用(AngularJS)
- UI 路由加载微调器在没有$timeout的情况下不起作用
- 为什么原型JavaScript在这种情况下不起作用
- ng-重复运行次数与整数参数一样多在某些情况下不起作用
- AngularJS指令在元素具有绑定值的情况下不起作用
- 为什么隐藏/显示(向上滑动/向下滑动)在这种情况下不起作用
- $('option').mousedown 事件在少数情况下不起作用
- Angular JS自定义指令在没有ng-app指令的情况下不起作用
- Django {{ block.super }} 在特定情况下不起作用
- Ajax 请求在没有警报语句的情况下不起作用
- 替换c#函数bijJquery在不知道如何进行故障排除的情况下不起作用
- clearInterval在特定情况下不起作用(javasripct,jquery)