【问题标题】:Bootstrap modal - hide one then show another引导模式 - 隐藏一个然后显示另一个
【发布时间】:2014-09-30 16:41:00
【问题描述】:

我已经使用 jQueryUI 很长时间了,但最近出于审美原因切换到了 Bootstrap。我现在正在努力解决我认为是一个简单的问题,并想知道其他更熟悉 Bootstrap 的人是否可以帮助我。

我有一个用于即时创建对话框的通用函数,有时我会显示一个没有按钮的对话框(在处理某些内容时),然后将其交换为一个有按钮的对话框(过程完成 - 单击好的,例如)。我不想在这里定义一个设定的过程,所以我基本上是说我希望能够在需要时关闭一个对话框并打开另一个对话框。这就是问题所在。

使用 Bootstrap,对话框可以进出动画,我喜欢它并希望保留它。我不想在交换对话框时这样做。我可以通过在显示时从第一个对话框中删除类fade 来做到这一点,在它显示之前从第二个对话框中删除,效果很好。然后我将这个类添加到第二个对话框中,这样它就会动画出来。但是,当我这样做时动画会出错,并且背景 div 应该轻轻淡出的地方会出现丑陋的闪光。

我整理了一个 jsfiddle 来演示这个问题。您可以单击第一个对话框上的关闭按钮来查看淡出动画应该的样子。

在我开始深入研究 Bootstrap 源文件之前,我们将不胜感激。

http://jsfiddle.net/ArchersFiddle/0302gLav/1/

tl;dr

查看 jsfiddle - 单击“显示对话框 2” - 单击“确定”。最后想去掉黑闪。

CSS

@import url("//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css");
.modal {
    display: none;
}

HTML

<div id="dialog1" class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">Modal Dialog 1</h4>
      </div>
      <div class="modal-body">This is the first modal dialog</div>
      <div class="modal-footer">
        <button type="button" id="dialog-ok" class="btn btn-default">Show dialog 2</button>          
        <button type="button" id="dialog-close" class="btn btn-default" data-dismiss="modal">Close</button>          
      </div>
    </div>
  </div>
</div>

<div id="dialog2" class="modal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">Modal Dialog 2</h4>
      </div>
      <div class="modal-body">This is the second modal dialog</div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">OK</button>          
      </div>
    </div>
  </div>
</div>

JavaScript

function showDialog2() {
    $("#dialog1").removeClass("fade").modal("hide");
    $("#dialog2").modal("show").addClass("fade");
}

$("#dialog1").modal("show");

$("#dialog-ok").on("click", function() {
    showDialog2();
});

