main.js 982 B

123456789101112131415161718192021222324252627282930313233343536373839
  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 (topBtn) {
  22. if (document.documentElement.scrollTop > px) {
  23. topBtn.classList.add("show");
  24. } else {
  25. topBtn.classList.remove("show");
  26. }
  27. }
  28. };
  29. if (topBtn) {
  30. topBtn.addEventListener('click', () => {
  31. document.documentElement.scrollTop = 0;
  32. }, false);
  33. }