// 関数定義(共通パーツ読み込み) function includeParts(){ $('#header').load('/assets/include/header.html'); $('#footer').load('/assets/include/footer.html'); } // ready関数を使用してDOMガロードされ操作・解析が可能になったタイミングで実行。画像などのリソースファイル読み込み前 $(function(){ var $body = $('body'); var $window = $(window); // 共通インクルードパーツの読み込み includeParts(); // ハンバーガーメニュー $(document).on("click", ".hamburger-btn",function(){ $(this).toggleClass('is-active'); $('.header-menu').toggleClass('is-active'); }); // スムーススクロール $('a[href^="#"]').click(function(){ let windowWidth = $(window).width(); let speed = 500; let href= $(this).attr("href"); let target = $(href == "#" || href == "" ? 'html' : href); let position = target.offset().top; if(windowWidth < 768){ position -= 115; } else { position -= 140; } $("html, body").animate({scrollTop:position}, speed, "swing"); return false; }); // モーダル var windowScroll = function() { return $window.scrollTop() } var hasClassName = function($btn, classname) { return $btn.hasClass(classname); }; var fixedWindow = { stop : function() { console.log("背景ストップ"); $body.css({ position: 'fixed', top: -1 * windowScroll(), height: 'calc(100% +' + windowScroll() + 'px)' }); }, free : function() { var nowTopValue = Number($body.css('top').replace('px','')) * -1; $body.attr('style',''); $window.scrollTop(nowTopValue); } }; $('body').on('click', '.modal__btn--open' , function() { var targetModalId = '#' + $(this).data('modal'); console.log(targetModalId); $(targetModalId).addClass('-open'); $(targetModalId).css('left', -$(window).scrollLeft()); fixedWindow.stop(); }); $('.modal').click(function() { if(hasClassName($(this), '-open')) { $(this).removeClass('-open'); $(this).attr('style',''); fixedWindow.free(); } }); $(".modal__btn--close").click(function(){ $(this).parents('.modal').removeClass('-open'); $(this).parents('.modal').attr('style',''); fixedWindow.free(); }); $('.modal__content').click(function() { event.stopPropagation(); }); });