作为程序员,没有合适的工具,就得手搓一个,PC端,移动端均可适用。废话不多说,直接上代码。
HTML:
<div class="calculator"><div class="input-group"><label for="height">身高(厘米):</label> <input id="height" min="100" type="number" placeholder="输入身高(厘米)"></div><div class="input-group"><label for="inseam">跨步高度(厘米):</label> <input id="inseam" min="50" type="number" placeholder="输入跨步高度(厘米)"></div><button onclick="calculateBikeSize()">计算自行车尺寸</button><div id="result" class="result"></div></div>
JS:
function calculateBikeSize() {
const height = parseFloat(document.getElementById("height").value);
const inseam = parseFloat(document.getElementById("inseam").value);
const resultDiv = document.getElementById("result");
// 验证输入是否有效
if (isNaN(height) || isNaN(inseam) || height <= 0 || inseam <= 0) {
resultDiv.textContent = "请输入有效的身高和跨步高度!";
return;
}
// 计算车架尺寸
const roadBikeSize = (height * 0.665).toFixed(1); // 公路车尺寸
const mountainBikeSize = (height * 0.56).toFixed(1); // 山地车尺寸
// 显示推荐尺寸
resultDiv.innerHTML = `
<div>推荐的公路车车架尺寸:${roadBikeSize} cm</div>
<div>推荐的山地车车架尺寸:${mountainBikeSize} cm</div>
`;
}
CSS:
.calculator {
width: 100%;
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 {
font-size: 18px;
margin-top: 20px;
text-align: center;
}
option {
background-color: #ffffff;
}
p {
font-size: 18px;
margin-top: 5px!important;
}