diff --git a/README.md b/README.md index 459188b..5faa561 100644 --- a/README.md +++ b/README.md @@ -17,3 +17,85 @@ +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 = ` +

星座: ${horoscope}

+

属相: ${zodiac}

+ `; +} +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; +} \ No newline at end of file