作为程序员,没有合适的工具,就得手搓一个,PC端,移动端均可适用。废话不多说,直接上代码。
HTML:
<div class="calculator"><label for="inputValue">输入角度值:</label> <input id="inputValue" type="number" placeholder="请输入数值"> <label for="inputUnit">选择输入单位:</label><select id="inputUnit"><option value="degrees">度 (°)</option><option value="radians">弧度 (rad)</option><option value="gradians">梯度 (gons)</option></select><label for="outputUnit">选择输出单位:</label><select id="outputUnit"><option value="degrees">度 (°)</option><option value="radians">弧度 (rad)</option><option value="gradians">梯度 (gons)</option></select><button onclick="convertAngle()">计算</button><div class="result"><p>转换结果:</p><p id="outputValue">0</p></div></div>
JS:
function convertAngle() {
const inputValue = parseFloat(document.getElementById('inputValue').value);
const inputUnit = document.getElementById('inputUnit').value;
const outputUnit = document.getElementById('outputUnit').value;
if (isNaN(inputValue)) {
alert('请输入有效的角度值');
return;
}
let valueInDegrees;
// Convert input value to degrees
switch (inputUnit) {
case 'degrees':
valueInDegrees = inputValue;
break;
case 'radians':
valueInDegrees = inputValue * (180 / Math.PI);
break;
case 'gradians':
valueInDegrees = inputValue * (9 / 10);
break;
}
// Convert degrees to output unit
let convertedValue;
switch (outputUnit) {
case 'degrees':
convertedValue = valueInDegrees;
break;
case 'radians':
convertedValue = valueInDegrees * (Math.PI / 180);
break;
case 'gradians':
convertedValue = valueInDegrees * (10 / 9);
break;
}
document.getElementById('outputValue').textContent = convertedValue.toFixed(2);
}
CSS:
.calculator {
width: 300px;
background-color: #333;
color: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
}
label {
display: block;
margin-bottom: 10px;
font-size: 16px;
}
input, select {
width: 100%!important;
padding: 10px!important;
margin-bottom: 20px;
color: #000000;
border-radius: 5px;
border: 1px solid #555;
font-size: 16px!important;
background-color: #ffffff!important;
}
button {
width: 100%;
padding: 10px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: orange;
}
.result {
margin-top: 20px;
text-align: center;
}
option {
background-color: #ffffff;
}
p {
font-size: 18px;
margin-top: 5px!important;
}
标签:case,color,计算器,break,角度,换算,inputValue,background,valueInDegrees
From: https://blog.csdn.net/yuchangchenTT/article/details/143678775