【问题标题】:Bootstrap 4 modal is not displayingBootstrap 4 模式未显示
【发布时间】:2021-10-22 21:58:25
【问题描述】:

我有一个简单的设计,其中一个图像位于一个居中的 div 内。图像上方是一个带有 h1 元素的半透明文本框。我希望在单击 h1 后弹出一个 bootstrap 4 模态组件。我已经按照各种教程来构建模态,但我似乎无法让它工作。我不确定问题可能是什么。

如果有人可以看一下并给我一些指导,将不胜感激。

CodePen:https://codepen.io/dayna-j/pen/RMZdej?editors=0010

HTML:

<head>
  <link href="https://fonts.googleapis.com/css?family=Fredericka+the+Great" rel="stylesheet">
</head>
<body>

  <div class='img-div'>
    <img src="https://image.ibb.co/eZcdEn/nature_3084841_1920.jpg" alt='close up of grass'/>
    <div class='textBox-div'>      
      <h1 class = no-select data-toggle="modal" data-target="#Modal">Click Me</h1>
    </div>


      <!-- Modal (inside img-div) -->
    <div id="Modal" class="modal fade" role="dialog">
      <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
         <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
         </div>
        <div class="modal-body">
          <p>Some text in the modal.</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
        </div>
      </div>
    </div>
  </div>
</body>

CSS:

.no-select {
user-select: none;
}

html {


 box-sizing: border-box;
  background-color: black;
  // overflow:hidden;
}

*, *:before, *:after {
  box-sizing: inherit;
}

* { 
  margin: 0;
  padding: 0; 
}

body{
  background: #232526;
  // background: papayawhip;
  display: flex;
  justify-content: center;
  align-items: center;
  // border: 5px solid green;
  // border-radius: 1000px;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

.container-fluid {
  position: relative;
  height: 100%;
  width: 100%;
}

.img-div {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}

img {
  // position: relative;
  max-height: 500px;
  max-width: 850px;
  width: 100%;
  border: 1px solid grey;
  // border-radius: 1000px; 
}

.textBox-div { 
  position: absolute;
  margin: 0 auto;
  text-align: center;
  width: 100%;
  background: whitesmoke;
  opacity: .5;
}

.textBox-div h1 {
  font-family: 'Fredericka the Great', cursive;
  margin: auto 0;
  font-weight: 900;
}

@media screen and (max-width:400px)
{
  h1 {font-size: 1.5em;}
}

@media screen and (max-width:200px)
{
  h1 {font-size: 1em;}
}

JavaScript:

$( document ).ready(function() {

  $('#Modal').modal('hide');


  $('h1').on('click', function(){
    $('#Modal').modal('show');
  });

});

【问题讨论】:

  • 如果你打开浏览器控制台,你会看到一些错误——特别是,Bootstrap 需要 popper.js。只要您在控制台中仍然存在错误,您的 JS 就不太可能按预期工作,因此如果您能解决这些问题,那么问题所在/在哪里就会更清楚。
  • 问题不在于 Bootstrap 的 beta 版本似乎没有 .modal 功能。您可以使用稳定的maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js

标签: css twitter-bootstrap responsive-design bootstrap-4


【解决方案1】:

代码有几个问题...

  • 您需要 popper.js,它不包含在 Codepen 中。
  • 同时拥有data-toggle="modal"$('#Modal').modal('show') 的JS 代码相互抵消,这是阻止模态显示
  • 在 CSS (html{}) 中使用 // 不是正确的注释。

使用data-toggle="modal" $('#Modal').modal('show'),但不能同时使用。

https://www.codeply.com/go/zcHpRWcMA5


链接到popper.min.jsbootstrap.min.css替代方法是使用bootstrap.bundle.min.js 结合两者..

CDN:https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.bundle.min.js

【讨论】:

    【解决方案2】:

    在您的head 中链接popper.js

    通过 CDN:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    

    (来自here

    同时删除您的 JS 脚本以显示和隐藏模式。你不需要它,因为 bootstrap 已经为你做了。

    【讨论】:

      【解决方案3】:
      var myModal = new bootstrap.Modal(document.getElementById('Your model id'))
      myModal.show();
      

      只有这段代码。

      【讨论】:

      猜你喜欢
      • 2021-03-19
      • 2017-09-20
      • 2018-08-04
      • 1970-01-01
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      • 2018-11-09
      • 2018-08-05
      相关资源
      最近更新 更多