2017-10-24 5 views
0

Ich bin in der Mitte der Erstellung eines mehrere modale für meine Website, die ich arbeite. Ich habe es geschafft, mit etwas zu beginnen und momentan bin ich fest daran, Modals separat voneinander auszulösen. Es ist im Grunde wie Bootstrap tut es durch Anhängen eines Daten-Attributs, glaube ich. Ich könnte einfach Bootstrap verwenden, um mein Problem zu lösen, aber ich möchte den ganzen Code Bloat vermeiden und habe es einfach selbst gebaut.Erstellen mehrerer Mods mit jquery

Hier ein Beispielcode, an dem ich gearbeitet habe. Ich habe es geschafft, das Looping zu machen, aber es scheint das falsche Modal zu öffnen.

$('.modal-container').insertAfter('.layout-container'); 
 

 
    $('.modal-container').each(function(i) { 
 
     $(this).attr('id', "modal" + (i + 1)); 
 

 
     modal_container_id = $(this).attr('data-modal', "modal" + (i + 1)); 
 
    }); 
 

 
    $('.modal-item .modal-btn').each(function(i) { 
 

 
     var modal_id = $(this).attr('id', "modal" + (i + 1)); 
 

 
     $(modal_id).on('click', function() { 
 
      $(modal_container_id).addClass('show-modal'); 
 
      $('body').addClass('lock-scroll'); 
 
     }); 
 
    }) 
 

 

 
    //Close Modal 
 
\t $('.modal-container .close-modal').on('click', function() { 
 
\t  $('.modal-container').removeClass('show-modal'); 
 
\t  $('body').removeClass('lock-scroll'); 
 
\t })
/* http://meyerweb.com/eric/tools/css/reset/ 
 
    v2.0 | 20110126 
 
    License: none (public domain) 
 
*/ 
 
html, body, div, span, applet, object, iframe, 
 
h1, h2, h3, h4, h5, h6, p, blockquote, pre, 
 
a, abbr, acronym, address, big, cite, code, 
 
del, dfn, em, img, ins, kbd, q, s, samp, 
 
small, strike, strong, sub, sup, tt, var, 
 
b, u, i, center, 
 
dl, dt, dd, ol, ul, li, 
 
fieldset, form, label, legend, 
 
table, caption, tbody, tfoot, thead, tr, th, td, 
 
article, aside, canvas, details, embed, 
 
figure, figcaption, footer, header, hgroup, 
 
menu, nav, output, ruby, section, summary, 
 
time, mark, audio, video { 
 
    margin: 0; 
 
    padding: 0; 
 
    border: 0; 
 
    font-size: 100%; 
 
    font: inherit; 
 
    vertical-align: baseline; } 
 

 
/* HTML5 display-role reset for older browsers */ 
 
article, aside, details, figcaption, figure, 
 
footer, header, hgroup, menu, nav, section { 
 
    display: block; } 
 

 
body { 
 
    line-height: 1; } 
 

 
ol, ul { 
 
    list-style: none; } 
 

 
blockquote, q { 
 
    quotes: none; } 
 

 
blockquote:before, blockquote:after, 
 
q:before, q:after { 
 
    content: ''; 
 
    content: none; } 
 

 
table { 
 
    border-collapse: collapse; 
 
    border-spacing: 0; } 
 

 
.layout-container { 
 
    position: relative; } 
 

 
