<script>
/* ---------------------------
メガメニュー(1段・プルダウンあり) v1.0.0
--------------------------- */
/*--- PC or SP 判定 ---*/
function windowPCorSP() {
if (window.matchMedia('(min-width: 770px)').matches) { /* 画面幅が770px以上のときの処理(PC) */
return 'pc';
} else { /* 画面幅が769px以下のときの処理(SP) */
return 'sp';
}
};
/*--- メガメニュー ---*/
jQuery(function() {
/*---------------------------------
初期化 (変数定義やクラス付与) START
---------------------------------*/
/* 設定:PC時の開閉方式(true=ホバー、false=クリック) */
const MEGA_PC_OPEN_BY_HOVER = true;
/* メガメニューのパーツを配置しているURLのパスを指定する (先頭に「/」を入れてください) */
const megaMenuPartsPath = '/js/mega-menu04-parts';
/* メガメニューを一時的に格納するセクション */
const megaMenuTemporarilyContentsSec = jQuery('.js-megaMenu_contents_sec');
/* theme-fo-base */
const themeFoBase = jQuery('#fo-base');
/* メインビジュアル(main-visual) */
const mainVisual = jQuery('.main-visual');
/*--- 初期化 (変数定義やクラス付与) END ---*/
/*--- メガメニューを別ページから読み込んで初期機能設定 START ---*/
megaMenuTemporarilyContentsSec.load(`${megaMenuPartsPath} .contents`, function() {
/*---------------------------------
メガメニューPC時 START
---------------------------------*/
/* ナビゲーションメガメニュー */
const megaMenuSec = jQuery('.js-megaMenu_custom_sec');
/* メガダウンメニュー */
const megaDownMenuSec = jQuery('.js-megaMenu_down_list_sec');
/* プルダウンリスト用メガダウンメニュー */
const megaDownMenuSec02 = jQuery('.js-megamenu04_pullList_sec');
/* メガメニューナビゲーションリンク */
const nav_menu_link = jQuery('.js-megaMenu_custom_sec .nav_menu_list a');
/* ダウンメニューか否か判定用テキスト */
const downMenu_reference_text = jQuery('.js-downMenu_reference_row p');
/* メガメニュー要素以外のセクションを削除 */
jQuery('.js-megaMenu_contents_sec section')
.not('.js-megaMenu_custom_sec, .js-megaMenu_down_list_sec')
.css('display', 'none');
/* メガメニューを「#fo-base」の上に配置 */
megaMenuTemporarilyContentsSec.insertBefore(themeFoBase);
/* メガメニューを囲んでいる不要なタグを削除 */
megaMenuSec.unwrap().unwrap();
/* JSで「display: none;」を付与して、チラツキ防止用クラスを外す */
megaDownMenuSec.css('display', 'none');
/*--- ダウンメニューか否か判定してクラスを付与 ----*/
nav_menu_link.each(function(index01, element01) {
downMenu_reference_text.each(function(index02, element02) {
if (jQuery(element01).text() === jQuery(element02).text()) {
/* idを生成 */
let id = index01 + 101;
let list = jQuery(element01).parent();
let downMenu = jQuery(element02).closest('.js-megaMenu_down_list_sec');
/* ダウンメニューをリンクリスト内部に移動 */
downMenu.appendTo(list);
/* ダウンメニュー用のクラスを付与 */
list.addClass('downMenu');
/* ダウンメニュー用のidをdata属性で付与 */
list.attr('data-mega-target', `${id}`);
downMenu.attr('data-mega-id', `${id}`);
}
});
});
/* トグルクリック処理 */
jQuery('.main-megaMenu').on('click', '.toggle_icon', function(e) {
e.preventDefault();
const toggle = jQuery(this);
const li = toggle.closest('li');
const childUl = li.children('ul').first();
/* トグルの開閉 */
if (childUl.length) {
childUl.slideToggle(200);
li.toggleClass('is-open');
}
});
/* 判定用に使用したテキストを削除 */
jQuery('.js-downMenu_reference_row').css('display', 'none');
/* ダウンメニューのリンクのみを取得 */
const downMenuLink = jQuery('.js-megaMenu_custom_sec .nav_menu_list .downMenu');
/* PC時の開閉方式を切り替え */
if (MEGA_PC_OPEN_BY_HOVER) {
downMenuLink.hover(
function() {
const $li = jQuery(this);
const id = $li.attr('data-mega-target');
$li.addClass('is-opened');
jQuery(`.js-megaMenu_down_list_sec[data-mega-id="${id}"]`).css('display', '');
},
function() {
const $li = jQuery(this);
const id = $li.attr('data-mega-target');
jQuery(`.js-megaMenu_down_list_sec[data-mega-id="${id}"]`).css('display', 'none');
$li.removeClass('is-opened');
}
);
} else {
jQuery(document)
.off('click.mm-open', '.js-megaMenu_custom_sec .downMenu > a')
.on('click.mm-open', '.js-megaMenu_custom_sec .downMenu > a', function(e) {
e.preventDefault();
if (windowPCorSP() === 'pc') {
const li = jQuery(this).parent('.downMenu');
const targetId = li.attr('data-mega-target');
const downMenu = jQuery(
`.js-megaMenu_down_list_sec[data-mega-id="${targetId}"]`
);
const isOpened = li.hasClass('is-opened');
jQuery('.js-megaMenu_down_list_sec').css('display', 'none');
jQuery('.downMenu').removeClass('is-opened');
if (!isOpened) {
downMenu.css('display', '');
li.addClass('is-opened');
}
}
});
/* ヘッダー内クリックでのクローズ判定(クリックした箇所以外をクリックしたら閉じる) */
jQuery(document)
.off('mousedown.mm-header')
.on('mousedown.mm-header', function(e) {
if (windowPCorSP() !== 'pc' || MEGA_PC_OPEN_BY_HOVER) return;
const $t = jQuery(e.target);
const insideHeader = $t.closest('.js-megaMenu_custom_sec').length > 0;
const onTrigger = $t.closest(
'.js-megaMenu_custom_sec .nav_menu_list .downMenu > a'
).length > 0;
const insidePanel = $t.closest(
'.js-megaMenu_custom_sec .js-megaMenu_down_list_sec'
).length > 0;
if (insideHeader && !onTrigger && !insidePanel) {
jQuery('.js-megaMenu_down_list_sec').css('display', 'none');
jQuery('.downMenu').removeClass('is-opened');
}
});
}
/* メガメニュー外クリックで閉じる(PC時のみ) */
jQuery(document)
.off('mousedown.mm-outside')
.on('mousedown.mm-outside', function(e) {
if (windowPCorSP() !== 'pc') return;
const $t = jQuery(e.target);
const insideMega =
$t.closest('.js-megaMenu_custom_sec').length > 0 ||
$t.closest('.js-megaMenu_down_list_sec').length > 0;
if (!insideMega) {
jQuery('.js-megaMenu_down_list_sec').css('display', 'none');
jQuery('.downMenu').removeClass('is-opened');
}
});
/*---------------------------------
プルダウン用リスト(js-menu_lists)のPC処理 START
---------------------------------*/
function pcMegaPullDownSetUp() {
/* js-megamenu04_pullList_sec を持つ downMenu に position: relative を追加 */
megaDownMenuSec02.closest('.downMenu').css('position', 'relative');
/* メインメニュー(プルダウン用リスト) */
const level_01 = jQuery('.js-menu_lists > div > ul > li');
const level_02 = jQuery('.js-menu_lists > div > ul > li > ul > li');
const level_03 = jQuery('.js-menu_lists > div > ul > li > ul > li > ul > li');
if (!level_01.length) {
return;
}
/* 1階層目 */
level_01.each(function() {
jQuery(this).addClass('js-level-01');
});
/* 2階層目 */
level_02.each(function() {
jQuery(this).addClass('js-level-02');
});
/* 3階層目 */
level_03.each(function() {
jQuery(this).addClass('js-level-03');
});
/* 1階層目トリガー */
level_01.each(function() {
const el = jQuery(this);
if (el.find('.js-level-02').length > 0) {
el.addClass('js-level-01-trigger').removeClass('js-level-01');
const iconElement = jQuery('<span class="toggle_icon"></span>');
const link = el.find('a').first();
if (link.length) {
link.append(iconElement);
}
}
});
/* 2階層目 [ul] をコンテナ化 */
level_02.each(function() {
const level_01_cont = jQuery(this).parent();
level_01_cont.addClass('js-level-01-cont').hide();
});
/* 2階層目トリガー */
level_02.each(function() {
const el = jQuery(this);
if (el.find('.js-level-03').length > 0) {
el.addClass('js-level-02-trigger').removeClass('js-level-02');
const iconElement = jQuery('<span class="toggle_icon"></span>');
const link = el.find('a').first();
if (link.length) {
link.append(iconElement);
}
}
});
/* 3階層目 [ul] をコンテナ化 */
level_03.each(function() {
const level_02_cont = jQuery(this).parent();
level_02_cont.addClass('js-level-02-cont').hide();
});
/* トリガー用イベント(PCのみ) */
const $scope = megaMenuSec; /* .js-megaMenu_custom_sec */
function setupPullTriggerEvents(triggerSelector, targetSelector) {
if (windowPCorSP() === 'pc') {
$scope
.find(triggerSelector)
.off('mouseenter.mm-pull mouseleave.mm-pull')
.hover(
function() {
const targetCont = jQuery(this).find(targetSelector).first();
if (targetCont.length) {
targetCont.css('display', 'block');
}
},
function() {
const targetCont = jQuery(this).find(targetSelector).first();
if (targetCont.length) {
targetCont.css('display', 'none');
}
}
);
}
}
/* 1階層目トリガー */
setupPullTriggerEvents('.js-level-01-trigger', '.js-level-01-cont');
/* 2階層目トリガー */
setupPullTriggerEvents('.js-level-02-trigger', '.js-level-02-cont');
}
if (windowPCorSP() === 'pc') {
pcMegaPullDownSetUp();
}
/*------------- プルダウン用リスト(js-menu_lists)のPC処理 END -------------*/
/*------------- メガメニューPC時 END -------------*/
/*---------------------------------
メガメニューSP時 START
---------------------------------*/
/* ハンバーガーメニューボタン */
const megaMenuTriggerElement = jQuery(
'<div class="megaMenu_trigger"><span></span><span></span><span></span></div>'
);
const mainMegaMenuElement = jQuery('<nav class="main-megaMenu"><ul></ul></nav>');
function spMegaMenuSetUp() {
function getStructureKey(ul) {
const items = [];
ul.find('> li').each(function() {
const li = jQuery(this);
const anchor = li.find('> p > a, > a, a').first();
const linkText = anchor.text().trim();
const href = anchor.attr('href') || '';
const childUl = li.children('ul');
let childKey = '';
if (childUl.length) {
childKey = getStructureKey(childUl);
}
items.push(
`${linkText}::${href}${childKey ? ` (${childKey})` : ''}`
);
});
return items.join('|');
}
/* ハンバーガメニューを挿入&削除 */
if (windowPCorSP() === 'sp' && !jQuery('.megaMenu_trigger').length) { /*--- 画面幅SPのとき ---*/
/* ハンバーガーメニューボタンとメインメニューを追加 */
megaMenuTriggerElement
.appendTo('.js-megaMenu_custom_sec > .inner')
.ready(function() {
mainMegaMenuElement.insertAfter('.megaMenu_trigger');
const megaMenuTrigger = jQuery('.megaMenu_trigger');
const mainMegaMenu = jQuery('.main-megaMenu');
mainMegaMenu.css('display', 'none');
/* メインメニューにメニュー項目を追加 */
nav_menu_link.each(function() {
const parentLi = jQuery(this).parent('li');
const newText = parentLi.text().trim();
const newDataTarget = parentLi.attr('data-mega-target');
const exists = jQuery('.main-megaMenu > ul > li')
.filter(function() {
const existingText = jQuery(this).text().trim();
const existingDataTarget = jQuery(this).attr('data-mega-target');
return (
existingText === newText &&
existingDataTarget === newDataTarget
);
}).length > 0;
if (!exists) {
const clonedLi = parentLi.clone(true, true);
clonedLi.appendTo('.main-megaMenu > ul');
}
});
const level01 = jQuery('.main-megaMenu > ul > li');
level01.addClass('js-level-01');
level01.each(function(index01, element01) {
let id = jQuery(element01).attr('data-mega-target');
let section = jQuery(
`.main-megaMenu .js-megaMenu_down_list_sec[data-mega-id="${id}"]`
);
let buttonLink = section.find('.button a').clone();
let buttonLinkList = buttonLink.removeClass().addClass('wrap-item');
if (buttonLink.length) {
buttonLinkList.appendTo(jQuery(element01));
}
const addedStructureKeys = new Set();
section
.find('.component.richtext, .component.list')
.each(function(groupIndex) {
const comp = jQuery(this);
const ul = comp.find('ul').first();
if (!ul.length) return;
const groupID = `${id}-${groupIndex}`;
const structureKey = getStructureKey(ul);
if (!addedStructureKeys.has(structureKey)) {
const clonedUl = ul.clone(true, true);
clonedUl.attr('data-group-id', groupID);
clonedUl.appendTo(jQuery(element01));
addedStructureKeys.add(structureKey);
}
});
section.remove();
});
jQuery('.main-megaMenu .js-megaMenu_down_list_sec').css('display', 'none');
jQuery('.wrap-item').wrap('<li class="js-level-02">');
jQuery('.wrap-item').removeAttr('class');
level01.each(function(index, element) {
let target = jQuery(element).find('.js-level-02');
target.wrapAll('<ul class="js-level-01-cont">');
});
const level01List = jQuery('.main-megaMenu > ul > li');
const level02List = jQuery('.main-megaMenu > ul > li > ul > li');
const level03List = jQuery(
'.main-megaMenu > ul > li > ul > li > ul > li'
);
const level04List = jQuery(
'.main-megaMenu > ul > li > ul > li > ul > li > ul > li'
);
level01List.addClass('js-level-01');
level02List.addClass('js-level-02');
level03List.addClass('js-level-03');
level04List.addClass('js-level-04');
level02List.closest('ul').addClass('js-level-01-cont');
level03List.closest('ul').addClass('js-level-02-cont');
level04List.closest('ul').addClass('js-level-03-cont');
/* トグルアイコンを追加 */
jQuery('.main-megaMenu li').each(function() {
const li = jQuery(this);
const childUl = li.children('ul');
if (childUl.length) {
let anchor = li.children('a, p > a, strong > a').first();
if (!anchor.length) {
anchor = li.find('a').first();
}
if (anchor.length && anchor.find('.toggle_icon').length === 0) {
anchor.append('<span class="toggle_icon"></span>');
}
}
});
jQuery('.main-megaMenu .downMenu > a').each(function() {
if (jQuery(this).find('.toggle_icon').length === 0) {
jQuery(this).append('<span class="toggle_icon"></span>');
}
});
/* ハンバーガーメニューにクリックイベントを付与 */
megaMenuTrigger.on('click', function() {
megaMenuTrigger.toggleClass('is-active');
mainMegaMenu.toggleClass('is-active');
mainMegaMenu.toggle();
jQuery('html').toggleClass('is-locked');
megaMenuSec.toggleClass('is-open');
});
/* トグル初期化用:すべて非表示にする */
jQuery('.main-megaMenu li ul').css('display', 'none');
});
} else if (windowPCorSP() === 'pc') {
/*--- 画面幅PCのとき ---*/
jQuery('.megaMenu_trigger').removeClass('is-active');
jQuery('.main-megaMenu').removeClass('is-active');
jQuery('.megaMenu_trigger').css('display', 'none');
jQuery('.main-megaMenu ul li').css('display', 'none');
jQuery('.main-megaMenu').css('display', 'none');
jQuery('.downMenu a .toggle_icon').css('display', 'none');
jQuery('html').removeClass('is-locked');
}
}
/*------------- メガメニューSP時 END -------------*/
/*--- ウィンドウのリサイズ完了後の処理 ---*/
const windowResize = () => {
let timer = 0;
let delay = 100;
window.addEventListener(
'resize',
() => {
clearTimeout(timer);
timer = setTimeout(() => {
spMegaMenuSetUp();
}, delay);
},
false
);
};
spMegaMenuSetUp();
windowResize();
});
/*------------- メガメニューを別ページから読み込んで初期機能設定 END -------------*/
});
/* SPメニューのトグル処理(再定義) */
jQuery(document)
.off('click', '.main-megaMenu .toggle_icon')
.on('click', '.main-megaMenu .toggle_icon', function(e) {
e.preventDefault();
const li = jQuery(this).closest('li');
const targetUl = li.children('ul');
if (targetUl.length) {
targetUl.stop(true, true).slideToggle(200);
li.toggleClass('is-open');
jQuery(this).toggleClass('is-open');
}
});
/*--- ページ内遷移ずれる対策 ---*/
window.addEventListener('load', () => {
setTimeout(() => {
const headerSec = document.querySelector('.js-megaMenu_custom_sec');
if (headerSec) {
const headerHeight = headerSec.offsetHeight;
if (headerSec.classList.contains('fixed')) {
document.documentElement.style.setProperty(
'--navi-height',
`${headerHeight}px`
);
}
}
}, 300);
});
/*--- ページ内遷移ずれる対策 END ---*/
/* ---------------------------
メガメニュー(1段・プルダウンあり) END
--------------------------- */
</script>