###前提・実現したいこと
html
1<a href="#modal01">モーダル1を開く</a> 2<a href="#modal02">モーダル2を開く</a> 3<a href="#modal03">モーダル3を開く</a> 4 5<div id="modal01"> 6 <div class="image"><img src="images/modal01.png" alt=""></div> 7 <a href="#modal03" class="modal-prev">前へ</a> 8 <a href="#modal02" class="modal-next">次へ</a> <a href="" class="modal-close hover">閉じる</a> 9</div> 10 11<div id="modal02"> 12 <div class="image"><img src="images/modal02.png" alt=""></div> 13 <a href="#modal01" class="modal-prev">前へ</a> 14 <a href="#modal03" class="modal-next">次へ</a> <a href="" class="modal-close hover">閉じる</a> 15</div> 16 17<div id="modal03"> 18 <div class="image"><img src="images/modal03.png" alt=""></div> 19 <a href="#modal02" class="modal-prev">前へ</a> 20 <a href="#modal01" class="modal-next">次へ</a> <a href="" class="modal-close hover">閉じる</a> 21</div>
javascript
1// モーダルの立ち上げ 2function modalOpen(target) { 3 // モーダルに必要な要素の挿入 4 $('body').css({position:"relative",overflowX:"hidden"}).append('<div id="modal-bg"></div><div id="modal"></div>'); 5 $(target).appendTo('#modal').fadeIn(); 6 $('#modal-bg').fadeIn(); 7 8 // モーダル高さが画面高さより狭いとき 9 if ($('#modal').height() <= $(window).height() ) { 10 $('#modal').css({ 11 top: $(window).scrollTop() + ($(window).height() / 2) - ($('#modal').height() / 2), 12 marginLeft: - $('#modal').width() / 2 13 }); 14 // モーダル高さが画面高さより広いとき 15 } else { 16 $('#modal').css({ 17 top: $(window).scrollTop() + 10, 18 marginLeft: - $('#modal').width() / 2 19 }); 20 } 21} 22 23// モーダルの中身切り替え 24function modalChange(target) { 25 $('#modal').children().clone().appendTo('body').fadeOut(); 26 $('#modal').html(''); 27 $(target).appendTo('#modal').fadeIn(); 28} 29 30// モーダルを閉じる 31function modalClose() { 32 $.when( 33 $('#modal').children().clone().appendTo('body').fadeOut(), 34 $('#modal-bg').remove(), 35 $('#modal').html('').remove() 36 ).done(function(){ 37 $('body').removeAttr('style'); 38 }); 39 40} 41 42$(function() { 43 // モーダルを開くとき 44 $(document).on('click', 'a.modal', function(e) { 45 e.preventDefault(); 46 var href= $(this).attr("href"); 47 var target = $(href == "#" || href == "" ? 'html' : href); 48 var position = target.offset().top; 49 $("html, body").animate({scrollTop:position}, 1000, "swing"); 50 var targetId = target.attr("id"); 51 setTimeout(function(){ 52 location.hash = targetId; 53 return false; 54 },500); 55 modalOpen(href); 56 }); 57 58 // モーダルの中身を切り替えるとき 59 $(document).on('click', 'a.modal-change', function(e) { 60 e.preventDefault(); 61 var href= $(this).attr("href"); 62 var target = $(href == "#" || href == "" ? 'html' : href); 63 var position = target.offset().top; 64 $("html, body").animate({scrollTop:position}, 1000, "swing"); 65 var targetId = target.attr("id"); 66 modalChange(href); 67 setTimeout(function(){ 68 location.hash = targetId; 69 return false; 70 },500); 71 }); 72 73 // モーダルを閉じるとき 74 $(document).on('click', '#modal-bg, a.modal-close', function(e) { 75 e.preventDefault(); 76 modalClose(); 77 location.hash = ''; 78 }); 79 80 81}); 82 83$(window).on('load',function(){ 84 // ハッシュタグ 85 if (location.hash == ""){ 86 var urlhash = location.hash; 87 console.log(urlhash); 88 modalOpen("#modal02"); 89 } 90});
このようにクリックするとモーダルが開くようにしています。
モーダルが開いた後は次へ前へボタンがあって切り替えできるようにしています。
urlで判定したいため、htmlの後に#modal01などとつけているのですが、
ついたタイミングで画面がスクロール(アンカーの位置まで動く)してしまうため
苦肉の策で、
$("html, body").animate({scrollTop:position}, 1000, "swing");
しております。
こちらをハッシュタグは付くけど、アンカーの位置まで移動させないようにする良いやり方はありますでしょうか。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/14 01:22