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

Dan*_*tos 5 javascript bootstrap-4

我正在研究一个非常轻松的调查应用程序.此应用程序在连接非常有限的位置在第三世界国家/地区运行.

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

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

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

小智 7

您不需要任何 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";
});
Run Code Online (Sandbox Code Playgroud)

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


Kei*_*ith 6

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

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

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