2017-03-24 21 views
1

私はお互いに矛盾している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/

+0

ブートストラップモードですか?バージョン3? – amenadiel

+0

@amenadiel私は正直なところ、モーダルコードをどこから得たのか分からないが、それはブートストラップ3.0からのものだと思っている。 –

答えて

1

てみてください関数。

$('a[href*="#"]:not([href="#"]):not([href="#modal"])').click(function() 

はここだけでは、あなたのモーダル機能を維持する必要がありスムーズなスクロールのdivのために働くsmoothscrollを示すフィドルです。

https://jsfiddle.net/bryangators/wjgu1vL9/

+0

hmmこれは素晴らしいが、 'href ="#modal "'はidを持つモーダルを開くだろうモーダルのこのコードでは、すべてのモーダルIDを ':not '部分に追加する必要があります。 代わりに 'data-toggle =" modal "'を使って行うことはできますか? –

+0

ooh nvm。それは私の自己をテストすることができ、コードは '$( 'a [href * ="# "]:not([href ="# "]):not([data-toggle =" modal "]) ').click(function(){'。ありがとうございました:) –

+0

あなたの歓迎です。 – bryangators55

関連する問題