(function ($) {
const childGroup = $('#sampleWrap').children();
const childList = [];
childGroup.each(function (i, ele) {
childList.push(ele);
});
const sliceAmount = 60;
const totalPage = Math.ceil(childList.length / sliceAmount);
let hashNumber = 0;
function hashCheck() {
if (location.hash.slice(1) >= 1 && location.hash.slice(1) <= totalPage) {
hashNumber = location.hash.slice(1) * 1;
} else {
hashNumber = 1;
location.hash = '#1';
}
}
function randerText(hash, amount) {
for (let i = (hash - 1) * amount; i < hash * amount; i++) {
if (i === (hash - 1) * amount) {
$('#sampleWrap').empty();
}
$('#sampleWrap').append(childList[i]);
$('.sample-page').text(`${hash} / ${totalPage}`);
}
}
// 頁數切換功能
const pagerDesktop = $('.pagebox.page-box');
const showItems = 5;
let pagerStep = '';
function appendHash(page) {
if (page === location.hash.slice(1) * 1) {
pagerDesktop.append(`${page}`);
} else if (page >= 1 && page <= totalPage) {
pagerDesktop.append(`${page}`);
}
}
function pagerShowDesktop(hash) {
pagerDesktop.empty();
if (hash >= 3) {
pagerDesktop.append(`最前頁`);
}
pagerDesktop.append(` 上一頁`);
if (hash !== 1) {
pagerDesktop.append(`...`);
}
const halfShow = Math.ceil(showItems / 2);
if (hash < halfShow) {
for (let i = 1; i <= showItems; i++) {
appendHash(i);
}
} else if (hash > totalPage - halfShow) {
for (let i = showItems - 1; i >= 0; i--) {
appendHash(totalPage - i);
}
} else {
for (let i = (halfShow - 1) * -1; i < halfShow; i++) {
appendHash(hash + i);
}
}
if (hash !== totalPage) {
pagerDesktop.append(`...`);
}
pagerDesktop.append(`下一頁 `);
if (hash <= totalPage - 2) {
pagerDesktop.append(`最末頁`);
}
}
pagerDesktop.on('click', '.page', function (e) {
const targetHash = $(this).data('hash');
location.hash = '#' + targetHash;
$('#wrapper').scrollTop(0)
});
// hash change
$(window).on('hashchange', function (e) {
hashCheck();
pagerShowDesktop(hashNumber);
randerText(hashNumber, sliceAmount);
});
// init
$(window).trigger('hashchange');
})($);
// ==========================================================
// 優惠倒數計時器
// ==========================================================
;(function($) {
const counter = $('.countdown-timer')
function strPadLeft(string, length, symbol) {
let newString = string
let newSymbol = symbol || '0'
if(newString.length < length) {
newString = newSymbol + newString
return strPadLeft(newString, length, newSymbol)
} else {
return newString
}
}
function countDown(endTime) {
const now = Date.now()
const leftTime = endTime - now
let leftSec = Math.floor(leftTime / 1000) % 60
let leftMin = Math.floor(leftTime / 1000 / 60) % 60
let leftHour = Math.floor(leftTime / 1000 / 60 / 60) % 24
let leftDay = Math.floor(leftTime / 1000 / 60 / 60 / 24)
let timeString = ''
if(leftTime > 0) {
if(leftDay > 0) {
timeString += `${leftDay}日 `
}
if(leftHour > 0) {
leftHour += ''
leftHour = strPadLeft(leftHour, 2)
timeString += `${leftHour}:`
}
leftMin += ''
leftMin = strPadLeft(leftMin, 2)
timeString += `${leftMin}:`
leftSec += ''
leftSec = strPadLeft(leftSec, 2)
timeString += `${leftSec}`
setTimeout(countDown, 1000, endTime)
} else {
timeString = '優惠到期'
}
counter.find('.font-timer').text(timeString)
}
if(counter.length) {
const deadline = counter.data('deadline')
const limitTime = new Date(deadline).getTime()
setTimeout(countDown, 1000, limitTime)
countDown(limitTime)
}
})($)