// 切換分頁時移至列表區塊
document.addEventListener("DOMContentLoaded", () => {
    if (document.querySelector('#news-block')) {
        let offsetHeight = document.querySelector('#news-block').offsetTop;
        if (offsetHeight) window.scrollTo(0, offsetHeight);
    }
});

function historyBack() {
    let prevPage = window.location.href;

    // 若無法返回上一頁則回到首頁
    window.history.go(-1);

    setTimeout(() => {
        if (window.location.href == prevPage) {
            window.location.href = "/";
        }
    }, 500);
}

let topBtn = document.querySelector('.top-btn');

window.onscroll = () => {
    const px = 100;
    if (topBtn) {
        if (document.documentElement.scrollTop > px) {
            topBtn.classList.add("show");
        } else {
            topBtn.classList.remove("show");
        }
    }
};

if (topBtn) {
    topBtn.addEventListener('click', () => {
        document.documentElement.scrollTop = 0;
    }, false);
}