私はお互いに矛盾している2つのjavascriptファイルを持っています。 1つはモーダルを開き、<a href="#modal" data-toggle="modal">open modal</a>
のようなリンクを使用し、id="modal"
でモーダルを開きます。しかし、他のスクリプトはスムーズなスクロールのためのもので、アンカーをURL (I'd like to keep that part!)
から削除しますが、円滑なスクロールスクリプトを追加した後はモーダルが機能しません。私はそれを修正することができますどのようなアイデア?JavaScriptの機能を分離する最も良い方法は何ですか?
modal.js:
$(".modal-wide").on("show.modal", function() {
var height = $(window).height() - 200;
$(this).find(".modal-body").css("max-height", height);
});
$('a[data-toggle="modal"]').bind('click',function(){
$('.modal').modal('hide');
});
scroll.js:
$(function() {
$('a[href*="#"]:not([href="#"])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html, body').animate({
scrollTop: target.offset().top
}, 500);
return false;
}
}
});
});
scroll.jsソース:スムーズスクロールをセレクタないように、あなたの特定のhrefタグを追加https://css-tricks.com/snippets/jquery/smooth-scrolling/
ブートストラップモードですか?バージョン3? – amenadiel
@amenadiel私は正直なところ、モーダルコードをどこから得たのか分からないが、それはブートストラップ3.0からのものだと思っている。 –