main.js 910 B

1234567891011121314151617181920212223242526272829303132333435
  1. // 切換分頁時移至列表區塊
  2. document.addEventListener("DOMContentLoaded", () => {
  3. if (document.querySelector('#news-block')) {
  4. let offsetHeight = document.querySelector('#news-block').offsetTop;
  5. if (offsetHeight) window.scrollTo(0, offsetHeight);
  6. }
  7. });
  8. function historyBack() {
  9. let prevPage = window.location.href;
  10. // 若無法返回上一頁則回到首頁
  11. window.history.go(-1);
  12. setTimeout(() => {
  13. if (window.location.href == prevPage) {
  14. window.location.href = "/";
  15. }
  16. }, 500);
  17. }
  18. let topBtn = document.querySelector('.top-btn');
  19. window.onscroll = () => {
  20. const px = 100;
  21. if (document.documentElement.scrollTop > px) {
  22. topBtn.classList.add("show");
  23. } else {
  24. topBtn.classList.remove("show");
  25. }
  26. };
  27. topBtn.addEventListener('click', () => {
  28. document.documentElement.scrollTop = 0;
  29. }, false);