【问题标题】:Bootstrap 4 Modal Open/close causes image position changingBootstrap 4 模态打开/关闭导致图像位置改变
【发布时间】:2020-01-21 20:19:39
【问题描述】:

我有以下简单的代码 sn-p 通过一个按钮打开一个 bs4 模式。

该按钮通过悬停容器出现。

问题是通过按钮打开模态后,然后关闭。图像意外地向上移动。图像的位置可以通过再次悬停容器来恢复。我只是想知道为什么会发生这种行为以及如何解决它。

https://jsfiddle.net/hb6n71zg/1/代码sn-p链接。

.container {
  position: relative;
  overflow: hidden;
}

.p-img {
  opacity: 1;
  width: 100%;
  transition: 0.5s ease;
}

.p-capt {
  background: white;
  width: 100%;
  transition: 0.5s ease;
  opacity: 0;
  position: absolute;
  bottom: -10%;
  text-align: center;
}

.container:hover .p-img {
  opacity: 0.3;
}

.container:hover .p-capt {
  opacity: 1;
  bottom: 0;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<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>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>


<h2>TOPTOPTOP</h2>
<p>TOPTOPTOPTOPTOP</p>

<div class="container">
  <img class="p-img w-100" src="https://www.wired.com/wp-content/uploads/2015/09/google-logo.jpg" />
  <div class="p-capt">
  <button type="button" class="btn btn-primary w-100" data-toggle="modal" data-target="#myModal">launch modal</button>
  </div>
</div>


<!-- Modal -->
<div id="myModal" class="modal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p>Modal body text goes here.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary">Save changes</button>
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

更新:

如果我通过 javascript 触发模式,就不会发生这种奇怪的行为。

<button type="button" class="btn btn-primary w-100">launch modal</button>

$(function(){
    $("button").click(function() {
    $("#myModal").modal('toggle');
  });
});

【问题讨论】:

    标签: css twitter-bootstrap bootstrap-4 modal-dialog


    【解决方案1】:

    这与三种不同条件之间非常奇怪的组合有关:

    • .container 拥有overflow: hidden;
    • .container 内容溢出
    • .container 不是 :hovered,除非您在模态关闭时移动鼠标。

    解决方法是把.container中的overflow:hidden去掉,使用按钮自己的容器(.p-cont)获取按钮的按钮剪辑效果:

    .container {
      position: relative;
    }
    
    .p-img {
      opacity: 1;
      width: 100%;
      transition: 0.5s ease;
      height: auto;
    }
    
    .p-capt {
      background: white;
      width: 100%;
      transition: 0.5s ease;
      opacity: 0;
      position: absolute;
      text-align: center;
      padding-left: 15px;
      padding-right: 15px;
      overflow: hidden;
      bottom: 0;
      height: 0;
    }
    
    .p-capt .btn-primary {
      position: relative;
      left: -15px;
    }
    
    .container:hover .p-img {
      opacity: 0.3;
    }
    
    .container:hover .p-capt {
      opacity: 1;
      height: 38px;
    }
    
    
    /* this removes the button's outline when active, as IMHO it didn't look right (it was cut off by the overflow:hidden. Totatlly optional, of course */
    
    .p-capt .btn-primary:not(:disabled):not(.disabled).active:focus,
    .p-capt .btn-primary:not(:disabled):not(.disabled):active:focus,
    .p-capt .show>.btn-primary.dropdown-toggle:focus,
    .p-capt .btn-primary.focus,
    .p-capt .btn-primary:focus {
      box-shadow: none;
    }
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
    
    <h2>TOPTOPTOP</h2>
    <p>TOPTOPTOPTOPTOP</p>
    
    <div class="container">
      <div class="row">
        <div class="col">
          <img class="p-img d-block w-100 h-auto" src="https://www.wired.com/wp-content/uploads/2015/09/google-logo.jpg" />
          <div class="p-capt">
            <button type="button" class="btn btn-primary btn-block" data-toggle="modal" data-target="#myModal">launch modal</button>
          </div>
        </div>
      </div>
    </div>
    <!-- Modal -->
    <div id="myModal" class="modal" tabindex="-1" role="dialog">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title">Modal title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <p>Modal body text goes here.</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-primary">Save changes</button>
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          </div>
        </div>
      </div>
    </div>

    注意:我还使用了 Bootstrap v4.4.1 而不是您的 v4.0.0,但这不会影响示例。我只是想确保这不是原因,并没有费心将其改回来。

    【讨论】:

    • 我尝试了您的解决方案,但小宽度屏幕似乎存在一些问题。滚动条一直在打开或关闭之间切换,否则一切都很好。我将选择简单的 JS 选项,因此我不必担心其他兼容性问题(大小/浏览器方面),而且我最终必须使用 JS。但是您的解决方案肯定会提供一些想法。我唯一不确定这个问题是预期的行为还是我错过了使用 BS 功能的正确方法。以后可能会在 Github 页面中打开一个问题。
    • @Yunhai,滚动条的开/关是正常的 Bootstrap 行为。当打开模态时,bootstrap 会向 body 添加一个应用 overflow:hidden 的类,当模态关闭时它会被删除。这样一来,模态内部的滚动不会滚动模态下的页面。它与您当前的问题完全无关(除了它们都是关于 Bootstrap 的)。
    • 另请注意,建议.container 的直接子代为.rows,.rows 的直接子代为.col-*-*s。我更新了这个例子。我还从body 中删除了min-height,这不是答案所必需的,这只是我在试图弄清楚如何解决您的问题并忘记删除时尝试的东西。
    • 我的意思是滚动条在特定宽度下悬停时打开/关闭,您的更新解决了这个问题。我确实知道.row.col.container等之间的一般BS结构。这就是为什么我对这个案例关于我原来的问题感到有点奇怪的原因。
    猜你喜欢
    • 2019-01-09
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 2021-05-22
    相关资源
    最近更新 更多