jquery - 如何完美地居中一个根据用户输入而变化的圆
问题描述
我正在做一个周长/面积计算器。用户输入的是圆的半径,我正在设置这个值,以便在创建圆的可视化时它可以是宽度和高度,但是绘图没有居中并且它一直在改变位置。我怀疑这是因为圆圈的宽度与根据用户的变化不同,因此它不会保持居中。
输入
<input type="number" id="radius" placeholder="Enter the radius">
圆的div
<div class="circles">
<span class="circle"></span>
</div>
jQuery
const $radius = $("#radius").val();
const $diameter = ($radius * 2);
const $radioPick = $("input[name='calculation']:checked").val();
const $converted = ($radius * 37.795275591);
$('.circle').css({
height: $converted,
width: $converted
});
CSS
.circle {
border: 1px rgba(0, 0, 0, 0.2) solid;
background-color: rgba(192, 192, 192, 0.3);
border-radius: 50%;
position: fixed;
z-index: -1;
left: 50%;
top: 50%;
}
$(document).ready(function() {
$('.circle').hide();
$('#radius').on('keyup', () => {
const $radius = $("#radius").val();
const $diameter = ($radius * 2);
const $radioPick = $("input[name='calculation']:checked").val();
const $converted = ($radius * 37.795275591);
if ($radius < 0) {
$('#error').text('Please input positive integers');
return false;
} else if ($radioPick === "area") {
const $acalc = ($radius * $radius * Math.PI).toPrecision(6);
$('#feedback').text('Area = ' + $acalc + ' cm\u00B2');
$('#diameter').text('Diameter = ' + $diameter + ' cm\u00B2');
$('.circle').css({
height: $converted,
width: $converted
});
$('.circle').fadeIn(300);
$('#error').hide();
} else if ($radioPick === "circuference") {
const $circalc = (2 * Math.PI * $radius).toPrecision(6);
$('#feedback').text('Circunference = ' + $circalc);
$('#diameter').text('Diameter = ' + $diameter);
$('.circle').css({
height: (2 * $converted),
width: (2 * $converted)
});
$('.circle').fadeIn(300);
$('#error').hide();
}
})
});
body {
margin: 0 auto;
font-family: Helvetica, sans-serif;
}
.wrapper {
height: 400px;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
.title h1 {
font-weight: lighter;
font-size: 50px;
/* top: 5%; */
/* left: 35%; */
}
#radius {
padding: 5px;
margin: 10px;
}
#bttn {
border: none;
font-size: 15px;
padding: 5px;
border-radius: 3px;
background-color: lightgrey;
border: 0.5px black solid;
}
.calculations {}
input[type="radio"] {
cursor: pointer;
}
input[type="radio"],
label {}
.answer {
margin: 50px;
width: 200px;
height: 100px;
}
.answer p {
font-weight: bold;
font-size: 18px;
}
.circle {
border: 1px rgba(0, 0, 0, 0.2) solid;
background-color: rgba(192, 192, 192, 0.3);
border-radius: 50%;
position: fixed;
z-index: -1;
left: 50%;
top: 50%;
}
#error {
color: red;
}
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous">
</script>
<div class="wrapper">
<div class="title">
<h1>Circles Calculator</h1>
</div>
<div class="calculations">
<input type="radio" name="calculation" value="area"><label> Area</label><br />
<input type="radio" name="calculation" value="circuference"><label> Circuference</label><br />
<input type="number" id="radius" placeholder="Enter the radius">
<input type="button" id="bttn" name="calculate" value="Calculate">
</div>
<div class="answer">
<p id="feedback"></p>
<p id="diameter"></p>
<p id="error"></p>
</div>
<div class="circles">
<span class="circle"></span>
</div>
</div>
我想要做的是:制作一个在屏幕上完全居中的圆圈/根据输入更改大小但不移动
解决方案
诀窍是一点 CSS Magic。您需要像使用 and 一样动态地调整您的andtop
为带有classleft
的元素。我们利用 CSS3属性。在计算面积时考虑:circle
width
height
calc()
$('.circle').css({
height: $converted,
width: $converted,
top: "calc(50% - " + ($converted/2) + "px)",
left: "calc(50% - " + ($converted/2) + "px)"
});
或周长:
$('.circle').css({
height: $converted,
width: $converted,
top: "calc(50% - " + $converted + "px)",
left: "calc(50% - " + $converted + "px)"
});
现在top
andleft
将被计算为 50% - 圆的半径,保持它的中心在页面的中心。
工作示例:https ://jsfiddle.net/Twisty/cgLfz1ro/
您还可以考虑一些其他小的更改。就像检查以确保用户选择了区域或周长,如果他们没有选择则生成错误。
推荐阅读
- optimization - 运行大量密码的有效方法是什么?
- asp.net-core - EF Core 显式忽略关系
- go - net.Listen 和 http.ListenAndServe 功能的区别
- python - 由于我想在句子中添加整数,如何获得没有括号或括号的输出?
- c++ - 什么是 C++ 中的类型别名?
- grafana - 如何通过 prometheus promQL 中的某个列连接两个指标(用于 grafana 仪表板)
- asp.net-mvc - Razor MVC 5 中“class”属性的字符串连接
- docker - 经常提交 docker 容器是一种好习惯吗?
- out-of-memory - pig 脚本因 GC 失败:超出开销限制
- javascript - 对我的 API 的 Ajax 调用返回未定义