简体   繁体   English

在背压时关闭引导模式

[英]Dismiss bootstrap modal on back press

I have a meteor app for both web and mobile platforms.我有一个适用于网络和移动平台的流星应用程序。

I have a bootstrap modal in it, and I need the modal to be dismissed whenever a user presses the browser's back button (in web app), or device's back button (in mobile app).我有一个引导模式,当用户按下浏览器的后退按钮(在网络应用程序中)或设备的后退按钮(在移动应用程序中)时,我需要关闭模式。

Currently, when I press (browser/device) back button, the modal disappears without any animation, the modal's faded backdrop is still displayed, and the user is taken to the previous page.目前,当我按下(浏览器/设备)后退按钮时,模态消失而没有任何动画,模态的褪色背景仍然显示,用户被带到上一页。

What I want is that when the modal is open, the modal (along with the backdrop) should dismiss, with animation, and the user should remain on the current page.我想要的是,当模态打开时,模态(连同背景)应该关闭,带有动画,并且用户应该留在当前页面上。

Here's my relevant code:这是我的相关代码:

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

...

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

    $('.modal').modal('hide');
}

Thanks :)谢谢 :)

Update更新

Using the following code in android dismisses the modal correctly, and stays on the same page.在 android 中使用以下代码正确关闭模态,并保持在同一页面上。 But now, it never ever allows the back press event to propagate.但是现在,它永远不会允许 back press 事件传播。

document.addEventListener("backbutton", this.handleBackPress);
...

handleBackPress(event) {
    $('.modal').modal('hide');
}

in your function, add $('.modal-backdrop').remove();在您的函数中,添加 $('.modal-backdrop').remove();

handleBackPress(event) {
  event.preventDefault();
  event.stopPropagation();
  $('.modal').modal('hide');
  $('.modal-backdrop').remove();
}

As for the fade effect, your modal should have a fade class attached to it: class="modal fade"至于淡入淡出效果,你的模态应该有一个淡入淡出的类: class="modalfade"

Try this :尝试这个 :

 $('#modalid').modal('toggle');

Your code will be like this:您的代码将是这样的:

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

   $('.modal').modal('toggle');
}

Try this out :试试这个:

    $('#backbutton').click(function() {
    $('.modal').modal('hide');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM