2016-08-29 6 views
1

私はBootstrap Modalで2つの異なることに取り組んでいます。 1つは、MVCの部分ビューからダイナミックにロードすることです。その部分は機能しているようです。今、私はModalをページの中心に置くようにしています。JavaScriptを使用してBootstrap Modalを中央に配置することができません

私はこの例で作業していますが、動作させるようには見えません。 https://www.abeautifulsite.net/vertically-centering-bootstrap-modals

JavaScriptのダイアログIDの名前を ".modal-container"に変更して、私のモーダルを見つけるようにしました。それ以外は何も変わっていない。私はF12 Dev Toolsを見てきましたが、何のエラーもありません。

Index.cshtml

@{ 
    Layout = null; 
} 

<!DOCTYPE html> 

<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>Index</title> 
    <link href="~/Content/Site.css" rel="stylesheet" /> 
</head> 
<body> 
    <div> 
     <div class="container"> 
      @Html.ActionLink("Test Modal ", "TestModal", "Test", null, new { @class = "modal-link btn btn-warning" }) 
     </div> 
    </div> 

    <div id="modal-container" class="modal fade" role="dialog"> 
     <div class="modal-content"> 
     </div> 
    </div> 

    <script src="~/Scripts/jquery-1.10.2.min.js"></script> 
    <script src="~/Scripts/bootstrap.min.js"></script> 
    <script src="~/Scripts/modernizr-2.6.2.js"></script> 

    <script type="text/javascript"> 

     //**** Bootstrap Modal - used with Partial Views *********************** 
     $(function() { 
      // Attach modal-container bootstrap attributes to links with .modal-link class. 
      // when a link is clicked with these attributes, bootstrap will display the href content in a modal dialog. 
      $('body').on('click', '.modal-link', function (e) { 
       e.preventDefault(); 
       $(this).attr('data-target', '#modal-container'); 
       $(this).attr('data-toggle', 'modal'); 
      }); 

      // Attach listener to .modal-close-btn's so that when the button is pressed the modal dialog disappears 
      $('body').on('click', '.modal-close-btn', function() { 
       $('#modal-container').modal('hide'); 
      }); 

      //clear modal cache, so that new content can be loaded 
      $('#modal-container').on('hidden.bs.modal', function() { 
       $(this).removeData('bs.modal'); 
      }); 

      $('#CancelModal').on('click', function() { 
       return false; 
      }); 
     }); 

     /** 
     * Vertically center Bootstrap 3 modals so they aren't always stuck at the top 
     */ 
     $(function() { 

      function reposition() { 

       var modal = $(this), 
        dialog = modal.find('.modal-container'); 

       modal.css('display', 'block'); 

       // Dividing by two centers the modal exactly, but dividing by three 
       // or four works better for larger screens. 
       dialog.css("margin-top", Math.max(0, ($(window).height() - dialog.height())/2)); 

      } 

      // Reposition when a modal is shown 
      $('.modal').on('show.bs.modal', reposition); 

      // Reposition when the window is resized 
      $(window).on('resize', function() { 
       $('.modal:visible').each(reposition); 
      }); 

     }); 
    </script> 

</body> 
</html> 

コントローラ

public class TestController : Controller 
    { 
     // GET: Test 
     public ActionResult Index() 
     { 
      return View(); 
     } 

     public ActionResult TestModal() 
     { 
      return PartialView("_TestModal"); 
     } 
    } 

_TestModal.cshtml

<div class="modal-body"> 
    <h4> 
     Are you sure you want to delete this cost center from all clocks in the group? 
    </h4> 

    @using (Html.BeginForm("Lyubomir", "Home", FormMethod.Post)) 
    { 
     <div class="row"> 
      &nbsp; 
     </div> 
     <div class="row"> 
      <div class="col-md-4 col-md-offset-4"> 
       <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> 
       <button type="submit" id="approve-btn" class="btn btn-danger">Save</button> 
      </div> 
     </div> 
    } 
</div> 


<script type="text/javascript"> 
    $(function() { 
     $('#approve-btn').click(function() { 
      $('#modal-container').modal('hide'); 
     }); 
    }); 
</script> 

編集:私は、モーダルがちょうど一番上に表示されていることを最初の記事で言及している必要があります100%の幅になります。

enter image description here

+0

センターを?または水平に? (それは水平に自動的に中心を置くべきです....)また、垂直の場合は、ブラウザのビューポートを意味しますか? –

+0

申し訳ありませんが、それを明確にしたはずです。私は自分の投稿を更新しました。 – Caverman

答えて

2

まあ、私はそれを見て、(私があることを信じている)あなたのコードではかなり単純なミスに気づいた:

dialog = modal.find('.modal-container'); 

はmodal-」のCLASSとの任意の要素を見つけますコンテナ'。

ただし、コードスニペットでは、divのIDはCLASSではなく「modal-container」に設定されています。

上記のコード行は、必要なdivの位置を特定していません。実際、私はそれが何らかの要素を見つけているとは思わない( 'モーダルコンテナ'のクラスを持たないので)。

そのため、おそらくのようなもののように変更します。

<div class="modal fade modal-container" role="dialog"> 
    <div class="modal-content"> 
    </div> 
</div> 

ORような何かを言うために)同じHTMLを残し、(modal.findを変更:

dialog = modal.find('#modal-container'); 

が私を知ってみましょうそれが助けば。


編集:あなたのブートストラップモーダル構造がオフになっているので、

あなたのモーダルは全幅です。あなたは部門を欠いています。

これはあなたが持っているものである:ここ

<div id="modal-container" class="modal fade" role="dialog"> 
    <div class="modal-content"> 
    </div> 
</div> 

は(とあなたの問題を解決する必要があります)何が必要です:

「モーダルのクラスとdiv要素を追加すること
<div id="modal-container" class="modal fade" role="dialog"> 
    <div class="modal-dialog"> 
    <div class="modal-content"> 
    </div> 
    </div> 

-dialog 'が役立つはずです。

ああ、とにmodal.find()変更:縦....

dialog = modal.find('.modal-dialog'); 
+0

私は問題になると確信していましたが、それは間違いなく問題の一部です。私はmodal.find( '#modal-container')に変更し、DIVのIDを同じままにしました。残念ながら私は何の変化も見ませんでした。 devツールにもエラーはありません。私はブラウザを閉じた/再オープンし、ちょうど安全な側になるためにTemp Internet Filesをクリアしました。 – Caverman

+0

私は何かがModalを上書きしているのだろうかと思います。私の理解から、モーダルの幅は100%であってはなりません。私はデフォルトの動作は、ブラウザの上部には、100%の幅ではなく、モーダルを置くことだと思う。 – Caverman

+0

私はあなたがなぜこれらの問題を抱えているのかを理解しました。また、ブートストラップモーダルに存在するdiv要素全体が欠落しています。投稿の最新の編集内容を(下に)チェックして、私に連絡してください。 –

関連する問題