script.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // H5页面交互脚本
  2. document.addEventListener('DOMContentLoaded', function() {
  3. // 平滑滚动到锚点
  4. const smoothScroll = () => {
  5. const links = document.querySelectorAll('a[href^="#"]');
  6. links.forEach(link => {
  7. link.addEventListener('click', function(e) {
  8. e.preventDefault();
  9. const targetId = this.getAttribute('href');
  10. if (targetId === '#') return;
  11. const targetElement = document.querySelector(targetId);
  12. if (targetElement) {
  13. window.scrollTo({
  14. top: targetElement.offsetTop - 20,
  15. behavior: 'smooth'
  16. });
  17. }
  18. });
  19. });
  20. };
  21. // 返回顶部按钮
  22. const backToTop = () => {
  23. const backButton = document.querySelector('.back-button');
  24. if (backButton && backButton.getAttribute('href') === '#') {
  25. backButton.addEventListener('click', function(e) {
  26. e.preventDefault();
  27. window.scrollTo({
  28. top: 0,
  29. behavior: 'smooth'
  30. });
  31. });
  32. }
  33. };
  34. // 目录生成(如果页面有目录区域)
  35. // const generateTOC = () => {
  36. // const tocContainer = document.querySelector('.toc');
  37. // if (!tocContainer) return;
  38. // const sections = document.querySelectorAll('.section-title');
  39. // if (sections.length === 0) return;
  40. // const tocList = document.createElement('ul');
  41. // tocList.className = 'toc-list';
  42. // sections.forEach((section, index) => {
  43. // const sectionId = `section-${index + 1}`;
  44. // section.id = sectionId;
  45. // const listItem = document.createElement('li');
  46. // listItem.className = 'toc-item';
  47. // const link = document.createElement('a');
  48. // link.className = 'toc-link';
  49. // link.href = `#${sectionId}`;
  50. // link.textContent = section.textContent;
  51. // listItem.appendChild(link);
  52. // tocList.appendChild(listItem);
  53. // });
  54. // tocContainer.appendChild(tocList);
  55. // };
  56. // 夜间模式切换
  57. // const darkModeToggle = () => {
  58. // const darkModeBtn = document.getElementById('dark-mode-toggle');
  59. // if (!darkModeBtn) return;
  60. // const body = document.body;
  61. // const container = document.querySelector('.container');
  62. // // 检查localStorage中的设置
  63. // const isDarkMode = localStorage.getItem('darkMode') === 'true';
  64. // if (isDarkMode) {
  65. // body.classList.add('dark-mode');
  66. // if (container) container.classList.add('dark-mode');
  67. // darkModeBtn.textContent = '切换到日间模式';
  68. // }
  69. // darkModeBtn.addEventListener('click', () => {
  70. // body.classList.toggle('dark-mode');
  71. // if (container) container.classList.toggle('dark-mode');
  72. // const isNowDark = body.classList.contains('dark-mode');
  73. // localStorage.setItem('darkMode', isNowDark);
  74. // darkModeBtn.textContent = isNowDark ? '切换到日间模式' : '切换到夜间模式';
  75. // });
  76. // };
  77. // 添加夜间模式样式
  78. // const addDarkModeStyles = () => {
  79. // if (!document.querySelector('#dark-mode-styles')) {
  80. // const style = document.createElement('style');
  81. // style.id = 'dark-mode-styles';
  82. // style.textContent = `
  83. // body.dark-mode {
  84. // background-color: #1a202c;
  85. // color: #e2e8f0;
  86. // }
  87. // .container.dark-mode {
  88. // background-color: #2d3748;
  89. // color: #e2e8f0;
  90. // box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
  91. // }
  92. // .dark-mode .header {
  93. // border-bottom-color: #4a5568;
  94. // }
  95. // .dark-mode .section-title {
  96. // color: #e2e8f0;
  97. // border-bottom-color: #4a5568;
  98. // }
  99. // .dark-mode .paragraph,
  100. // .dark-mode .sub-title {
  101. // color: #cbd5e0;
  102. // }
  103. // .dark-mode .toc {
  104. // background-color: #4a5568;
  105. // border-left-color: #667eea;
  106. // }
  107. // .dark-mode .toc-link {
  108. // color: #cbd5e0;
  109. // }
  110. // .dark-mode .toc-link:hover {
  111. // color: #90cdf4;
  112. // }
  113. // .dark-mode .footer {
  114. // border-top-color: #4a5568;
  115. // }
  116. // .dark-mode .copyright {
  117. // color: #a0aec0;
  118. // }
  119. // .dark-mode .important {
  120. // background-color: #44337a;
  121. // border-left-color: #9f7aea;
  122. // }
  123. // .dark-mode .warning {
  124. // background-color: #742a2a;
  125. // border-left-color: #fc8181;
  126. // }
  127. // .dark-mode .note {
  128. // background-color: #2c5282;
  129. // border-left-color: #63b3ed;
  130. // }
  131. // `;
  132. // document.head.appendChild(style);
  133. // }
  134. // };
  135. // 初始化所有功能
  136. smoothScroll();
  137. backToTop();
  138. // addDarkModeStyles();
  139. // darkModeToggle();
  140. // 添加阅读进度指示器
  141. const addReadingProgress = () => {
  142. const progressBar = document.createElement('div');
  143. progressBar.style.cssText = `
  144. position: fixed;
  145. top: 0;
  146. left: 0;
  147. width: 0%;
  148. height: 3px;
  149. background: linear-gradient(90deg, #667eea, #764ba2);
  150. z-index: 1000;
  151. transition: width 0.1s;
  152. `;
  153. document.body.appendChild(progressBar);
  154. window.addEventListener('scroll', () => {
  155. const winScroll = document.body.scrollTop || document.documentElement.scrollTop;
  156. const height = document.documentElement.scrollHeight - document.documentElement.clientHeight;
  157. const scrolled = (winScroll / height) * 100;
  158. progressBar.style.width = scrolled + '%';
  159. });
  160. };
  161. addReadingProgress();
  162. // 打印功能
  163. const printButton = document.getElementById('print-page');
  164. if (printButton) {
  165. printButton.addEventListener('click', () => {
  166. window.print();
  167. });
  168. }
  169. // 分享功能(基础版)
  170. const shareButton = document.getElementById('share-page');
  171. if (shareButton && navigator.share) {
  172. shareButton.style.display = 'inline-block';
  173. shareButton.addEventListener('click', async () => {
  174. try {
  175. await navigator.share({
  176. title: document.title,
  177. text: document.querySelector('.page-title')?.textContent || 'BEX条款',
  178. url: window.location.href
  179. });
  180. } catch (err) {
  181. console.log('分享取消或出错:', err);
  182. }
  183. });
  184. }
  185. });