$(function() { hamburgerControl(); adjustAnchorPoint(); /*ハンバーガーメニュー*/ function hamburgerControl() { const $hamburgerButton = $('#header-hamburger-button') const targetId = $hamburgerButton.attr('aria-controls') const $target = $('#' + targetId) const $linksInTarget = $target.find('a') /*PC時 aria-hidden*/ if(window.matchMedia('(min-width: 1060px)').matches) { $target.attr('aria-hidden', 'false'); } const onEscKeyDown = (e) => { if(e.key === "Escape") { closeMenu() } } const onClickOutsideMenu = (e) => { const $t = $(e.target) if($t.closest('#' + targetId).length < 1 && $t.closest($hamburgerButton).length < 1) { e.preventDefault() closeMenu() } } const closeMenu = () => { $target.attr('aria-hidden', 'true') $hamburgerButton.attr('aria-expanded', 'false') $hamburgerButton.trigger('focus') window.removeEventListener('keydown', onEscKeyDown) document.removeEventListener('click', onClickOutsideMenu, true) } const openMenu = () => { $target.attr('aria-hidden', 'false') $hamburgerButton.attr('aria-expanded', 'true') window.addEventListener('keydown', onEscKeyDown) document.addEventListener('click', onClickOutsideMenu, true) } $hamburgerButton.on('click', (e) => { const button = e.currentTarget if(button.ariaExpanded === 'true') { closeMenu() } else { openMenu() } }) $linksInTarget.on('click', (e) => { const href = $(e.currentTarget).attr('href') if(href.startsWith('#')) { closeMenu() } }) } // smooth scroll function adjustAnchorPoint() { const $header = $('.lp202306-header') $('a[href^="#"]').on('click', function (e) { const target = $(this).attr('href'); if (!/^#.+/.test(target) || /^#top$/.test(target)) { return false } e.preventDefault() const headerHeight = $header.height(), margin = 30, headerTotalHeight = headerHeight + margin const position = $(target).offset().top - headerTotalHeight $('html, body').animate({scrollTop: position}, 550, 'swing') }); } });