Bootstrap模态 - 隐藏一个然后显示另一个

Arc*_*her 6 javascript css jquery modal-dialog twitter-bootstrap

我已经使用jQueryUI很长一段时间了,但最近由于审美原因转而使用Bootstrap.我现在正在努力解决我希望成为一个简单的问题,并想知道是否有其他更熟悉Bootstrap的人可以帮助我.

我有一个用于动态创建对话框的通用函数,有时候我会显示一个没有按钮的对话框(当处理某些东西时),然后将它交换到一个有按钮的对话框(进程完成 - 单击OK,for例).我不打算在这里定义一个集合过程,所以我基本上说我希望能够关闭一个对话框并在需要时打开另一个对话框.这就是问题所在.

使用Bootstrap,对话框可以进行动画制作,我喜欢它,并希望保留它.我不想在交换对话框时这样做.我可以通过fade从显示的第一个对话框中删除该类,并从显示之前的第二个对话框中删除该类来完成此操作,并且效果很好.然后我将该类添加到第二个对话框,以便它将动画显示出来.然而,当我这样做时动画出错了,并且有一个丑陋的闪光,背景div应该轻轻淡出.

我已经把一个jsfiddle放在一起来证明这个问题.您可以单击第一个对话框上的关闭按钮以查看淡出动画应该是什么样子.

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

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

TL;博士

查看jsfiddle - 单击"显示对话框2" - 单击"确定".我想在最后摆脱黑色闪光.

CSS

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

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>
Run Code Online (Sandbox Code Playgroud)

JavaScript的

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

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

$("#dialog-ok").on("click", function() {
    showDialog2();
});
Run Code Online (Sandbox Code Playgroud)

Arc*_*her 3

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

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

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");
            }
        }
    };
})();
Run Code Online (Sandbox Code Playgroud)

模板 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>
Run Code Online (Sandbox Code Playgroud)

用法示例:

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
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

更多选项解释

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
    }
});
Run Code Online (Sandbox Code Playgroud)

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

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