简体   繁体   中英

How to create multiple modal in jquery?

I am new in Jquery. I have a little jquery modal code for showing modal. But this modal only can use for one code. I mean, I cant create two modal from this code. For example;

Modal01 Modal02

Even if I create 2 modal, it's behave abnormal. I saw taht, data-id or any id or any atribute use for creating multiple modal. Where two part. Button contains a unique id or name and same name or id use for targeting modal.

Look at my code please:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<style>
.mi-modal {
    position: fixed;
    z-index: 10000; /* 1 */
    top: 0;
    left: 0;
    visibility: hidden;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
}
.mi-modal.modal-visible {
    visibility: visible;
}
.mi-modal-overlay {
  position: fixed;
  z-index: 10;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: hsla(0, 0%, 0%, 0.4);
  visibility: hidden;
  opacity: 0;
  transition: visibility 0s linear 0.3s, opacity 0.3s;
  box-sizing: border-box;  
}
.mi-modal.modal-visible .mi-modal-overlay {
  opacity: 1;
  visibility: visible;
  transition-delay: 0s;
}
.mi-modal-wrapper {
  position: absolute;
  z-index: 9999;
  top: 6em;
  left: 50%;
  width: 32em;
  margin-left: -16em;
  background-color: #fff;
  box-shadow: 0 0 1.5em hsla(0, 0%, 0%, 0.35);
  box-sizing: border-box;  
}
.mi-modal-transition {
  transition: all 0.4s;
  transform: translateY(-10%);
  opacity: 0;
}
.mi-modal.modal-visible .mi-modal-transition {
  transform: translateY(0);
  opacity: 1;
}
.mi-modal-header,
.mi-modal-content {
  padding: 1em;
}
.mi-modal-header {
  position: relative;
  background-color: #fff;
  box-shadow: 0 1px 2px hsla(0, 0%, 0%, 0.06);
  border-bottom: 1px solid #e8e8e8;
}
.mi-modal-close {
  position: absolute;
  top: 0;
  right: 0;
  padding: 1em;
  color: #aaa;
  background: none;
  border: 0;
  font-size: 18px;
}
.mi-modal-close:hover {
  color: #777;
}
.mi-modal-heading {
  font-size: 1.125em;
  margin: 0;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
.mi-modal-content > *:first-child {
  margin-top: 0;
}
.mi-modal-content > *:last-child {
  margin-bottom: 0;
}
.mi-modal.modal-scroll .mi-modal-content{
max-height: 60vh;
overflow-y: scroll;
}
.mi-modal.modal-scroll .mi-modal-wrapper {
  position: absolute;
  z-index: 9999;
  top: 2em;
  left: 50%;
  width: 32em;
  margin-left: -16em;
  background-color: #CDf;
  box-shadow: 0 0 1.5em hsla(0, 0%, 0%, 0.35);
  box-sizing: border-box;  
}
</style>


<button class="mi-modal-toggle">Show modal</button>
  <div class="mi-modal">
    <div class="mi-modal-overlay mi-modal-toggle"></div>
    <div class="mi-modal-wrapper mi-modal-transition">
      <div class="mi-modal-header">
        <button class="mi-modal-close mi-modal-toggle">&times;</button>
        <h2 class="mi-modal-heading">This is a modal</h2>
      </div>
      <div class="mi-modal-body">
        <div class="mi-modal-content">
          <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Impedit eum delectus, libero, accusantium dolores inventore obcaecati placeat cum sapiente vel laboriosam similique totam id ducimus aperiam, ratione fuga blanditiis maiores.</p>
          <button class="mi-btn btn-danger mi-ripple mi-ripple-light mi-modal-toggle">No</button>
          <button class="mi-btn btn-info mi-ripple mi-ripple-light">Confirm</button>
        </div>
      </div>
    </div>
  </div>





<script>
$('.mi-modal-toggle').on('click', function(e) {
  e.preventDefault();
  $('.mi-modal').toggleClass('modal-visible');
});
</script>

How can I do it.......?

There's many options; but the main principle is that you need separate HTML for each modal, which you don't have. You can have a single modal "frame" and then fill it with HTML, eg from a partial-view rendered server-side via an ajax call - but without that, and to start with, make sure you have two lots of mi-modal structure.

Then just give each one either a unique id or a unique class, with code for each one opening the related dialog. HTML element class= doesn't need to be limited to css/styles. eg

$('.mi-modal-toggle1').on('click', function(e) {
  $('.mi-modal1').toggleClass('modal-visible');
});
$('.mi-modal-toggle2').on('click', function(e) {
  $('.mi-modal2').toggleClass('modal-visible');
});

Start with

  • 2 sets of HTML for the modal
  • explicit buttons for each modal

Snippet:

 // common close button $('.mi-modal-toggle').click(function() { $(this).closest(".mi-modal").toggleClass('modal-visible'); }); // explicit button per modal $('.mi-modal-toggle1').on('click', function(e) { $('.mi-modal1').toggleClass('modal-visible'); }); $('.mi-modal-toggle2').on('click', function(e) { $('.mi-modal2').toggleClass('modal-visible'); });
 .mi-modal { position: fixed; z-index: 10000; /* 1 */ top: 0; left: 0; visibility: hidden; width: 100%; height: 100%; box-sizing: border-box; }.mi-modal.modal-visible { visibility: visible; }.mi-modal-overlay { position: fixed; z-index: 10; top: 0; left: 0; width: 100%; height: 100%; background: hsla(0, 0%, 0%, 0.4); visibility: hidden; opacity: 0; transition: visibility 0s linear 0.3s, opacity 0.3s; box-sizing: border-box; }.mi-modal.modal-visible.mi-modal-overlay { opacity: 1; visibility: visible; transition-delay: 0s; }.mi-modal-wrapper { position: absolute; z-index: 9999; top: 6em; left: 50%; width: 32em; margin-left: -16em; background-color: #fff; box-shadow: 0 0 1.5em hsla(0, 0%, 0%, 0.35); box-sizing: border-box; }.mi-modal-transition { transition: all 0.4s; transform: translateY(-10%); opacity: 0; }.mi-modal.modal-visible.mi-modal-transition { transform: translateY(0); opacity: 1; }.mi-modal-header, .mi-modal-content { padding: 1em; }.mi-modal-header { position: relative; background-color: #fff; box-shadow: 0 1px 2px hsla(0, 0%, 0%, 0.06); border-bottom: 1px solid #e8e8e8; }.mi-modal-close { position: absolute; top: 0; right: 0; padding: 1em; color: #aaa; background: none; border: 0; font-size: 18px; }.mi-modal-close:hover { color: #777; }.mi-modal-heading { font-size: 1.125em; margin: 0; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; }.mi-modal-content > *:first-child { margin-top: 0; }.mi-modal-content > *:last-child { margin-bottom: 0; }.mi-modal.modal-scroll.mi-modal-content{ max-height: 60vh; overflow-y: scroll; }.mi-modal.modal-scroll.mi-modal-wrapper { position: absolute; z-index: 9999; top: 2em; left: 50%; width: 32em; margin-left: -16em; background-color: #CDf; box-shadow: 0 0 1.5em hsla(0, 0%, 0%, 0.35); box-sizing: border-box; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button class="mi-modal-toggle1">Show modal</button> <button class="mi-modal-toggle2">Show modal</button> <div class="mi-modal mi-modal1"> <div class="mi-modal-overlay mi-modal-toggle"></div> <div class="mi-modal-wrapper mi-modal-transition"> <div class="mi-modal-header"> <button class="mi-modal-close mi-modal-toggle">&times;</button> <h2 class="mi-modal-heading">This is a modal</h2> </div> <div class="mi-modal-body"> <div class="mi-modal-content"> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Impedit eum delectus, libero, accusantium dolores inventore obcaecati placeat cum sapiente vel laboriosam similique totam id ducimus aperiam, ratione fuga blanditiis maiores.</p> <button class="mi-btn btn-danger mi-ripple mi-ripple-light mi-modal-toggle">No</button> <button class="mi-btn btn-info mi-ripple mi-ripple-light">Confirm</button> </div> </div> </div> </div> <div class="mi-modal mi-modal2"> <div class="mi-modal-overlay mi-modal-toggle"></div> <div class="mi-modal-wrapper mi-modal-transition"> <div class="mi-modal-header"> <button class="mi-modal-close mi-modal-toggle">&times;</button> <h2 class="mi-modal-heading">This is the second modal</h2> </div> <div class="mi-modal-body"> <div class="mi-modal-content"> <p>Nothing to see here</p> <button class="mi-btn btn-danger mi-ripple mi-ripple-light mi-modal-toggle">No</button> <button class="mi-btn btn-info mi-ripple mi-ripple-light">Confirm</button> </div> </div> </div> </div>

You can then move onto data-driven code - ie one piece of code that will handle all/new modals without needing to change the code.

Give both the button and the modal html the same data- attribute and use js to locate them, eg:

<button class="mi-modal-toggle" data-modal-id="modal1">Show modal</button>
<div class="mi-modal" data-modal-id="modal1">
    $('.mi-modal-toggle').on('click', function(e) {
      var modalid = $(this).data("modal-id");
      $(`.mi-modal[data-modal-id='${modalid}']`).toggleClass('modal-visible');
    });

Now you can add as many button+modal pairs as you like, without needing to change the js.

Note: if you add buttons/HTML after the page has opened (dynamically create them) then you'll need event delegation instead, eg:

$(document).on("click", ".mi-modal-toggle", function(e) {...

but otherwise it's the same. (for purists: use the closest static html node, rather than document )

Updated snippet:

 // common close button $('.mi-modal-toggle').click(function() { $(this).closest(".mi-modal").toggleClass('modal-visible'); }); // explicit button per modal $('.mi-modal-toggle').on('click', function(e) { var modalid = $(this).data("modal-id"); $(`.mi-modal[data-modal-id='${modalid}']`).toggleClass('modal-visible'); });
 .mi-modal { position: fixed; z-index: 10000; /* 1 */ top: 0; left: 0; visibility: hidden; width: 100%; height: 100%; box-sizing: border-box; }.mi-modal.modal-visible { visibility: visible; }.mi-modal-overlay { position: fixed; z-index: 10; top: 0; left: 0; width: 100%; height: 100%; background: hsla(0, 0%, 0%, 0.4); visibility: hidden; opacity: 0; transition: visibility 0s linear 0.3s, opacity 0.3s; box-sizing: border-box; }.mi-modal.modal-visible.mi-modal-overlay { opacity: 1; visibility: visible; transition-delay: 0s; }.mi-modal-wrapper { position: absolute; z-index: 9999; top: 6em; left: 50%; width: 32em; margin-left: -16em; background-color: #fff; box-shadow: 0 0 1.5em hsla(0, 0%, 0%, 0.35); box-sizing: border-box; }.mi-modal-transition { transition: all 0.4s; transform: translateY(-10%); opacity: 0; }.mi-modal.modal-visible.mi-modal-transition { transform: translateY(0); opacity: 1; }.mi-modal-header, .mi-modal-content { padding: 1em; }.mi-modal-header { position: relative; background-color: #fff; box-shadow: 0 1px 2px hsla(0, 0%, 0%, 0.06); border-bottom: 1px solid #e8e8e8; }.mi-modal-close { position: absolute; top: 0; right: 0; padding: 1em; color: #aaa; background: none; border: 0; font-size: 18px; }.mi-modal-close:hover { color: #777; }.mi-modal-heading { font-size: 1.125em; margin: 0; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; }.mi-modal-content > *:first-child { margin-top: 0; }.mi-modal-content > *:last-child { margin-bottom: 0; }.mi-modal.modal-scroll.mi-modal-content{ max-height: 60vh; overflow-y: scroll; }.mi-modal.modal-scroll.mi-modal-wrapper { position: absolute; z-index: 9999; top: 2em; left: 50%; width: 32em; margin-left: -16em; background-color: #CDf; box-shadow: 0 0 1.5em hsla(0, 0%, 0%, 0.35); box-sizing: border-box; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button class="mi-modal-toggle" data-modal-id="modal1">Show modal</button> <button class="mi-modal-toggle" data-modal-id="secondmodal">Show modal</button> <div class="mi-modal" data-modal-id="modal1"> <div class="mi-modal-overlay mi-modal-toggle"></div> <div class="mi-modal-wrapper mi-modal-transition"> <div class="mi-modal-header"> <button class="mi-modal-close mi-modal-toggle">&times;</button> <h2 class="mi-modal-heading">This is a modal</h2> </div> <div class="mi-modal-body"> <div class="mi-modal-content"> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Impedit eum delectus, libero, accusantium dolores inventore obcaecati placeat cum sapiente vel laboriosam similique totam id ducimus aperiam, ratione fuga blanditiis maiores.</p> <button class="mi-btn btn-danger mi-ripple mi-ripple-light mi-modal-toggle">No</button> <button class="mi-btn btn-info mi-ripple mi-ripple-light">Confirm</button> </div> </div> </div> </div> <div class="mi-modal" data-modal-id="secondmodal"> <div class="mi-modal-overlay mi-modal-toggle"></div> <div class="mi-modal-wrapper mi-modal-transition"> <div class="mi-modal-header"> <button class="mi-modal-close mi-modal-toggle">&times;</button> <h2 class="mi-modal-heading">This is the second modal</h2> </div> <div class="mi-modal-body"> <div class="mi-modal-content"> <p>Nothing to see here</p> <button class="mi-btn btn-danger mi-ripple mi-ripple-light mi-modal-toggle">No</button> <button class="mi-btn btn-info mi-ripple mi-ripple-light">Confirm</button> </div> </div> </div> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM