这是我的代码:
https://jsfiddle.net/rzcos32n/
我有此代码:
function isItANumber(el) {
var element1 = document.getElementById("circle").innerHTML;
var percent = parseFloat(element1)*100;
percent+='%';
var increased = parseInt(el.innerText);
var element = document.getElementById("Value");
if (isNaN(increased) || element1 >= 0) {
document.getElementById("Result").innerHTML = "KanBan";
document.getElementById("circle").style.marginLeft = percent;
};
if (isNaN(increased) || element1 >= 0.33) {
document.getElementById("Result").innerHTML = "ScrumBan";
document.getElementById("circle").style.marginLeft = percent;
};
if (isNaN(increased) || element1 >= 0.66) {
document.getElementById("Result").innerHTML = "Scrum";
document.getElementById("circle").style.marginLeft = percent;
}
if (isNaN(increased) || element1 > 0.9) {
document.getElementById("Result").innerHTML = "Scrum";
document.getElementById("circle").style.marginRight = '0%';
}
}
var element = document.getElementById("circle");
isItANumber(element);
#line {
width: 100%;
/* 2 */
height: 5px;
background: gray;
position:relative;
margin-top: 30px;
}
#circle {
height: 50px;
width: 50px;
border-radius: 50%;
background: blue;
text-align: center;
color: white;
font-weight: 700;
line-height: 50px;
font-size: 14px;
position:absolute;
margin-top:-22.5px;
}
<div id="Result"></div>
<div id="line">
<div id="circle">
1.0
</div>
</div>
我根据圆中的百分比值水平设置圆在直线上的位置。因此,如果它是“ 1”,则表示100%。
我通过为它分配一个margin-left来做到这一点,它对大多数值都非常适用,除非它的高值(如1)将圆圈完全推离页面。
有没有办法让它始终留在页面上,即使它的左边距为0%或100%也是如此?
答案 0 :(得分:1)
我以这种方式添加了var percent = "calc("+parseFloat(element1)*100+"% - 50px)";
,您获得了100%的收益,但是我们必须减去圆宽度(50像素)
function isItANumber(el) {
var element1 = document.getElementById("circle").innerHTML;
var percent = "calc("+parseFloat(element1)*100+"% - 50px)";
var increased = parseInt(el.innerText);
var element = document.getElementById("Value");
if (isNaN(increased) || element1 >= 0) {
document.getElementById("Result").innerHTML = "KanBan";
document.getElementById("circle").style.marginLeft = percent;
};
if (isNaN(increased) || element1 >= 0.33) {
document.getElementById("Result").innerHTML = "ScrumBan";
document.getElementById("circle").style.marginLeft = percent;
};
if (isNaN(increased) || element1 >= 0.66) {
document.getElementById("Result").innerHTML = "Scrum";
document.getElementById("circle").style.marginLeft = percent;
}
if (isNaN(increased) || element1 > 0.9) {
document.getElementById("Result").innerHTML = "Scrum";
document.getElementById("circle").style.marginRight = '0%';
}
}
var element = document.getElementById("circle");
isItANumber(element);
#line {
width: 100%;
/* 2 */
height: 5px;
background: gray;
position:relative;
margin-top: 30px;
}
#circle {
height: 50px;
width: 50px;
border-radius: 50%;
background: blue;
text-align: center;
color: white;
font-weight: 700;
line-height: 50px;
font-size: 14px;
position:absolute;
margin-top:-22.5px;
}
<div id="Result"></div>
<div id="line">
<div id="circle">
1.0
</div>
</div>
答案 1 :(得分:0)
您不需要marginLeft
属性,可以使用left
属性,因为它是定位元素。这是您接受的输出,或者可能对您有帮助。
function isItANumber(el) {
var element1 = document.getElementById("circle").innerHTML;
var percent = parseFloat(element1)*100;
percent+='%';
var increased = parseInt(el.innerText);
var element = document.getElementById("Value");
if (isNaN(increased) || element1 >= 0 && element1 < 0.33 ) {
document.getElementById("Result").innerHTML = "KanBan";
document.getElementById("circle").style.left = percent;
};
if (isNaN(increased) || element1 >= 0.33 && element1 < 0.66) {
document.getElementById("Result").innerHTML = "ScrumBan";
document.getElementById("circle").style.left = percent;
};
if (isNaN(increased) || element1 >= 0.66 && element1 < 0.9) {
document.getElementById("Result").innerHTML = "Scrum";
document.getElementById("circle").style.left = percent;
}
if (isNaN(increased) || element1 > 0.9) {
document.getElementById("Result").innerHTML = "Scrum";
document.getElementById("circle").style.left = 'auto';
document.getElementById("circle").style.right = '0';
}
}
var element = document.getElementById("circle");
isItANumber(element);
#line {
box-sizing: border-box;
/* 2 */
height: 5px;
background: gray;
position:relative;
margin: 30px auto 0 auto;
}
#circle {
height: 50px;
width: 50px;
border-radius: 50%;
background: blue;
text-align: center;
color: white;
font-weight: 700;
line-height: 50px;
font-size: 14px;
position:absolute;
margin-top:-22.5px;
transition: 0.3s;
left: 0;
}
<div id="Result"></div>
<div id="line">
<div id="circle">
1.0
</div>
</div>
===谢谢===
答案 2 :(得分:0)
删除了添加了100% margin-left
并被margin-left:auto
替换的javascript,这会将圆圈放置在右侧,并使用margin-top: -22.5px
将其放置在行的顶部。谢谢
function isItANumber(el) {
var element1 = document.getElementById("circle").innerHTML;
var percent = parseFloat(element1) * 100;
percent += '%';
var increased = parseInt(el.innerText);
var element = document.getElementById("Value");
if (isNaN(increased) || element1 >= 0) {
document.getElementById("Result").innerHTML = "KanBan";
document.getElementById("circle").style.marginLeft = percent;
};
if (isNaN(increased) || element1 >= 0.33) {
document.getElementById("Result").innerHTML = "ScrumBan";
};
if (isNaN(increased) || element1 >= 0.66) {
document.getElementById("Result").innerHTML = "Scrum";
document.getElementById("circle").style.marginLeft = percent;
}
if (isNaN(increased) || element1 > 0.9) {
document.getElementById("Result").innerHTML = "Scrum";
document.getElementById("circle").style.marginRight = '0%';
}
}
var element = document.getElementById("circle");
isItANumber(element);
#line {
width: 100%;
/* 2 */
height: 5px;
background: gray;
position: relative;
margin-top: 30px;
padding-top: 1px;
}
#circle {
height: 50px;
width: 50px;
border-radius: 50%;
background: blue;
text-align: center;
color: white;
font-weight: 700;
line-height: 50px;
font-size: 14px;
/* position: absolute; */
margin-top: -22.5px;
/* margin: auto; */
/* margin-top: 100px; */
margin-left: auto !important;
}
<div id="Result"></div>
<div id="line">
<div id="circle">
1.0
</div>
</div>