How to open a Bootstrap modal without jQuery or bootstrap.js javascript?

前端 未结 3 2103
我寻月下人不归
我寻月下人不归 2021-02-19 06:47

I\'m working on a VERY LIGHT survey application. This application runs in third world countries in locations with very limited connection.

We found that the loading-tim

3条回答
  •  我在风中等你
    2021-02-19 06:49

    @uday's link to CSS only modal is a nice trick, but might be awkward to use if you use #tag's for other purposes (eg, Routing & param passing).

    So here is an example that uses very little JS to achieve something very similar. I've tried to keep the Snippet as small as possible so that it's easy to see what's happening.

    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;
    }
    
    
    

提交回复
热议问题