如何使用 Bootstrap 重置弹出模式的位置?

Igo*_*gor 4 javascript bootstrap-modal

我正在寻找解决我的问题的方法,我看到了很多不同的方法来做到这一点,但没有一个对我有用。我将把我的代码粘贴到这里,看看你是否能以某种方式帮助我。我有一个可拖动的弹出窗口来显示注释。主屏幕上有一个项目列表,每次用户单击“查看”链接时,都会打开弹出窗口,其中包含该特定项目的注释。一切正常。弹出窗口将打开并显示正确的信息,我可以在屏幕上移动弹出窗口。那么我遇到的唯一问题是:一旦我关闭弹出窗口并打开一个新的弹出窗口,它不会将弹出窗口重置到原始位置,而是准确地打开我离开另一个弹出窗口的位置。当用户关闭弹出窗口时,我需要重置弹出窗口的位置。

这是我的js:

require(['jquery'
    , 'bootstrap'
    , 'datepicker'
    , 'typeahead'
    , 'combobox'
    , 'tagsinput'
], function($){

    // DOM ready
    $(function(){
        $('.modal-dialog').draggable();

        $('#noteModal').on('show.bs.modal', function(e) {

            //get data-id attribute of the clicked element
            var note = $(e.relatedTarget).data('note');
            //populate the textbox
            $(e.currentTarget).find('span[name="note"]').text(note);
        });



    });
});
Run Code Online (Sandbox Code Playgroud)

这是我的 html 页面上的模式:

<!-- Modal to display the Note popup -->
<div class="modal" id="noteModal" tabindex="-1" role="dialog" aria-labelledby="noteModalLabel">
    <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="noteModalLabel">Note</h4>
            </div>
            <div class="modal-body">
                <span name="note" id="note"></span>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

每次用户关闭弹出窗口时如何重置弹出窗口的位置?

谢谢你!

Dan*_*ger 5

在点击处理程序中,您可以使用 JQuery 设置模式的 css,如下所示:

if (!$(".modal.in").length) {
  $(".modal-dialog").css({
    top: 0,
    left: 0
  });
}
Run Code Online (Sandbox Code Playgroud)

这将重置模态框的位置。假设您使用 JS 打开模态框,您可以在 JavaScript 中调用模态框之前使用它。

尝试运行下面的代码片段或查看此CodePen 演示,了解使用您的模式的示例。

if (!$(".modal.in").length) {
  $(".modal-dialog").css({
    top: 0,
    left: 0
  });
}
Run Code Online (Sandbox Code Playgroud)
// DOM ready
$(function() {
  $(".modal-dialog").draggable();
  $("#btn1").click(function() {
    // reset modal if it isn't visible
    if (!$(".modal.in").length) {
      $(".modal-dialog").css({
        top: 0,
        left: 0
      });
    }
    $("#noteModal").modal({
      backdrop: false,
      show: true
    });
  });

  $("#noteModal").on("show.bs.modal", function(e) {
    var note = $('#btn1').data('note');

    $(e.currentTarget).find('span[name="note"]').html(note);
  });
});
Run Code Online (Sandbox Code Playgroud)


如果您想在模式打开时保持背景可用,我不久前在这里发布了一个解决方案。

我希望这有帮助!