script.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. document.addEventListener('DOMContentLoaded', () => {
  2. // --- 交互功能1: 响应式汉堡菜单 ---
  3. const hamburgerMenu = document.getElementById('hamburger-menu');
  4. const mainNav = document.querySelector('.main-nav');
  5. if (hamburgerMenu && mainNav) {
  6. hamburgerMenu.addEventListener('click', () => {
  7. mainNav.classList.toggle('active');
  8. });
  9. }
  10. // --- 交互功能2 (进阶): 白天/黑夜模式切换 ---
  11. const themeSwitcher = document.getElementById('theme-switcher');
  12. const body = document.body;
  13. const currentTheme = localStorage.getItem('theme');
  14. if (currentTheme === 'dark') {
  15. body.classList.add('dark-mode');
  16. if (themeSwitcher) themeSwitcher.textContent = '🌙';
  17. } else {
  18. if (themeSwitcher) themeSwitcher.textContent = '☀️';
  19. }
  20. if (themeSwitcher) {
  21. themeSwitcher.addEventListener('click', () => {
  22. body.classList.toggle('dark-mode');
  23. if (body.classList.contains('dark-mode')) {
  24. localStorage.setItem('theme', 'dark');
  25. themeSwitcher.textContent = '🌙';
  26. } else {
  27. localStorage.setItem('theme', 'light');
  28. themeSwitcher.textContent = '☀️';
  29. }
  30. });
  31. }
  32. // --- 交互功能3 (进阶): 联系表单异步提交 (配合PHP) ---
  33. const contactForm = document.getElementById('contact-form');
  34. if (contactForm) {
  35. contactForm.addEventListener('submit', function(event) {
  36. event.preventDefault();
  37. let statusDiv = this.querySelector('.form-status');
  38. if (!statusDiv) {
  39. statusDiv = document.createElement('div');
  40. statusDiv.className = 'form-status';
  41. this.appendChild(statusDiv);
  42. }
  43. const formData = new FormData(this);
  44. fetch('handle_contact.php', { method: 'POST', body: formData })
  45. .then(response => response.text().then(text => ({ ok: response.ok, text })))
  46. .then(({ ok, text }) => {
  47. statusDiv.textContent = text;
  48. statusDiv.style.color = ok ? 'green' : 'red';
  49. if (ok) contactForm.reset();
  50. })
  51. .catch(error => {
  52. statusDiv.textContent = '提交失败,请检查网络连接。';
  53. statusDiv.style.color = 'red';
  54. });
  55. });
  56. }
  57. // --- 交互功能4 (进阶): 自动播放轮播图 ---
  58. const carousel = document.querySelector('.carousel');
  59. if (carousel) {
  60. const items = carousel.querySelectorAll('.carousel-item');
  61. const prevBtn = carousel.querySelector('.carousel-control.prev');
  62. const nextBtn = carousel.querySelector('.carousel-control.next');
  63. let currentIndex = 0;
  64. let intervalId = null;
  65. if (items.length > 1) {
  66. const showItem = (index) => items.forEach((item, i) => item.classList.toggle('active', i === index));
  67. const nextItem = () => { currentIndex = (currentIndex + 1) % items.length; showItem(currentIndex); };
  68. const prevItem = () => { currentIndex = (currentIndex - 1 + items.length) % items.length; showItem(currentIndex); };
  69. const startCarousel = () => { stopCarousel(); intervalId = setInterval(nextItem, 3000); };
  70. const stopCarousel = () => clearInterval(intervalId);
  71. if (nextBtn) nextBtn.addEventListener('click', () => { nextItem(); startCarousel(); });
  72. if (prevBtn) prevBtn.addEventListener('click', () => { prevItem(); startCarousel(); });
  73. carousel.addEventListener('mouseenter', stopCarousel);
  74. carousel.addEventListener('mouseleave', startCarousel);
  75. showItem(currentIndex);
  76. startCarousel();
  77. }
  78. }
  79. });