LLWiki正在建设中,欢迎加入我们!
“Widget:Countdown”的版本间差异
跳转到导航
跳转到搜索
小 标签:移动版网页编辑 移动版编辑 |
小 |
||
第10行: | 第10行: | ||
before = ele.children[0], |
before = ele.children[0], |
||
after = ele.children[1], |
after = ele.children[1], |
||
⚫ | |||
isBefore = then.isBefore(now), |
isBefore = then.isBefore(now), |
||
monthsHave31Days = [0, 2, 4, 6, 7, 9, 11], // 月份从0开始 |
monthsHave31Days = [0, 2, 4, 6, 7, 9, 11], // 月份从0开始 |
||
第33行: | 第34行: | ||
if (day < 0) { |
if (day < 0) { |
||
month--; |
month--; |
||
if (monthsHave31Days.includes((isBefore ? then : now).month())) { |
if (monthsHave31Days.includes((isBefore ? then : now).month())) { day += 31; } |
||
else if ((isBefore ? then : now).month() === 1) { |
|||
if ((isBefore ? then : now).year() % 4 === 0) { day += 29; } |
|||
else { day += 28; } |
|||
⚫ | |||
⚫ | |||
day += 28; |
|||
} |
|||
} else { |
|||
day += 30; |
|||
} |
} |
||
⚫ | |||
} |
} |
||
if (month < 0) { |
if (month < 0) { |
||
第49行: | 第45行: | ||
month += 12; |
month += 12; |
||
} |
} |
||
if (year > 0) { |
if (year > 0) { result += `<span class="countdown-num">${year}</` + 'span>年'; } |
||
result += `<span class="countdown-num">${ |
if (month > 0) { result += `<span class="countdown-num">${month}</` + 'span>' + i18n.substring(2); } |
||
⚫ | |||
} |
|||
if ( |
if (day > 0) { result += `<span class="countdown-num">${day}</` + 'span>天'; } |
||
result += `<span class="countdown-num">${ |
else if (result !== "") { result += `<span class="countdown-num">${0}</` + 'span>天'; } |
||
} else if (result !== "") { |
|||
result += `<span class="countdown-num">${0}</` + 'span>' + ele.dataset.month; |
|||
} |
|||
if (day > 0) { |
|||
result += `<span class="countdown-num">${day}</` + 'span>天'; |
|||
} else if (result !== "") { |
|||
result += `<span class="countdown-num">${0}</` + 'span>天'; |
|||
} |
|||
if(year == 0 && month == 0) { |
if(year == 0 && month == 0) { |
||
if (hour > 0) { |
if (hour > 0) { result += `<span class="countdown-num">${hour}</` + 'span>' + i18n.substring(0, 2); } |
||
result += `<span class="countdown-num">${ |
else if (result !== "") { result += `<span class="countdown-num">${0}</` + 'span>' + i18n.substring(0, 2); } |
||
} else if (result !== "") { |
|||
⚫ | |||
} |
|||
if(day == 0) { |
if(day == 0) { |
||
if (minute > 0) { |
if (minute > 0) { result += `<span class="countdown-num">${minute}</` + 'span>分'; } |
||
result += `<span class="countdown-num">${ |
else if (result !== "") { result += `<span class="countdown-num">${0}</` + 'span>分'; } |
||
if (second > 0) { result += `<span class="countdown-num">${second}</` + 'span>秒'; } |
|||
result += `<span class="countdown-num">${0}</` + 'span> |
else if (result !== "") { result += `<span class="countdown-num">${0}</` + 'span>秒'; } |
||
} |
|||
if (second > 0) { |
|||
result += `<span class="countdown-num">${second}</` + 'span>秒'; |
|||
} else if (result !== "") { |
|||
result += `<span class="countdown-num">${0}</` + 'span>秒'; |
|||
} |
|||
} |
} |
||
} |
} |
||
第118行: | 第97行: | ||
<includeonly> |
<includeonly> |
||
</script> |
</script> |
||
<style> |
|||
⚫ | |||
/* </includeonly>CSS样式表:<pre lang="css"> */ |
|||
.countdownNode { |
|||
visibility:hidden; |
|||
} |
|||
/* </pre><includeonly> */ |
|||
⚫ |
2020年9月30日 (三) 22:27的版本
JavaScript脚本:
"use strict";
window.addEventListener('load', function () {
const fromNow = function (ele) {
let now = moment(),
then = moment(ele.dataset.target),
before = ele.children[0],
after = ele.children[1],
i18n = ele.title,
isBefore = then.isBefore(now),
monthsHave31Days = [0, 2, 4, 6, 7, 9, 11], // 月份从0开始
year = isBefore ? now.year() - then.year() : then.year() - now.year(),
month = isBefore ? now.month() - then.month() : then.month() - now.month(),
day = isBefore ? now.date() - then.date() : then.date() - now.date(),
hour = isBefore ? now.hour() - then.hour() : then.hour() - now.hour(),
minute = isBefore ? now.minute() - then.minute() : then.minute() - now.minute(),
second = isBefore ? now.second() - then.second() : then.second() - now.second(),
result = "";
if (second < 0) {
minute--;
second += 60;
}
if (minute < 0) {
hour--;
minute += 60;
}
if (hour < 0) {
day--;
hour += 24;
}
if (day < 0) {
month--;
if (monthsHave31Days.includes((isBefore ? then : now).month())) { day += 31; }
else if ((isBefore ? then : now).month() === 1) {
if ((isBefore ? then : now).year() % 4 === 0) { day += 29; }
else { day += 28; }
}
else { day += 30; }
}
if (month < 0) {
year--;
month += 12;
}
if (year > 0) { result += `<span class="countdown-num">${year}</` + 'span>年'; }
if (month > 0) { result += `<span class="countdown-num">${month}</` + 'span>' + i18n.substring(2); }
else if (result !== "") { result += `<span class="countdown-num">${0}</` + 'span>' + i18n.substring(2); }
if (day > 0) { result += `<span class="countdown-num">${day}</` + 'span>天'; }
else if (result !== "") { result += `<span class="countdown-num">${0}</` + 'span>天'; }
if(year == 0 && month == 0) {
if (hour > 0) { result += `<span class="countdown-num">${hour}</` + 'span>' + i18n.substring(0, 2); }
else if (result !== "") { result += `<span class="countdown-num">${0}</` + 'span>' + i18n.substring(0, 2); }
if(day == 0) {
if (minute > 0) { result += `<span class="countdown-num">${minute}</` + 'span>分'; }
else if (result !== "") { result += `<span class="countdown-num">${0}</` + 'span>分'; }
if (second > 0) { result += `<span class="countdown-num">${second}</` + 'span>秒'; }
else if (result !== "") { result += `<span class="countdown-num">${0}</` + 'span>秒'; }
}
}
if(isBefore) {
var countdown = before.querySelector( '.countdown' );
if(countdown) { countdown.innerHTML = result; }
before.style.display = "";
after.style.display = "none";
}
else {
var countdown = after.querySelector( '.countdown' );
if(countdown) { countdown.innerHTML = result; }
after.style.display = "";
before.style.display = "none";
}
};
const run = () => {
document.querySelectorAll( '.countdownNode:not(.disabled)' ).forEach((ele) => {
fromNow(ele);
ele.style.visibility = "visible";
});
};
document.querySelectorAll( '.countdownNode' ).forEach((ele) => {
const time = moment(ele.dataset.target);
if (!time || !time.isValid()) {
ele.classList.add("error", "disabled")
ele.innerHTML = "(发生了致命错误!)";
return;
}
var before = ele.children[0],
after = ele.children[1];
before.innerHTML = before.innerHTML.replace("$1", '<span class="countdown"><' + '/span>');
after.innerHTML = after.innerHTML.replace("$1", '<span class="countdown"><' + '/span>');
});
run();
window.setInterval(run, 1000);
});
//
CSS样式表:
*/
.countdownNode {
visibility:hidden;
}
/*