【问题标题】:Make back button on phone close bootstrap modal在手机关闭引导模式上设置后退按钮
【发布时间】:2016-04-12 07:58:08
【问题描述】:

我有一个从 Bootstrap 示例代码中获取的简单模式。是这样的:

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

我希望能够在手机上通过返回按钮关闭模态框,因为它在移动设备上占据了整个页面,并且在右上角搜索“x”不是很方便。我查看了文档,但只有一个选项可以使用键盘上的 Escape 键关闭模式,而移动设备没有这样的解决方案。谁能帮帮我?

【问题讨论】:

  • 我不了解移动开发,但是如果您可以抓住后退按钮按下事件,那么您可以触发模态关闭事件。
  • 这是一个网站。我只需要这个在移动后退按钮上工作。它与按页面上的后退按钮具有相同的功能。我只是不知道如何捕捉或利用它。

标签: jquery html twitter-bootstrap


【解决方案1】:

虽然我建议使用简单的 X 或关闭按钮关闭模式,但您应该能够通过以下方式收听事件:

$(window).on("navigate", function (event, data) {
  var direction = data.state.direction;
  if (direction == 'back') {
    // do something
  }
  if (direction == 'forward') {
    // do something else
  }
});

你需要监听导航事件和 state.direction。

您是否考虑过将 X 保留在模态框的静态标题中?而且内容是可滚动的..

【讨论】:

  • 这样会更容易,我已经考虑过了,但是我为之做这个的人,希望能够使用手机上的后退按钮关闭模式
  • @MihailIvanchev - 很公平,请尝试上述方法。如果有帮助,请不要忘记投票/标记为答案。
  • 我做了以下事情:&lt;script&gt; $(window).on("navigate", function (event, data) { var direction = data.state.direction; if (direction == 'back') { $('.modal').modal('hide'); } if (direction == 'forward') { $('.modal').modal('toggle'); } }); &lt;/script&gt; 不起作用我也尝试通过 id 挂钩模式,但它仍然不起作用(例如:#myModalHorizo​​ntal 而不是 .modal)跨度>
【解决方案2】:

我使它适用于我的案例,同时实现了 https://stackoverflow.com/a/42114285/5118339https://stackoverflow.com/a/25464754/5118339,并进行了一些修复

function handleBackPress(event) {
    event.preventDefault();
    event.stopPropagation();

    $('.modal').modal('hide');
    $('.modal-backdrop').remove();
}

var closedModalHashStateId = "#modalClosed";
var openModalHashStateId = "#modalOpen";

/* Updating the hash state creates a new entry
 * in the web browser's history. The latest entry in the web browser's
 * history is "modal.html#modalClosed". */
window.location.hash = closedModalHashStateId;

$(window).on('popstate', this.handleBackPress);
document.addEventListener("backbutton", this.handleBackPress, false);

/* The latest entry in the web browser's history is now "modal.html#modalOpen".
 * The entry before this is "modal.html#modalClosed". */
$('.modal').on('show.bs.modal', function(e) {
  window.history.pushState('forward', null, './'+openModalHashStateId);
});  

/* When the user closes the modal using the Twitter Bootstrap UI, 
 * we just return to the previous entry in the web 
 * browser's history, which is "modal.html#modalClosed". This is the same thing
 * that happens when the user clicks the web browser's back button. */
$('.modal').on('hide.bs.modal', function(e) {
  window.history.back();
});  

【讨论】:

  • 如果我打开了多个模式,那么这会关闭所有模式。请进行更改以一一关闭。首先是最新的,然后是屏幕上打开的下一个,依此类推。我在这里提出了一个问题stackoverflow.com/questions/69260548/…
猜你喜欢
  • 2016-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多