【问题讨论】:

    标签: javascript jquery css twitter-bootstrap modal-dialog


    【解决方案1】:
    function showDialog2() {
    $("#dialog1").removeClass("fade").modal("hide");
    $("#dialog2").addClass("fade").modal("show");
    

    }

    你想成为this

    【讨论】:

    • 谢谢,但是两个对话框之间有一个明显的动画。如果您查看我的 jsfiddle,您会发现没有。
    • 删除 'fade' 类对我有用,这个小提琴有一个错误,fade 类也必须从第二个对话框中删除,而不是添加它
    【解决方案2】:

    更新

    我为您的最后一个按钮添加了一个click() 处理程序,并添加了一个测试标识符id="test",其中对话框和背景随着fadeOut() 效果淡出。重要的是淡出包含对话框和背景的元素.modal-backdrop

    $("#test").on("click", function () {
        $(".modal-backdrop").fadeOut("slow");
    });
    

    JsFiddle

    【讨论】:

    • 谢谢,实际上并没有那么糟糕。不过,我想使用 Bootstrap 附带的内置动画来保留它。例如,in 类处理对话框的动画,将来可能会更改以允许其他选项,因此我不想硬编码当前由 Bootstrap 完成的任何内容。看这里的例子...jsfiddle.net/ArchersFiddle/fvr5Lpnn/2
    • 我喜欢你对removeClass("in"); 所做的事情,但我确实理解你是如何处理对话框的,以及为什么硬编码并不是真正的聪明选择。那是我一开始就尝试不做的事情,但是当您覆盖默认功能时,似乎会发生闪烁。
    • 正是我的问题,是的。我目前的解决方案几乎就是这个答案(使用in 类)。我试图让它尽可能通用,因为它是一个存在于 40 多个客户网站上的网络产品的一部分。每当向站点中使用的控件添加新选项时,我都会通过他们的站点管理页面向客户公开这些选项。例如,如果以后我只需要将类从in 更改为bounce,对我来说使用现有的Bootstrap 功能会简单得多。不过,我确实很感谢您的帮助:)
    • 我完全同意。我想出了一个更好的选择,但是现在它是肮脏的编码。 ;-) 我从 Bootstrap data-dismiss="modal" 重新添加了内置动画属性,并且只是淡出封装了对话框和背景的 .modal-backdrop 元素。查看更新的答案!
    • 再次非常感谢,但我仍然不高兴,因为这不会触发 Bootstrap modal 使用的任何事件处理程序,因此它不是面向未来的。我已经寻求一个完全解决我能想到的所有问题的解决方案,并将其作为答案发布。感谢您的帮助,绝对是 +1 :)
    【解决方案3】:

    好的,我不想回答我自己的问题,但我有一个 100% 万无一失的解决方案(据我所知)。我最终寻求一种解决方案来检查现有对话框并对其进行修改,而不是隐藏它并显示一个新对话框。

    这是一个有效的 jsfiddle(在通常加载 html 模板的 ajax 调用中使用 echo)...

    http://jsfiddle.net/ArchersFiddle/0302gLav/9/

    代码是我正在开发的一个更大的库的一部分,但无论如何我都会在这里发布它,因为它很可能对其他人有用。

    JavaScript 库

    (function () {
    
        var _defaultOptions = {
            backdrop: "static",
            close: true,
            keyboard: true
        };
    
        window.bootstrap = {
            modal: {
                show: function (options) {
    
                    options = $.extend(_defaultOptions, options);
    
                    var buttonText = "";
    
                    for (var key in options.buttons) {
    
                        options.buttons[key].id = "button_" + key.split(" ").join("");
    
                        var button = options.buttons[key];
    
                        buttonText += "<button type=\"button\" " +
                            "id=\"" + button.id + "\" " +
                            "class=\"btn " +
                            (typeof (button.class) == "undefined" ? "btn-default" : button.class) + "\" " +
                            (typeof (button.dismiss) == "undefined" ? "" : "data-dismiss=\"modal\" ") + ">" +
                            key + "</button>";
                    }
    
                    $.ajax({
                        url: "templates/bootstrap-modal.html"
                    })
                    .done(function (data) {
                        data = data
                            .replace("{:Title}", options.title)
                            .replace("{:Body}", options.body)
                            .replace("{:Buttons}", buttonText);
    
                        var $modal = $("#bootstrap-modal");
                        var existing = $modal.length;
    
                        if (existing) {
                            $modal.html($(data).find(".modal-dialog"));
                        }
                        else {
                            $modal = $(data).appendTo("body");
                        }
    
                        for (var key in options.buttons) {
                            var button = options.buttons[key];
                            if (typeof (button.callback) !== undefined) {
                                $("#" + button.id).on("click", button.callback);
                            }
                        }
    
                        $modal.off("shown.bs.modal").on("shown.bs.modal", function(e) {
                            if (typeof(options.onshow) === "function") {
                                options.onshow(e);
                            }
                        });
    
                        if (!existing) {
                            $modal.modal(options);
                        }
    
                        if (options.large === true) {
                            $modal.find(".modal-dialog").addClass("modal-lg");
                        }
    
                        if (options.close === false) {
                            $modal.find("button.close").remove();
                        }
                    });
                },
                hide: function (callback) {
                    var $modal = $("#bootstrap-modal");
    
                    if (!$modal.length) return;
    
                    $modal.on("hidden.bs.modal", function(e) {
                        $modal.remove();
                        if (typeof(callback) === "function") {
                            callback(e);
                        }
                    });
    
                    $modal.modal("hide");
                }
            }
        };
    })();
    

    模板 HTML

    <div id="bootstrap-modal" class="modal fade">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
            <h4 class="modal-title">{:Title}</h4>
          </div>
          <div class="modal-body">{:Body}</div>
          <div class="modal-footer">
            {:Buttons}
          </div>
        </div>
      </div>
    </div>
    

    使用示例:

    bootstrap.modal.show({
        title: "Dialog Title",
        body: "<p>This is the dialog body and can contain any old html rubbish.</p>",
        buttons: {
            Close: {
                dismiss: true
            }
        }
    });
    

    更多选项说明

    bootstrap.modal.show({
        backdrop: true,                     // show the backdrop
        close: true,                        // show the close button
        keyboard: true,                     // allow the keyboard to close the dialog
        title: "Dialog Title",
        body: "<p>This is the dialog body and can contain any old html rubbish.</p>",
        buttons: {
            Button1: {
                class: "btn-primary",           // any class you want to add to the button
                dismiss: false,                 // does this button close the dialog?
                callback: function() {          // button click handler
                    // the button was clicked - do something here
                }
            },
            Button2: {
                // options as defined as above.  You can have as many buttons as you want
            }
        },
        onshow: function(e) {
            // this will trigger when the dialog has completed the show animation
        }
    });
    

    bootstrap.modal.hide(function(e) {
        // this will trigger when the dialog has completed the hide animation
    });
    

    show() 方法中的所有选项都是可选的,但显然您需要标题和正文。

    【讨论】:

      【解决方案4】:

      我已经编写了如何在打开另一个模态之前关闭打开的模态的代码。

      $('[data-toggle="modal"]').on('click', function() { //On click a button which call a modal
          if(($(".modal").data('bs.modal') || {}).isShown){ //If a modal is shown
              var modal = $(".modal.in"); // Get the current element
              $(modal).modal('hide'); // Hide the current modal
          }
      });
      

      希望有所帮助!

      【讨论】:

      • 谢谢,但没有。如果您再次阅读该问题,您会发现我想摆脱关闭一个对话框和打开另一个对话框之间的动画。修改现有对话框是最精确的方法。
      【解决方案5】:

      有点晚了,但可能会对遇到同样问题的人有所帮助。

      在显示新模式时删除 fade 类也将显示没有 fade 类的背景。

      为了干净的过渡,隐藏当前模态并显示新模态而不淡入淡出,但在关闭时淡出:

      // hide the existing modal, but without fade animation
      $("#existing-modal").removeClass("fade").modal("hide");
      
      // show the new modal without fade animation, but enable the fade animation for later
      $("#new-modal").removeClass("fade").modal("show").addClass("fade");
      
      // enable fade animation of the backdrop, that was removed for the new modal
      $(".modal-backdrop.in").addClass("fade");
      

      (使用最新的 Bootstrap 版本,使用$(".modal-backdrop.show").addClass("fade")),

      修复示例:http://jsfiddle.net/bvalentino/f82z1wex/2/

      【讨论】:

        猜你喜欢
        • 2019-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-20
        • 1970-01-01
        • 2022-11-15
        相关资源
        最近更新 更多