[HTML] 纯文本查看 复制代码 <!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>利息计算器</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f4f4f9;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.calculator-card {
background-color: #fff;
padding: 2rem;
border-radius: 10px;
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
width: 100%;
max-width: 400px;
}
h2 {
text-align: center;
color: #333;
margin-bottom: 1.5rem;
}
.input-group {
margin-bottom: 1rem;
}
label {
display: block;
margin-bottom: 0.5rem;
color: #666;
font-size: 0.9rem;
}
input[type="number"] {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
box-sizing: border-box;
font-size: 1rem;
}
input:focus {
border-color: #007BFF;
outline: none;
}
.result-area {
margin-top: 1.5rem;
padding: 1rem;
background-color: #e9ecef;
border-radius: 5px;
text-align: center;
}
.result-label {
color: #555;
font-size: 0.9rem;
}
.result-value {
color: #007BFF;
font-size: 1.8rem;
font-weight: bold;
margin-top: 5px;
}
.formula-hint {
font-size: 0.8rem;
color: #999;
text-align: center;
margin-top: 1rem;
}
.tab-container {
display: flex;
margin-bottom: 1rem;
}
.tab {
flex: 1;
text-align: center;
padding: 0.5rem;
background-color: #eee;
cursor: pointer;
border-radius: 5px 5px 0 0;
}
.tab.active {
background-color: #007BFF;
color: white;
}
.tab-content {
display: none;
}
.tab-content.active {
display: block;
}
.seven-day-inputs {
display: flex;
gap: 8px;
margin-top: 5px;
}
.day-input {
flex: 1;
min-width: 0;
padding: 8px;
text-align: center;
border: 1px solid #ddd;
border-radius: 4px;
}
input::selection {
background-color: #007BFF;
color: white;
}
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
-webkit-appearance: none;
margin: 0;
}
input[type="number"] {
-moz-appearance: textfield;
}
</style>
</head>
<body>
<div class="calculator-card">
<h2>利息计算器</h2>
<div class="tab-container">
<div class="tab active" onclick="switchTab('standard')">标准利息</div>
<div class="tab" onclick="switchTab('sevenDay')">七日年化</div>
</div>
<!-- 标准利息计算部分 -->
<div id="standard" class="tab-content active">
<div class="input-group">
<label for="principal">本金 (元)</label>
<input type="number" id="principal" placeholder="例如:10000" oninput="calculateStandard()">
</div>
<div class="input-group">
<label for="rate">年利率 (%)</label>
<input type="number" id="rate" placeholder="例如:2.8" step="0.01" oninput="calculateStandard()">
</div>
<div class="input-group">
<label for="days">天数</label>
<input type="number" id="days" value="30" placeholder="默认365" oninput="calculateStandard()">
</div>
<div class="result-area">
<div class="result-label">预计利息 (保留两位小数)</div>
<div class="result-value" id="result-standard">0.00</div>
</div>
<div class="formula-hint">
公式:本金 × 年利率% × 天数 ÷ 365
</div>
</div>
<!-- 七日年化计算部分 -->
<div id="sevenDay" class="tab-content">
<div class="input-group">
<label for="sevenDayPrincipal">本金 (元)</label>
<input type="number" id="sevenDayPrincipal" placeholder="例如:10000"
onkeydown="if(event.key==='Enter')document.querySelector('.day-input').focus()">
</div>
<div class="input-group">
<label>每日收益(元)</label>
<div class="seven-day-inputs">
<input type="number" class="day-input" placeholder="1st" data-day="1" onkeydown="handleDayInput(event, 1)">
<input type="number" class="day-input" placeholder="2nd" data-day="2" onkeydown="handleDayInput(event, 2)">
<input type="number" class="day-input" placeholder="3rd" data-day="3" onkeydown="handleDayInput(event, 3)">
<input type="number" class="day-input" placeholder="4th" data-day="4" onkeydown="handleDayInput(event, 4)">
<input type="number" class="day-input" placeholder="5th" data-day="5" onkeydown="handleDayInput(event, 5)">
<input type="number" class="day-input" placeholder="6th" data-day="6" onkeydown="handleDayInput(event, 6)">
<input type="number" class="day-input" placeholder="7th" data-day="7" onkeydown="handleDayInput(event, 7)">
</div>
</div>
<div class="result-area">
<div class="result-label">七日总收益</div>
<div class="result-value" id="total-sevenDay-earnings">0.00 元</div>
<div class="result-label" style="margin-top: 10px;">七日年化收益率</div>
<div class="result-value" id="result-sevenDay">0.00%</div>
</div>
<div class="formula-hint">
公式:(七日总收益 ÷ 本金 ÷ 7) × 365 × 100%
</div>
</div>
</div>
<script>
function handleFocus(event) {
event.target.select();
}
function switchTab(tabId) {
document.querySelectorAll('.tab').forEach(tab => tab.classList.remove('active'));
document.querySelectorAll('.tab-content').forEach(content => content.classList.remove('active'));
document.querySelector(`.tab[onclick="switchTab('${tabId}')"]`).classList.add('active');
document.getElementById(tabId).classList.add('active');
setTimeout(() => {
const firstInput = document.querySelector(`#${tabId} input`);
if (firstInput) firstInput.focus();
}, 50);
}
function setupStandardInterestFocus() {
const inputs = document.querySelectorAll('#standard input');
inputs.forEach((input, index) => {
input.addEventListener('keydown', function(e) {
if (e.key === 'Enter') {
e.preventDefault();
const nextIndex = (index + 1) % inputs.length;
if (nextIndex === 0) {
calculateStandard();
setTimeout(() => inputs[0].focus(), 300);
} else {
inputs[nextIndex].focus();
}
}
});
});
}
function setupSevenDayFocus() {
const principalInput = document.getElementById('sevenDayPrincipal');
const dayInputs = document.querySelectorAll('.day-input');
principalInput.addEventListener('keydown', function(e) {
if (e.key === 'Enter') {
e.preventDefault();
dayInputs[0].focus();
}
});
dayInputs.forEach((input, index) => {
input.addEventListener('keydown', function(e) {
if (e.key === 'Enter') {
e.preventDefault();
const isLastInput = (index === dayInputs.length - 1);
if (isLastInput) {
calculateSevenDay();
setTimeout(() => principalInput.focus(), 300);
} else {
dayInputs[index + 1].focus();
}
}
});
});
}
document.addEventListener('DOMContentLoaded', function() {
document.querySelectorAll('input').forEach(input => {
input.addEventListener('focus', handleFocus);
input.addEventListener('click', handleFocus);
});
setupStandardInterestFocus();
setupSevenDayFocus();
});
function calculateStandard() {
const principal = parseFloat(document.getElementById('principal').value);
const rate = parseFloat(document.getElementById('rate').value);
const days = parseFloat(document.getElementById('days').value);
if (isNaN(principal) || isNaN(rate) || isNaN(days)) {
document.getElementById('result-standard').innerText = "0.00";
return;
}
const interest = (principal * (rate / 100) * days) / 365;
document.getElementById('result-standard').innerText = interest.toFixed(2);
}
function calculateSevenDay() {
const principal = parseFloat(document.getElementById('sevenDayPrincipal').value);
let totalEarnings = 0;
let allDaysFilled = true;
for (let day = 1; day <= 7; day++) {
const dayEarning = parseFloat(document.querySelector(`.day-input[data-day="${day}"]`).value);
if (!isNaN(dayEarning)) {
totalEarnings += dayEarning;
} else {
allDaysFilled = false;
}
}
document.getElementById('total-sevenDay-earnings').innerText = totalEarnings.toFixed(2) + ' 元';
if (allDaysFilled && !isNaN(principal) && principal > 0) {
const sevenDayYield = ((totalEarnings / principal / 7) * 365 * 100).toFixed(2);
document.getElementById('result-sevenDay').innerText = `${sevenDayYield}%`;
} else {
document.getElementById('result-sevenDay').innerText = "0.00%";
}
}
</script>
</body>
</html> |