给网站添加一个好看的跨年倒计时功能,与用户一起跨年,下面是截图预览
废话不多说,直接上代码:
<div class="countdown-tips-wrapper">
<div class="newyear2026-tips-bar" id="newyearTipsBar">
<i class="fas fa-glass-cheers newyear-icon"></i>
<span class="newyear-text">🎊 2026跨年</span>
<div class="countdown-box" id="countdownBox"></div>
<a href="https://www.weidus.com/2026" target="_blank" class="newyear-link">立即前往 →</a>
<div class="newyear-close" id="newyearTipsClose">
<i class="fas fa-times"></i>
</div>
</div>
</div>
JS代码:
<script>
// 1. 关闭公告条功能
const newyearTipsBar = document.getElementById('newyearTipsBar');
const newyearTipsClose = document.getElementById('newyearTipsClose');
newyearTipsClose.addEventListener('click', function() {
newyearTipsBar.style.opacity = '0';
newyearTipsBar.style.height = '0';
newyearTipsBar.style.padding = '0';
newyearTipsBar.style.border = '0';
setTimeout(() => {
newyearTipsBar.style.display = 'none';
}, 300);
});
// 2. 实时跨年倒计时功能
const countdownBox = document.getElementById('countdownBox');
const targetTime = new Date('2026-01-01 00:00:00').getTime(); // 2026新年零点
// 补零函数
function addZero(num) {
return num < 10 ? '0' + num : num;
}
// 更新倒计时
function updateCountdown() {
const currentTime = new Date().getTime();
const timeDiff = targetTime - currentTime;
let days = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
let hours = Math.floor((timeDiff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
let minutes = Math.floor((timeDiff % (1000 * 60 * 60)) / (1000 * 60));
let seconds = Math.floor((timeDiff % (1000 * 60)) / 1000);
// 补零处理
hours = addZero(hours);
minutes = addZero(minutes);
seconds = addZero(seconds);
if (timeDiff > 0) {
countdownBox.innerHTML = `倒计时:<span class="countdown-num">${days}</span>天
<span class="countdown-num">${hours}</span>时
<span class="countdown-num">${minutes}</span>分
<span class="countdown-num">${seconds}</span>秒`;
} else {
countdownBox.innerHTML = '已开启!';
clearInterval(countdownTimer);
}
}
// 初始化+每秒更新
updateCountdown();
const countdownTimer = setInterval(updateCountdown, 1000);
</script>
CSS代码:
.countdown-tips-wrapper {
width: 100%;
display: flex;
justify-content: center;
align-items: flex-start;
}
.newyear2026-tips-bar {
height: 36px;
width: 100%;
max-width: 580px;
background: linear-gradient(135deg, #ff7c94, #fbab5e, #fee256);
display: flex;
align-items: center;
justify-content: center;
padding: 0 12px;
box-shadow: 0 3px 15px rgba(255, 56, 92, 0.3);
position: relative;
overflow: hidden;
z-index: 999;
border-radius: 18px;
flex-wrap: nowrap;
}
.newyear2026-tips-bar::before {
content: "";
position: absolute;
top: 0;
left: -100%;
width: 50%;
height: 100%;
background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.1), transparent);
animation: lightSlide 3s infinite linear;
}
@keyframes lightSlide {
0% { left: -100%; }
100% { left: 150%; }
}
.newyear-icon {
color: #fff;
font-size: 14px;
margin-right: 6px;
animation: pulse 1.2s infinite ease-in-out;
text-shadow: 0 0 5px rgba(255, 255, 255, 0.5);
flex-shrink: 0;
}
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.08); }
100% { transform: scale(1); }
}
.newyear-text {
font-size: 13px;
color: #fff;
font-weight: 500;
margin-right: 6px;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
letter-spacing: 0.3px;
flex-shrink: 0;
}
.countdown-box {
font-size: 13px;
color: #fff;
font-weight: 600;
margin-right: 6px;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
flex-shrink: 1;
white-space: nowrap;
}
.countdown-num {
color: #fff;
background-color: rgba(0, 0, 0, 0.15);
padding: 1px 3px;
border-radius: 3px;
margin: 0 1px; /* 缩小数字间距 */
}
.newyear-link {
font-size: 11px;
color: #fff;
text-decoration: none;
font-weight: 600;
padding: 2px 5px;
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 8px;
transition: all 0.3s ease;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
flex-shrink: 0; /* 禁止链接被压缩 */
white-space: nowrap; /* 禁止链接文字换行 */
}
.newyear-link:hover {
background-color: rgba(255, 255, 255, 0.25);
border-color: #fff;
transform: translateY(-1px);
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
.newyear-close {
position: absolute;
right: 12px;
top: 50%;
transform: translateY(-50%);
width: 16px; /* 移动端缩小按钮尺寸 */
height: 16px;
display: flex;
align-items: center;
justify-content: center;
color: rgba(255, 255, 255, 0.85);
cursor: pointer;
border-radius: 50%;
transition: all 0.3s ease;
flex-shrink: 0; /* 禁止关闭按钮被压缩 */
}
.newyear-close:hover {
background-color: rgba(255, 255, 255, 0.25);
color: #fff;
transform: translateY(-50%) scale(1.1);
}
@media screen and (max-width: 375px) {
.newyear2026-tips-bar {
height: 34px;
padding: 0 10px;
}
.newyear-text, .countdown-box, .newyear-link {
font-size: 10px; /* 进一步缩小字体 */
}
.newyear-icon {
font-size: 13px;
}
.newyear-close {
width: 15px;
height: 15px;
right: 10px;
}
}
@media screen and (max-width: 575px) {
.countdown-box {
font-size: 11px;
}
.newyear-text {
font-size: 12px;
}
}
#newyearTipsClose{
display: none;
}
需要引入一条css:
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css">
完成以上代码即可实现跨年倒计时,虽然已经来到了2026年,但是春节还没来,可以在上述js代码中将倒计时日期改为春节的日期,如下
// 2. 实时跨年倒计时功能
const countdownBox = document.getElementById('countdownBox');
const targetTime = new Date('2026-02-17 00:00:00').getTime(); // 2026新年零点
日期改为2月17日即可!
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
暂无评论...
