/** * AI Creative Studio - Main JavaScript * Vanilla JavaScript (jQuery不使用) * * @package AI_Creative_Studio */ (function() { 'use strict'; /** * DOM読み込み完了後に実行 */ document.addEventListener('DOMContentLoaded', function() { // 外部リンクに target="_blank" を追加 initExternalLinks(); // スムーズスクロール initSmoothScroll(); // デバッグ情報の出力 if (typeof aicsData !== 'undefined') { console.log('AI Creative Studio Theme Loaded - Version:', aicsData.theme.version); } }); /** * 外部リンクの処理 */ function initExternalLinks() { const links = document.querySelectorAll('a[href^="http"]'); const currentDomain = window.location.hostname; links.forEach(function(link) { try { const linkDomain = new URL(link.href).hostname; if (linkDomain !== currentDomain) { link.setAttribute('target', '_blank'); link.setAttribute('rel', 'noopener noreferrer'); } } catch (e) { // URL解析エラーの場合は何もしない } }); } /** * スムーズスクロールの初期化 */ function initSmoothScroll() { const links = document.querySelectorAll('a[href^="#"]'); links.forEach(function(link) { link.addEventListener('click', function(e) { const href = this.getAttribute('href'); if (href === '#' || href === '#top') { e.preventDefault(); window.scrollTo({ top: 0, behavior: 'smooth' }); return; } const target = document.querySelector(href); if (target) { e.preventDefault(); target.scrollIntoView({ behavior: 'smooth', block: 'start' }); } }); }); } })(); /** * モバイルメニューの初期化 */ function initMobileMenu() { const menuToggle = document.querySelector('.menu-toggle'); const navigation = document.querySelector('.main-navigation'); if (!menuToggle || !navigation) { return; } // メニュートグルクリックイベント menuToggle.addEventListener('click', function() { const isExpanded = this.getAttribute('aria-expanded') === 'true'; // aria-expanded属性を切り替え this.setAttribute('aria-expanded', !isExpanded); // ナビゲーションの表示切り替え navigation.classList.toggle('is-open'); // body要素のスクロールを制御 document.body.style.overflow = !isExpanded ? 'hidden' : ''; }); // メニュー外クリックで閉じる document.addEventListener('click', function(e) { if (!navigation.contains(e.target) && !menuToggle.contains(e.target)) { if (navigation.classList.contains('is-open')) { menuToggle.setAttribute('aria-expanded', 'false'); navigation.classList.remove('is-open'); document.body.style.overflow = ''; } } }); // ESCキーでメニューを閉じる document.addEventListener('keydown', function(e) { if (e.key === 'Escape' && navigation.classList.contains('is-open')) { menuToggle.setAttribute('aria-expanded', 'false'); navigation.classList.remove('is-open'); document.body.style.overflow = ''; menuToggle.focus(); } }); } // 既存のDOMContentLoadedイベントに追加 document.addEventListener('DOMContentLoaded', function() { // 既存の関数 initExternalLinks(); initSmoothScroll(); // 新しく追加 initMobileMenu(); // デバッグ情報 if (typeof aicsData !== 'undefined') { console.log('AI Creative Studio Theme Loaded - Version:', aicsData.theme.version); } });