------### 前提
HTML/CSS(SCSS)/jQueryで実装
jQueryでモーダルを作成。
実現したいこと
モーダルを開いたときに後ろの背景を固定して、モーダルを閉じたときに開いた場所に戻るように設定したい
Google Chrome環境でいけている。(固定されないこともある)
Safari環境では固定されない。
発生している問題・エラーメッセージ
背景が固定されず、top:0になり一番上に戻ってしまう。
該当のソースコード
jQyery
1let scrollPosition; 2scrollPosition = document.documentElement.scrollTop; 3$(".is-open").on("click",function() { 4 $(this).next(".c-modal").fadeIn(); 5 $('body').addClass('fixed').css({'top': -scrollPosition}); 6}); 7$(".is-close").on("click",function() { 8 $(".c-modal").fadeOut(); 9 $('body').removeClass('fixed').css({'top': 0}); 10 window.scrollTo( 0 , scrollPosition ); 11});
jQyery
1let scrollPosition; 2scrollPosition = document.documentElement.scrollTop; 3$(".is-open").on("click",function() { 4 $(this).next(".c-modal").fadeIn(); 5 $('body').addClass('fixed').css({'top': -scrollPosition}); 6}); 7$(".is-close").on("click",function() { 8 $(".c-modal").fadeOut(); 9 $('body').removeClass('fixed').css({'top': 0}); 10 window.scrollTo( 0 , scrollPosition ); 11});
HTML
1<button class="c-button__modal is-open">ボタン</button> 2 <div class="c-modal is-close"> 3 <div class="c-modal__text"> 4 <p>なんたらかんたら</p> 5 </div> 6</div>
CSS
1body.fixed { 2 position:fixed; 3 position: -webkit-fixed; 4 left:0; 5 width:100%; 6 height:100%; 7} 8 9html { 10 font-size: 62.5%; /* 16px * 62.5% = 10px */ 11 font-style: normal; 12 font-weight: 400; 13 line-height:1.0; 14 width: 100%; 15 } 16 body { 17 overflow: hidden; 18 z-index: -100; 19 width: 100%; 20 }
試したこと
scrollPosition = document.documentElement.scrollTop || $(window).scrollTop()
としたり、$(window).scrollTop()だけにしたり、$(’html’).scrollTop()や$(’body').scrollTop()
補足情報(FW/ツールのバージョンなど)
現状のコードで確認されたもの
Mac Chrome環境△(固定される時もある)
Mac Safari環境×
iPhone Safari環境×
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。