gpt4 book ai didi

javascript - 当页面加载时,该元素被完全覆盖。应该是部分

转载 作者:太空宇宙 更新时间:2023-11-04 05:40:01 25 4
gpt4 key购买 nike

当页面加载时,该元素被完全覆盖。应该是部分的。如果我单击输入元素上的任何箭头,它会被部分覆盖(我需要的)。我该怎么做才能在页面加载时使其部分绘制?

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<style type="text/css">
.border {
fill: none;
stroke: green;
stroke-width: 1;
}
.sector {
fill: none;
stroke: green;
stroke-width: 50;
}
</style>
<script
type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"
></script>
<script src="file.js"></script>
</head>
<body>
<svg
width="100px"
height="100px"
viewBox="0 0 100 100"
preserveAspectRatio="xMidYMid meet"
>
<circle class="sector" r="25" cx="50" cy="50" />
<circle class="border" r="50" cx="50" cy="50" />
</svg>
<label>
<span
>YES %: <input type="number" min="0" max="100" value="78" id="yes"
/></span>
</label>
</body>
</html>

文件.js:

$(document).ready(function() {
let yesValue = 78;
document.getElementById("yes").innerHTML = yesValue;
function setSector(part) {
var radius = +$(".sector").attr("r"),
width = 2 * Math.PI * radius,
result = (width / 100) * part;
$(".sector").css("stroke-dasharray", result + "," + width);
}
$("[type=number]").on("input", function() {
var part = +$(this).val();
setSector(part);
});
});

总的来说,我需要部分绘制的圆圈和字符串:

YES % : 78

最佳答案

只需调用document.ready()时的函数

$(document).ready(function() {
let yesValue = 78;
setSector(yesValue);
document.getElementById("yes").innerHTML = yesValue;
function setSector(part) {
var radius = +$(".sector").attr("r"),
width = 2 * Math.PI * radius,
result = (width / 100) * part;
$(".sector").css("stroke-dasharray", result + "," + width);
}
$("[type=number]").on("input", function() {
var part = +$(this).val();
setSector(part);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<style type="text/css">
.border {
fill: none;
stroke: green;
stroke-width: 1;
}
.sector {
fill: none;
stroke: green;
stroke-width: 50;
}
</style>
<script
type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"
></script>
<script src="file.js"></script>
</head>
<body>
<svg
width="100px"
height="100px"
viewBox="0 0 100 100"
preserveAspectRatio="xMidYMid meet"
>
<circle class="sector" r="25" cx="50" cy="50" />
<circle class="border" r="50" cx="50" cy="50" />
</svg>
<label>
<span
>YES %: <input type="number" min="0" max="100" value="78" id="yes"
/></span>
</label>
</body>
</html>

关于javascript - 当页面加载时,该元素被完全覆盖。应该是部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59300833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com