LLWiki正在建设中,欢迎加入我们!
“Widget:Countdown”的版本间差异
跳转到导航
跳转到搜索
小 |
小 |
||
第6行: | 第6行: | ||
window.addEventListener('load', function () { |
window.addEventListener('load', function () { |
||
var timeObj = {}; |
var timeObj = {}; |
||
const fromNow = function ( |
const fromNow = function (ele) { |
||
const now = moment() |
const now = moment(), |
||
then = moment(ele.dataset.target), |
|||
before = ele.children[1], |
|||
⚫ | |||
after = ele.children[2], |
|||
isBefore = then.isBefore(now), |
|||
⚫ | |||
let year = isBefore ? now.year() - then.year() : then.year() - now.year(), |
let year = isBefore ? now.year() - then.year() : then.year() - now.year(), |
||
month = isBefore ? now.month() - then.month() : then.month() - now.month(), |
month = isBefore ? now.month() - then.month() : then.month() - now.month(), |
||
第61行: | 第64行: | ||
} |
} |
||
if (hour > 0) { |
if (hour > 0) { |
||
result += `<span class="countdown-num">${hour}</` + 'span> |
result += `<span class="countdown-num">${hour}</` + 'span>' + ele.children[0].innerHTML; |
||
} else if (result !== "") { |
} else if (result !== "") { |
||
result += `<span class="countdown-num">${0}</` + 'span> |
result += `<span class="countdown-num">${0}</` + 'span>' + ele.children[0].innerHTML; |
||
} |
} |
||
if (minute > 0) { |
if (minute > 0) { |
||
第75行: | 第78行: | ||
result += `<span class="countdown-num">${0}</` + 'span>秒'; |
result += `<span class="countdown-num">${0}</` + 'span>秒'; |
||
} |
} |
||
if(isBefore) { |
|||
before.innerHTML = before.innerHTML.replace("$1", result.replace(/(\d) /g, "$1")); |
|||
after.style.display = "none"; |
|||
⚫ | |||
else { |
|||
afet.innerHTML = after.innerHTML.replace("$1", result.replace(/(\d) /g, "$1")); |
|||
before.style.display = "none"; |
|||
} |
|||
}; |
}; |
||
const run = () => { |
const run = () => { |
||
document.querySelectorAll(".countdownNode:not(.disabled)").forEach((ele) |
document.querySelectorAll(".countdownNode:not(.disabled)").forEach( fromNow(ele) ); |
||
ele.innerHTML = fromNow(timeObj[ele.id], ele.dataset.before || "$1前", ele.dataset.after || "还剩$1"); |
|||
⚫ | |||
}; |
}; |
||
document.querySelectorAll(".countdownNode").forEach((ele) => { |
document.querySelectorAll(".countdownNode").forEach((ele) => { |
2020年8月24日 (一) 04:43的版本
JavaScript脚本:
"use strict";
window.addEventListener('load', function () {
var timeObj = {};
const fromNow = function (ele) {
const now = moment(),
then = moment(ele.dataset.target),
before = ele.children[1],
after = ele.children[2],
isBefore = then.isBefore(now),
monthsHave31Days = [0, 2, 4, 6, 7, 9, 11]; // 月份从0开始
let 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();
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;
}
let result = "";
if (year > 0) {
result += `<span class="countdown-num">${year}</` + 'span>年';
}
if (month > 0) {
result += `<span class="countdown-num">${month}</` + 'span>月';
} else if (result !== "") {
result += `<span class="countdown-num">${0}</` + 'span>月';
}
if (day > 0) {
result += `<span class="countdown-num">${day}</` + 'span>日';
} else if (result !== "") {
result += `<span class="countdown-num">${0}</` + 'span>日';
}
if (hour > 0) {
result += `<span class="countdown-num">${hour}</` + 'span>' + ele.children[0].innerHTML;
} else if (result !== "") {
result += `<span class="countdown-num">${0}</` + 'span>' + ele.children[0].innerHTML;
}
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) {
before.innerHTML = before.innerHTML.replace("$1", result.replace(/(\d) /g, "$1"));
after.style.display = "none";
}
else {
afet.innerHTML = after.innerHTML.replace("$1", result.replace(/(\d) /g, "$1"));
before.style.display = "none";
}
};
const run = () => {
document.querySelectorAll(".countdownNode:not(.disabled)").forEach( fromNow(ele) );
};
document.querySelectorAll(".countdownNode").forEach((ele) => {
const time = moment(ele.dataset.target);
if (!time || !time.isValid()) {
ele.classList.add("error", "disabled")
ele.innerHTML = "(发生了致命错误!)";
return;
}
timeObj[ele.id] = time;
});
run();
window.setInterval(run, 1000);
});
//
CSS样式表:
*/
.countdown-num {
display:inline-block;
width:1.25em;
text-align:right;
}
/*