.modal-container { 
 
    position: fixed; 
 
    top: 0; 
 
    left: 0; 
 
    z-index: 100; 
 
    visibility: hidden; 
 
    opacity: 0; 
 
    width: 100%; 
 
    height: 100%; 
 
    background-color: rgba(0, 0, 0, 0.5); 
 
    -webkit-transition: opacity 0.3s ease-in; 
 
    -moz-transition: opacity 0.3s ease-in; 
 
    transition: opacity 0.3s ease-in; } 
 
    .modal-container.show-modal { 
 
    visibility: visible; 
 
    opacity: 1; 
 
    -webkit-transition: opacity 0.3s ease-in; 
 
    -moz-transition: opacity 0.3s ease-in; 
 
    transition: opacity 0.3s ease-in; } 
 
    .modal-container .modal-body { 
 
    position: absolute; 
 
    max-width: 500px; 
 
    height: 500px; 
 
    top: 0; 
 
    bottom: 0; 
 
    right: 0; 
 
    left: 0; 
 
    margin: auto; 
 
    padding: 20px; 
 
    background-color: #fff; } 
 

 
/*# sourceMappingURL=style.css.map */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div class="layout-container"> 
 
    <div class="modal-item"> 
 
     <button class="modal-btn">Modal One Trigger</button> 
 

 
     <div class="modal-container"> 
 
      <button class="close-modal">Close Modal</button> 
 
      Modal One 
 
     </div> 
 
    </div> 
 

 

 
    <div class="modal-item"> 
 
     <button class="modal-btn">Modal Two Trigger</button> 
 

 
     <div class="modal-container"> 
 
      <button class="close-modal">Close Modal</button> 
 
      Modal Two 
 
     </div> 
 
    </div> 
 

 
</div>

+1

Wir mehr Ihrer HTML benötigen - wir können nicht sehen, wie Sie Ihre Modals codiert haben – Haring10

+1

Sie mehrere Fehler hier haben: Erstens, geben Sie 'modal1' /' modal2 'als id zu den öffnenden buttons und dem modalen div, und jede id sollte eindeutig sein, ändere eines der Präfixe. Zweitens wählen Sie nicht das gute Element in der Click-Funktion aus, sondern Sie verwenden eine vorherige Variable '$ (modal_container_id)'. Verwenden Sie stattdessen einen Selektor basierend auf der Schaltflächen-ID – Kaddath

+0

Wenn Sie das ID-Selektor-Präfix '#' dh '$ ('#' + modal_id)' verwenden möchten, ist es jedoch besser, die DOM-Traversalmethode zu verwenden, um das gewünschte Element – Satpal

Antwort

1

Wenn Sie die folgenden Änderungen an Ihrem Code zu machen, wird es funktionieren.

vorgenommene Änderungen:

  1. Ich habe $(this).attr('id', "modal" + (i + 1)) die Zeile kommentiert, wie es doppelte id für modal-container schaffen.

  2. ändern $(modal_container_id).addClass('show-modal'); zu $("[data-modal='" + $(this).attr("id") + "']").addClass('show-modal'); als modal_container_id bezieht sich wegen der vorherigen Schleife modal2.

$('.modal-container').insertAfter('.layout-container'); 
 

 
$('.modal-container').each(function (i) { 
 
\t modal_container_id = $(this).attr('data-modal', "modal" + (i + 1)); 
 
}); 
 

 
$('.modal-item .modal-btn').each(function (i) { 
 
\t var modal_id = $(this).attr('id', "modal" + (i + 1)); 
 
\t $(modal_id).on('click', function() { 
 
\t \t $("[data-modal='" + $(this).attr("id") + "']").addClass('show-modal'); 
 
\t \t $('body').addClass('lock-scroll'); 
 
\t }); 
 
}) 
 

 
//Close Modal 
 
$('.modal-container .close-modal').on('click', function() { 
 
\t $('.modal-container').removeClass('show-modal'); 
 
\t $('body').removeClass('lock-scroll'); 
 
})
/* http://meyerweb.com/eric/tools/css/reset/ 
 
v2.0 | 20110126 
 
License: none (public domain) 
 
*/ 
 

 
\t html, 
 
\t body, 
 
\t div, 
 
\t span, 
 
\t applet, 
 
\t object, 
 
\t iframe, 
 
\t h1, 
 
\t h2, 
 
\t h3, 
 
\t h4, 
 
\t h5, 
 
\t h6, 
 
\t p, 
 
\t blockquote, 
 
\t pre, 
 
\t a, 
 
\t abbr, 
 
\t acronym, 
 
\t address, 
 
\t big, 
 
\t cite, 
 
\t code, 
 
\t del, 
 
\t dfn, 
 
\t em, 
 
\t img, 
 
\t ins, 
 
\t kbd, 
 
\t q, 
 
\t s, 
 
\t samp, 
 
\t small, 
 
\t strike, 
 
\t strong, 
 
\t sub, 
 
\t sup, 
 
\t tt, 
 
\t var, 
 
\t b, 
 
\t u, 
 
\t i, 
 
\t center, 
 
\t dl, 
 
\t dt, 
 
\t dd, 
 
\t ol, 
 
\t ul, 
 
\t li, 
 
\t fieldset, 
 
\t form, 
 
\t label, 
 
\t legend, 
 
\t table, 
 
\t caption, 
 
\t tbody, 
 
\t tfoot, 
 
\t thead, 
 
\t tr, 
 
\t th, 
 
\t td, 
 
\t article, 
 
\t aside, 
 
\t canvas, 
 
\t details, 
 
\t embed, 
 
\t figure, 
 
\t figcaption, 
 
\t footer, 
 
\t header, 
 
\t hgroup, 
 
\t menu, 
 
\t nav, 
 
\t output, 
 
\t ruby, 
 
\t section, 
 
\t summary, 
 
\t time, 
 
\t mark, 
 
\t audio, 
 
\t video { 
 
\t \t margin: 0; 
 
\t \t padding: 0; 
 
\t \t border: 0; 
 
\t \t font-size: 100%; 
 
\t \t font: inherit; 
 
\t \t vertical-align: baseline; 
 
\t } 
 

 
\t /* HTML5 display-role reset for older browsers */ 
 

 
\t article, 
 
\t aside, 
 
\t details, 
 
\t figcaption, 
 
\t figure, 
 
\t footer, 
 
\t header, 
 
\t hgroup, 
 
\t menu, 
 
\t nav, 
 
\t section { 
 
\t \t display: block; 
 
\t } 
 

 
\t body { 
 
\t \t line-height: 1; 
 
\t } 
 

 
\t ol, 
 
\t ul { 
 
\t \t list-style: none; 
 
\t } 
 

 
\t blockquote, 
 
\t q { 
 
\t \t quotes: none; 
 
\t } 
 

 
\t blockquote:before, 
 
\t blockquote:after, 
 
\t q:before, 
 
\t q:after { 
 
\t \t content: ''; 
 
\t \t content: none; 
 
\t } 
 

 
\t table { 
 
\t \t border-collapse: collapse; 
 
\t \t border-spacing: 0; 
 
\t } 
 

 
\t .layout-container { 
 
\t \t position: relative; 
 
\t } 
 

 
\t .modal-container { 
 
\t \t position: fixed; 
 
\t \t top: 0; 
 
\t \t left: 0; 
 
\t \t z-index: 100; 
 
\t \t visibility: hidden; 
 
\t \t opacity: 0; 
 
\t \t width: 100%; 
 
\t \t height: 100%; 
 
\t \t background-color: rgba(0, 0, 0, 0.5); 
 
\t \t -webkit-transition: opacity 0.3s ease-in; 
 
\t \t -moz-transition: opacity 0.3s ease-in; 
 
\t \t transition: opacity 0.3s ease-in; 
 
\t } 
 

 
\t .modal-container.show-modal { 
 
\t \t visibility: visible; 
 
\t \t opacity: 1; 
 
\t \t -webkit-transition: opacity 0.3s ease-in; 
 
\t \t -moz-transition: opacity 0.3s ease-in; 
 
\t \t transition: opacity 0.3s ease-in; 
 
\t } 
 

 
\t .modal-container .modal-body { 
 
\t \t position: absolute; 
 
\t \t max-width: 500px; 
 
\t \t height: 500px; 
 
\t \t top: 0; 
 
\t \t bottom: 0; 
 
\t \t right: 0; 
 
\t \t left: 0; 
 
\t \t margin: auto; 
 
\t \t padding: 20px; 
 
\t \t background-color: #fff; 
 
\t } 
 

 
\t /*# sourceMappingURL=style.css.map */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
<div class="layout-container"> 
 
\t <div class="modal-item"> 
 
\t \t <button class="modal-btn">Modal One Trigger</button> 
 

 
\t \t <div class="modal-container"> 
 
\t \t \t <button class="close-modal">Close Modal</button> 
 
\t \t \t Modal One 
 
\t \t </div> 
 
\t </div> 
 

 
\t <div class="modal-item"> 
 
\t \t <button class="modal-btn">Modal Two Trigger</button> 
 

 
\t \t <div class="modal-container"> 
 
\t \t \t <button class="close-modal">Close Modal</button> 
 
\t \t \t Modal Two 
 
\t \t </div> 
 
\t </div> 
 
</div>

2

Wenn ich Ihre Frage verstanden. Sie möchten den jeweiligen modalen Container bei Button-Click-Ereignissen anzeigen. Ich habe den Code vereinfacht. Hoffe, es funktioniert.

 $('.modal-btn').on('click', function() { 
 
      $(this).next('.modal-container').addClass('show-modal'); 
 
      $('body').addClass('lock-scroll'); 
 
     }); 
 

 

 
    //Close Modal 
 
\t $('.modal-container .close-modal').on('click', function() { 
 
\t  $('.modal-container').removeClass('show-modal'); 
 
\t  $('body').removeClass('lock-scroll'); 
 
\t })
/* http://meyerweb.com/eric/tools/css/reset/ 
 
    v2.0 | 20110126 
 
    License: none (public domain) 
 
*/ 
 
html, body, div, span, applet, object, iframe, 
 
h1, h2, h3, h4, h5, h6, p, blockquote, pre, 
 
a, abbr, acronym, address, big, cite, code, 
 
del, dfn, em, img, ins, kbd, q, s, samp, 
 
small, strike, strong, sub, sup, tt, var, 
 
b, u, i, center, 
 
dl, dt, dd, ol, ul, li, 
 
fieldset, form, label, legend, 
 
table, caption, tbody, tfoot, thead, tr, th, td, 
 
article, aside, canvas, details, embed, 
 
figure, figcaption, footer, header, hgroup, 
 
menu, nav, output, ruby, section, summary, 
 
time, mark, audio, video { 
 
    margin: 0; 
 
    padding: 0; 
 
    border: 0; 
 
    font-size: 100%; 
 
    font: inherit; 
 
    vertical-align: baseline; } 
 

 
/* HTML5 display-role reset for older browsers */ 
 
article, aside, details, figcaption, figure, 
 
footer, header, hgroup, menu, nav, section { 
 
    display: block; } 
 

 
body { 
 
    line-height: 1; } 
 

 
ol, ul { 
 
    list-style: none; } 
 

 
blockquote, q { 
 
    quotes: none; } 
 

 
blockquote:before, blockquote:after, 
 
q:before, q:after { 
 
    content: ''; 
 
    content: none; } 
 

 
table { 
 
    border-collapse: collapse; 
 
    border-spacing: 0; } 
 

 
.layout-container { 
 
    position: relative; } 
 

 
.modal-container { 
 
    position: fixed; 
 
    top: 0; 
 
    left: 0; 
 
    z-index: 100; 
 
    visibility: hidden; 
 
    opacity: 0; 
 
    width: 100%; 
 
    height: 100%; 
 
    background-color: rgba(0, 0, 0, 0.5); 
 
    -webkit-transition: opacity 0.3s ease-in; 
 
    -moz-transition: opacity 0.3s ease-in; 
 
    transition: opacity 0.3s ease-in; } 
 
    .modal-container.show-modal { 
 
    visibility: visible; 
 
    opacity: 1; 
 
    -webkit-transition: opacity 0.3s ease-in; 
 
    -moz-transition: opacity 0.3s ease-in; 
 
    transition: opacity 0.3s ease-in; } 
 
    .modal-container .modal-body { 
 
    position: absolute; 
 
    max-width: 500px; 
 
    height: 500px; 
 
    top: 0; 
 
    bottom: 0; 
 
    right: 0; 
 
    left: 0; 
 
    margin: auto; 
 
    padding: 20px; 
 
    background-color: #fff; } 
 

 
/*# sourceMappingURL=style.css.map */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div class="layout-container"> 
 
    <div class="modal-item"> 
 
     <button class="modal-btn">Modal One Trigger</button> 
 

 
     <div class="modal-container"> 
 
      <button class="close-modal">Close Modal</button> 
 
      Modal One 
 
     </div> 
 
    </div> 
 

 

 
    <div class="modal-item"> 
 
     <button class="modal-btn">Modal Two Trigger</button> 
 

 
     <div class="modal-container"> 
 
      <button class="close-modal">Close Modal</button> 
 
      Modal Two 
 
     </div> 
 
    </div> 
 

 
</div>

+0

Halten, was, wenn der' .modal-Container' außerhalb der '.layout-container' class daher '$ ('. modal-container'). insertAfter ('. layout-container');' muss außerhalb liegen – clestcruz

+0

Hier befinden sich .modal-container und .modal-btn beide unter demselben Parent. Wenn sich .modal-container außerhalb des .layout-Containers befindet, können Sie die jQuery parent() -Methode verwenden, um den nächsten .modal-Container zu finden. –

1

können Sie einfach Jquery UI modals erstellen verwenden.

$(document).ready(function(){ 
 
\t \t 
 
\t \t $(".dialog").dialog({ 
 
\t \t \t autoOpen: false 
 
\t \t }); 
 
\t \t 
 
\t \t $("#modal1Btn").click(function(){ 
 
\t \t \t $("#dialog1").dialog("open"); 
 
\t \t }); 
 
\t \t 
 
\t \t $("#modal2Btn").click(function(){ 
 
\t \t \t $("#dialog2").dialog("open"); 
 
\t \t }) 
 
\t }) 
 
<!doctype html> 
 
<html lang="en"> 
 
<head> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
    <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> 
 
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script> 
 
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
 

 
</head> 
 
<body> 
 
    
 
<button id="modal1Btn" class="modal-btn">Modal One Trigger</button> 
 
<button id="modal2Btn" class="modal-btn">Modal Two Trigger</button> 
 
    
 
<div class="dialog" id="dialog1" title="Basic dialog"> 
 
    <p>Dailog 1</p> 
 
</div> 
 

 
<div class="dialog" id="dialog2" title="Basic dialog"> 
 
    <p>Dailog 2</p> 
 
</div> 
 
    
 
    
 
</body> 
 
</html>

1

Ich habe Ihre js mantaining die HTML-Struktur bearbeitet. Sie müssen nur ids erarbeiten, bevor das modale außerhalb des Behälters zu bewegen;)

$('.modal-item').each(function(idx) { 
 
    var $modal = $(this).find('.modal-container'), 
 
    $button = $(this).find('.modal-btn'); 
 
    $button.data('modal', '#modal-' + idx); 
 
    $modal.attr('id', 'modal-' + idx); 
 
    $modal.insertAfter('.layout-container'); 
 
}); 
 

 
$('.layout-container').on('click', '.modal-btn', function() { 
 
    $($(this).data('modal')).addClass('show-modal'); 
 
    $('body').addClass('lock-scroll'); 
 
}); 
 

 
//Close Modal 
 
$('.modal-container .close-modal').on('click', function() { 
 
    $('.modal-container').removeClass('show-modal'); 
 
    $('body').removeClass('lock-scroll'); 
 
})
/* http://meyerweb.com/eric/tools/css/reset/ 
 
    v2.0 | 20110126 
 
    License: none (public domain) 
 
*/ 
 
html, body, div, span, applet, object, iframe, 
 
h1, h2, h3, h4, h5, h6, p, blockquote, pre, 
 
a, abbr, acronym, address, big, cite, code, 
 
del, dfn, em, img, ins, kbd, q, s, samp, 
 
small, strike, strong, sub, sup, tt, var, 
 
b, u, i, center, 
 
dl, dt, dd, ol, ul, li, 
 
fieldset, form, label, legend, 
 
table, caption, tbody, tfoot, thead, tr, th, td, 
 
article, aside, canvas, details, embed, 
 
figure, figcaption, footer, header, hgroup, 
 
menu, nav, output, ruby, section, summary, 
 
time, mark, audio, video { 
 
    margin: 0; 
 
    padding: 0; 
 
    border: 0; 
 
    font-size: 100%; 
 
    font: inherit; 
 
    vertical-align: baseline; } 
 

 
/* HTML5 display-role reset for older browsers */ 
 
article, aside, details, figcaption, figure, 
 
footer, header, hgroup, menu, nav, section { 
 
    display: block; } 
 

 
body { 
 
    line-height: 1; } 
 

 
ol, ul { 
 
    list-style: none; } 
 

 
blockquote, q { 
 
    quotes: none; } 
 

 
blockquote:before, blockquote:after, 
 
q:before, q:after { 
 
    content: ''; 
 
    content: none; } 
 

 
table { 
 
    border-collapse: collapse; 
 
    border-spacing: 0; } 
 

 
.layout-container { 
 
    position: relative; } 
 

 
.modal-container { 
 
    position: fixed; 
 
    top: 0; 
 
    left: 0; 
 
    z-index: 100; 
 
    visibility: hidden; 
 
    opacity: 0; 
 
    width: 100%; 
 
    height: 100%; 
 
    background-color: rgba(0, 0, 0, 0.5); 
 
    -webkit-transition: opacity 0.3s ease-in; 
 
    -moz-transition: opacity 0.3s ease-in; 
 
    transition: opacity 0.3s ease-in; } 
 
    .modal-container.show-modal { 
 
    visibility: visible; 
 
    opacity: 1; 
 
    -webkit-transition: opacity 0.3s ease-in; 
 
    -moz-transition: opacity 0.3s ease-in; 
 
    transition: opacity 0.3s ease-in; } 
 
    .modal-container .modal-body { 
 
    position: absolute; 
 
    max-width: 500px; 
 
    height: 500px; 
 
    top: 0; 
 
    bottom: 0; 
 
    right: 0; 
 
    left: 0; 
 
    margin: auto; 
 
    padding: 20px; 
 
    background-color: #fff; } 
 

 
/*# sourceMappingURL=style.css.map */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div class="layout-container"> 
 
    <div class="modal-item"> 
 
     <button class="modal-btn">Modal One Trigger</button> 
 

 
     <div class="modal-container"> 
 
      <button class="close-modal">Close Modal</button> 
 
      Modal One 
 
     </div> 
 
    </div> 
 

 

 
    <div class="modal-item"> 
 
     <button class="modal-btn">Modal Two Trigger</button> 
 

 
     <div class="modal-container"> 
 
      <button class="close-modal">Close Modal</button> 
 
      Modal Two 
 
     </div> 
 
    </div> 
 

 
</div>