码桶

发现社区成员的开源项目

CMR /

爱情苦海

公开
main
main.js3 KB
/* ========================================
   aqkh.com — 爱情苦海 Scripts
   ======================================== */

(function () {
  'use strict';

  // ---- Theme Switcher ----
  function initThemeSwitcher() {
    var toggle = document.getElementById('theme-toggle');
    var panel = document.getElementById('theme-panel');
    var themeLink = document.getElementById('theme-css');
    var options = document.querySelectorAll('.theme-option');

    if (!toggle || !panel || !themeLink) return;

    // Load saved theme
    var saved = localStorage.getItem('aqkh-theme') || 'minimal';
    applyTheme(saved);

    // Toggle panel
    toggle.addEventListener('click', function (e) {
      e.stopPropagation();
      panel.classList.toggle('open');
    });

    // Close panel on outside click
    document.addEventListener('click', function (e) {
      if (!panel.contains(e.target) && e.target !== toggle) {
        panel.classList.remove('open');
      }
    });

    // Theme option clicks
    options.forEach(function (btn) {
      btn.addEventListener('click', function () {
        var theme = btn.getAttribute('data-theme');
        applyTheme(theme);
        localStorage.setItem('aqkh-theme', theme);
        panel.classList.remove('open');
      });
    });

    function applyTheme(theme) {
      themeLink.setAttribute('href', 'css/themes/' + theme + '.css');

      // Update active state
      options.forEach(function (btn) {
        btn.classList.toggle('active', btn.getAttribute('data-theme') === theme);
      });
    }
  }

  // ---- Scroll Animations ----
  function initScrollAnimations() {
    var elements = document.querySelectorAll('.fade-in');

    if (!('IntersectionObserver' in window)) {
      elements.forEach(function (el) {
        el.classList.add('visible');
      });
      return;
    }

    var observer = new IntersectionObserver(
      function (entries) {
        entries.forEach(function (entry) {
          if (entry.isIntersecting) {
            entry.target.classList.add('visible');
            observer.unobserve(entry.target);
          }
        });
      },
      {
        threshold: 0.15,
        rootMargin: '0px 0px -40px 0px'
      }
    );

    elements.forEach(function (el, index) {
      el.style.transitionDelay = (index % 4) * 0.15 + 's';
      observer.observe(el);
    });
  }

  // ---- Scroll Indicator ----
  function initScrollIndicator() {
    var indicator = document.querySelector('.scroll-indicator');
    if (!indicator) return;

    var hidden = false;
    window.addEventListener(
      'scroll',
      function () {
        if (!hidden && window.scrollY > 100) {
          indicator.style.opacity = '0';
          indicator.style.transition = 'opacity 0.5s ease';
          hidden = true;
        }
      },
      { passive: true }
    );
  }

  // ---- Initialize ----
  function init() {
    initThemeSwitcher();
    initScrollAnimations();
    initScrollIndicator();
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', init);
  } else {
    init();
  }
})();