如何在没有 jQuery 或 bootstrap.js javascript 的情况下打开 Bootstrap 模式?

新手上路,请多包涵

我正在开发一个非常简单的调查应用程序。该应用程序在连接非常有限的第三世界国家/地区运行。

我们发现加载时间与用户参与度成正比(对我们来说非常重要)。

今天我使用了 2 个库——VueJS 和一个自定义引导程序构建。我想调用一个模态。但是模式需要添加 Bootstrap Javascript 和 jQuery。这些库几乎使加载时间加倍。

如何在不添加这两个库的情况下打开模式?

原文由 Daniel Santos 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 246
2 个回答

@uday 链接到 CSS only modal 是一个很好的技巧,但如果您将#tag 用于其他目的(例如,路由和参数传递),则使用起来可能会很尴尬。

所以这是一个使用很少的 JS 来实现非常相似的东西的例子。我试图让代码段尽可能小,以便很容易看到发生了什么。

 var modal = document.querySelector(".modal");
var container = modal.querySelector(".container");

document.querySelector("button").addEventListener("click", function (e) {
  modal.classList.remove("hidden")
});

document.querySelector(".modal").addEventListener("click", function (e) {
  if (e.target !== modal && e.target !== container) return;
  modal.classList.add("hidden");
});
 .modal {
  background-color: rgba(0,0,0,0.4); /* Transparent dimmed overlay */
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: table;
}

.modal.hidden {
  display: none;
}

.modal .container {
 display: table-cell;
 text-align: center;
 vertical-align: middle;
 width: 200px;
}

.modal .body {
  box-shadow: 5px 10px #888888;
  display: inline-block;
  background-color: white;
  border: 1px solid black;
  padding: 10px;
}
 <button>Show Modal</button>

<div class="modal hidden">
  <div class="container">
    <div class="body">
      <p>Click outside this box to close the modal.<p>
      <p>You could of course add a close button etc</p>
      <p>But this is left for the OP todo</p>
    </div>
  </div>
</div>

原文由 Keith 发布,翻译遵循 CC BY-SA 4.0 许可协议

你不需要任何 css 样式。你应该在你的 HTML 中创建引导模式,然后在你的 js 中,你必须根据以下描述简单地给它一些样式:

 var locModal = document.getElementById('locModal');
var btnclose = document.getElementById('w-change-close');
var btnShow= document.getElementById('w-change-location');

//show the modal
btnShow.addEventListener('click', (e) => {
    locModal.style.display = "block";
    locModal.style.paddingRight = "17px";
    locModal.className="modal fade show";
});
    //hide the modal
    btnclose.addEventListener('click', (e) => {
        locModal.style.display = "none";
        locModal.className="modal fade";
});


HTML 应该是这样的:

 <!-- Button trigger modal -->
<button id="w-change-location" type="button" class="btn btn-primary mt-3" data-toggle="modal" data-target="#locModal">
                Change Location
   </button>
    <!-- Modal -->
    <div class="modal fade" id="locModal" tabindex="-1" role="dialog" aria-labelledby="locModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="locModalLabel">Choose Location</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form action="" id="w-form">
                    <div class="form-group">
                        <label for="city"> City</label>
                        <input type="text" class="form-control" id="city">
                    </div>
                    <div class="form-group">
                        <label for="state"> State </label>
                        <input type="text" class="form-control" id="state">
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button id="w-change-close" type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button id="w-change-btn" type="button" class="btn btn-primary">Save changes</button>
            </div>
        </div>
    </div>
</div>

原文由 Mehdi bayat 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题