
Day-Night Toggle body, html { margin: 0; padding: 0; height: 100vh; display: flex; justify-content: center; align-items: center; font-family: Arial, sans-serif; transition: background 0.8s ease, filter 0.8s ease; overflow: hidden; } .container { position: absolute; bottom: 30px; right: 30px; text-align: center; } .toggle-button { width: 100px; height: 50px; background: #333; border-radius: 25px; position: relative; cursor: pointer; transition: background 0.3s ease; box-shadow: 0 4px 15px rgba(0, 0, 0, 0.5); display: flex; align-items: center; justify-content: space-between; padding: 5px; } .toggle-circle { width: 40px; height: 40px; background: #fff; border-radius: 50%; position: absolute; transition: all 0.5s ease; box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3); } /* Icons for Sun and Moon on Button */ .button-sun, .button-moon { font-size: 2rem; } .button-sun { color: #FFD700; } .button-moon { color: #B0C4DE; } /* Large Sun and Moon at Top Left */ .large-sun, .large-moon { font-size: 10rem; position: absolute; top: 20px; right: 10px; transition: opacity 0.8s ease, transform 0.8s ease; } .large-sun { color: #FFD700; opacity: 0; } .large-moon { color: #B0C4DE; opacity: 0; } /* Day and Night styles */ .day .large-sun { opacity: 1; transform: translateY(0); } .night .large-moon { opacity: 1; transform: translateY(0); } .day .image { filter: brightness(1.5) contrast(1.2) saturate(2); } .night .image { filter: brightness(0.7) contrast(1) saturate(1); } .image { position: absolute; height: 100%; width: 100%; } .moon-image { height: 200px; width: 200px; filter: drop-shadow(1px 1px 20px white); } .sun-image { height: 250px; width: 300px; filter: drop-shadow(2px 2px 40px rgb(206, 145, 23)); } /* New styles for text */ .mode-text { position: absolute; top: 40%; left: 40%; transform: translate(-50%, -50%); font-size: 2rem; color: #fff; text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.6); transition: opacity 0.5s ease; } .day .mode-text { opacity: 1; } .night .mode-text { opacity: 0; } .night .mode-text-night { opacity: 1; } @@##@@@@##@@@@##@@Good Morning! It's a beautiful day!Good Night! Sweet dreams!let isDay = true; function toggleDayNight() { const body = document.body; const toggleCircle = document.querySelector('.toggle-circle'); const largeSun = document.querySelector('.large-sun'); const largeMoon = document.querySelector('.large-moon'); const dayText = document.querySelector('.day-text'); const nightText = document.querySelector('.night-text'); if (isDay) { // Switch to night mode body.classList.remove('day'); body.classList.add('night'); toggleCircle.style.transform = 'translateX(60px)'; dayText.style.opacity = '0'; nightText.style.opacity = '1'; } else { // Switch to day mode body.classList.remove('night'); body.classList.add('day'); toggleCircle.style.transform = 'translateX(0)'; dayText.style.opacity = '1'; nightText.style.opacity = '0'; } isDay = !isDay; }



以上就是使用 html css 和 javascript 制作太阳和月亮动画的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1494653.html
微信扫一扫
支付宝扫一扫