You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# lllzzhhh
<!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="styles.css">
</head>
<body>
<div class="container">
<h1>星座和属相查询</h1>
<input type="date" id="birthDate" placeholder="输入出生日期">
<button onclick="queryHoroscopeAndZodiac()">查询</button>
<div id="result"></div>
</div>
<script src="script.js"></script>
</body>
</html>
function queryHoroscopeAndZodiac() {
const birthDate = document.getElementById('birthDate').value;
if (!birthDate) {
alert('请输入出生日期!');
return;
}
const birthDateObj = new Date(birthDate);
const dayOfYear = (function(date) {
let start = new Date(date.getFullYear(), 0, 0);
let diff = date - start;
let oneDay = 1000 * 60 * 60 * 24;
return Math.floor(diff / oneDay);
})(birthDateObj);
const chineseZodiac = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊','猴', '鸡', '狗', '猪'];
const year = birthDateObj.getFullYear();
const zodiacIndex = (year - 4) % 12; // 1900年是庚子年鼠年从0开始计数
const zodiac = chineseZodiac[zodiacIndex];
const horoscopeMap = [
{ sign: '摩羯座', start: 0, end: 19 },
{ sign: '水瓶座', start: 20, end: 49 },
{ sign: '双鱼座', start: 50, end: 79 },
{ sign: '白羊座', start: 80, end: 109 },
{ sign: '金牛座', start: 110, end: 140 },
{ sign: '双子座', start: 141, end: 171 },
{ sign: '巨蟹座', start: 172, end: 203 },
{ sign: '狮子座', start: 204, end: 234 },
{ sign: '处女座', start: 235, end: 265 },
{ sign: '天秤座', start: 266, end: 295 },
{ sign: '天蝎座', start: 296, end: 325 },
{ sign: '射手座', start: 326, end: 355 }, // 射手座跨越了年份,需要特别处理
{ sign: '摩羯座', start: 355, end: 365 } // 处理12月22日之前的射手座上一年
];
let horoscope = '未知';
for (let i = 0; i < horoscopeMap.length; i++) {
if ((dayOfYear >= horoscopeMap[i].start && dayOfYear <= horoscopeMap[i].end) ||
(i === horoscopeMap.length - 1 && dayOfYear <= horoscopeMap[i].end && dayOfYear >= 0)) { // 处理跨年情况
horoscope = horoscopeMap[i].sign;
break;
}
}
document.getElementById('result').innerHTML = `
<p>星座: ${horoscope}</p >
<p>属相: ${zodiac}</p >
`;
}
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
}
h1 {
margin-bottom: 20px;
}
input, button {
padding: 10px;
font-size: 16px;
margin: 5px;
}
#result {
margin-top: 20px;
font-size: 18px;
}