<!
DOCTYPE
html>
<
html
lang
=
"zh-CN"
>
<
head
>
<
meta
charset
=
"UTF-8"
>
<
meta
name
=
"viewport"
content
=
"width=device-width, initial-scale=1.0"
>
<
title
>保质期计算器</
title
>
<
link
rel
=
"stylesheet"
href
=
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css"
>
<
style
>
:root {
--primary: #2dd4bf;
--secondary: #64748b;
--background: #f1f5f9;
--white: #ffffff;
--success: #4ade80;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
/* 修改 max-width,让页面在小屏幕上更窄 */
max-width: 35%;
margin: 6rem auto;
padding: 10px;
background-color: var(--background);
color: var(--secondary);
}
.calculator {
background-color: var(--white);
padding: 1.5rem;
border-radius: 1.5rem;
box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.1);
animation: fadeIn 0.5s ease-out;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(-20px); }
to { opacity: 1; transform: translateY(0); }
}
h2 {
color: var(--primary);
text-align: center;
margin-bottom: 2rem;
font-size: 2.25rem;
font-weight: 600;
}
.form-group {
margin-bottom: 1.5rem;
}
label {
display: block;
margin-bottom: 0.75rem;
font-weight: 500;
color: var(--secondary);
}
.date-picker-container {
position: relative;
cursor: pointer;
border: 2px solid #e2e8f0;
border-radius: 0.75rem;
padding: 1rem 1.5rem;
transition: all 0.3s ease;
display: flex;
align-items: center;
}
.date-picker-container:hover {
border-color: var(--primary);
background: rgba(45, 212, 191, 0.05);
}
#production-date {
position: absolute;
opacity: 0;
width: 100%;
height: 100%;
left: 0;
top: 0;
cursor: pointer;
}
.date-display {
flex: 1;
font-size: 1rem;
color: var(--secondary);
}
input, select {
width: 100%;
padding: 1rem;
border: 2px solid #e2e8f0;
border-radius: 0.75rem;
box-sizing: border-box;
font-size: 1rem;
transition: border-color 0.3s ease;
}
input:focus, select:focus {
outline: none;
border-color: var(--primary);
}
button {
background-color: var(--primary);
color: var(--white);
padding: 1rem 2rem;
border: none;
border-radius: 0.75rem;
cursor: pointer;
width: 100%;
font-size: 1rem;
font-weight: 500;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #22c55e;
transform: translateY(-2px);
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -2px rgba(0, 0, 0, 0.1);
}
.result {
margin-top: 2rem;
padding: 1.5rem;
background-color: #f0fdf4;
border-radius: 0.75rem;
border-left: 4px solid var(--success);
display: none;
animation: slideIn 0.3s ease-out;
}
@keyframes slideIn {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
</
style
>
</
head
>
<
body
>
<
div
class
=
"calculator"
>
<
h2
>保质期计算器</
h2
>
<
div
class
=
"form-group"
>
<
label
for
=
"production-date"
><
i
class
=
"fas fa-calendar-day"
></
i
> 生产日期</
label
>
<
div
class
=
"date-picker-container"
>
<
span
class
=
"date-display"
id
=
"date-display"
>点击选择日期</
span
>
<
input
type
=
"date"
id
=
"production-date"
required
>
</
div
>
</
div
>
<
div
class
=
"form-group"
>
<
label
for
=
"shelf-life"
>保质期时长:</
label
>
<
div
style
=
"display: flex; gap: 1rem;"
>
<
input
type
=
"number"
id
=
"shelf-life"
min
=
"1"
value
=
"1"
required>
<
select
id
=
"time-unit"
>
<
option
value
=
"days"
>天</
option
>
<
option
value
=
"weeks"
>周</
option
>
<
option
value
=
"months"
>月</
option
>
<
option
value
=
"years"
>年</
option
>
</
select
>
</
div
>
</
div
>
<
button
>计算过期日期</
button
>
<
div
class
=
"result"
id
=
"result"
>
<
p
>过期日期:<
span
id
=
"expiry-date"
></
span
></
p
>
<
p
>剩余天数:<
span
id
=
"days-remaining"
></
span
> 天</
p
>
</
div
>
</
div
>
<
script
>
function calculateExpiry() {
const productionDate = new Date(document.getElementById('production-date').value);
const shelfLife = parseInt(document.getElementById('shelf-life').value);
const timeUnit = document.getElementById('time-unit').value;
if (!document.getElementById('production-date').value || isNaN(shelfLife)) {
alert("请填写所有必填字段");
return;
}
const expiryDate = new Date(productionDate);
switch(timeUnit) {
case 'days':
expiryDate.setDate(expiryDate.getDate() + shelfLife);
break;
case 'weeks':
expiryDate.setDate(expiryDate.getDate() + (shelfLife * 7));
break;
case 'months':
expiryDate.setMonth(expiryDate.getMonth() + shelfLife);
break;
case 'years':
expiryDate.setFullYear(expiryDate.getFullYear() + shelfLife);
break;
}
document.getElementById('expiry-date').textContent = expiryDate.toLocaleDateString();
const today = new Date();
const timeDiff = expiryDate - today;
const daysRemaining = Math.ceil(timeDiff / (1000 * 3600 * 24));
// 修改显示逻辑
if (daysRemaining <
0
) {
document.getElementById('days-remaining').innerHTML = `<strong>已过期</
strong
> ${Math.abs(daysRemaining)} `;
} else {
document.getElementById('days-remaining').textContent = daysRemaining;
}
document.getElementById('result').style.display = 'block';
}
document.getElementById('production-date').addEventListener('change', function() {
const displayElement = document.getElementById('date-display');
displayElement.textContent = this.value
? new Date(this.value).toLocaleDateString('zh-CN', {
year: 'numeric',
month: 'long',
day: 'numeric'
})
: "点击选择日期";
});
</
script
>
</
body
>
</
html
>