IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaScript Discussion :

Ouvrir plusieurs fenêtres modales


Sujet :

JavaScript

  1. #1
    Membre actif Avatar de elcoyotos
    Homme Profil pro
    Amateur passionné
    Inscrit en
    Octobre 2006
    Messages
    490
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Amateur passionné

    Informations forums :
    Inscription : Octobre 2006
    Messages : 490
    Points : 294
    Points
    294
    Par défaut Ouvrir plusieurs fenêtres modales
    Bonjour,

    Après des heures de recherche infructueuses je me résous à demander de l'aide.

    Voici le script que j'ai récupéré sur https://www.w3schools.com/howto/tryi...how_css_modal2

    Code HTML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    body {font-family: Arial, Helvetica, sans-serif;}
     
    /* The Modal (background) */
    .modal-box {
      display: none; /* Hidden by default */
      position: fixed; /* Stay in place */
      z-index: 1; /* Sit on top */
      padding-top: 100px; /* Location of the box */
      left: 0;
      top: 0;
      width: 100%; /* Full width */
      height: 100%; /* Full height */
      overflow: auto; /* Enable scroll if needed */
      background-color: rgb(0,0,0); /* Fallback color */
      background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
    }
     
    /* Modal Content */
    .modal-content {
      position: relative;
      background-color: #fefefe;
      margin: auto;
      padding: 0;
      border: 1px solid #888;
      width: 80%;
      box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
      -webkit-animation-name: animatetop;
      -webkit-animation-duration: 0.4s;
      animation-name: animatetop;
      animation-duration: 0.4s
    }
     
    /* Add Animation */
    @-webkit-keyframes animatetop {
      from {top:-300px; opacity:0} 
      to {top:0; opacity:1}
    }
     
    @keyframes animatetop {
      from {top:-300px; opacity:0}
      to {top:0; opacity:1}
    }
     
    /* The Close Button */
    .close {
      color: white;
      float: right;
      font-size: 28px;
      font-weight: bold;
    }
     
    .close:hover,
    .close:focus {
      color: #000;
      text-decoration: none;
      cursor: pointer;
    }
     
    .modal-header {
      padding: 2px 16px;
      background-color: #5cb85c;
      color: white;
    }
     
    .modal-body {padding: 2px 16px;}
     
    .modal-footer {
      padding: 2px 16px;
      background-color: #5cb85c;
      color: white;
    }
    </style>
    </head>
    <body>
     
    <h2>Animated Modal with Header and Footer</h2>
     
    <!-- Trigger/Open The Modal -->
    <button id="myBtn">Open Modal</button>
     
    <!-- The Modal -->
    <div id="myModal" class="modal-box">
     
      <!-- Modal content -->
      <div class="modal-content">
        <div class="modal-header">
          <span class="close">&times;</span>
          <h2>Modal Header</h2>
        </div>
        <div class="modal-body">
          <p>Some text in the Modal Body</p>
          <p>Some other text...</p>
        </div>
        <div class="modal-footer">
          <h3>Modal Footer</h3>
        </div>
      </div>
     
    </div>
     
    <script>
    // Get the modal
    var modal = document.getElementById("myModal");
     
    // Get the button that opens the modal
    var btn = document.getElementById("myBtn");
     
    // Get the <span> element that closes the modal
    var span = document.getElementsByClassName("close")[0];
     
    // When the user clicks the button, open the modal 
    btn.onclick = function() {
      modal.style.display = "block";
    }
     
    // When the user clicks on <span> (x), close the modal
    span.onclick = function() {
      modal.style.display = "none";
    }
     
    // When the user clicks anywhere outside of the modal, close it
    window.onclick = function(event) {
      if (event.target == modal) {
        modal.style.display = "none";
      }
    }
    </script>
     
    </body>
    </html>

    Mon problème est le suivant :

    Je n'arrive pas à écrire le script pour plusieurs fenetres modal.
    Quelqu'un peut il m'aider ?
    Écoute, sinon ta langue te perdra (proverbe Navajo)

  2. #2
    Expert éminent
    Avatar de Watilin
    Homme Profil pro
    En recherche d'emploi
    Inscrit en
    Juin 2010
    Messages
    3 093
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : En recherche d'emploi

    Informations forums :
    Inscription : Juin 2010
    Messages : 3 093
    Points : 6 754
    Points
    6 754
    Par défaut
    Salut,
    pour gérer plusieurs fenêtres modales, il te faut un id différent pour chacune. Le plus simple est peut-être de concaténer un nombre après une base commune, par exemple "myModal0", "myModal1" etc.
    Tu auras également besoin de plusieurs boutons, un par modale, et donc il te faut également une « collection » d’ids pour les boutons.

    Pour les <span> qui servent à fermer les modales, ça devient un peu plus compliqué. Soit tu utilises également des ids, en assignant un onclick à chaque <span>, soit tu fais ce qu’on appelle de la délégation d’évènement, c’est-à-dire qu’au lieu d’écouter les clicks directement sur chaque <span>, tu les écoutes sur un élément parent commun, par exemple le <body>.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    document.body.addEventListener("click", function (event) {
      // teste si l’élément cliqué a la classe "close"
      if (event.target.classList.contains("close")) {
     
        // trouve la modale parente au bouton close
        const parentModal = event.target.closest(".modal-box");
     
        parentModal.style.display = "none";
      }
    });
    La doc de la méthode .closest()

    Enfin, pour le cas où on clique en-dehors d’une modale, le plus simple est de fermer toutes les modales, même si en principe il n’y en a qu’une seule de visible. Dans ce cas tu peux utiliser querySelectorAll getElementsByClassName pour sélectionner tous les éléments ayant la classe "modal-box", et boucler dessus, par exemple avec une boucle for...of.

    Sinon, deux remarques plus générales :
    • pense au mode strict
    • il n’est pas rare que window.onclick soit affecté par plusieurs scripts, ou à plusieurs endroits dans le même script. Quand ça arrive, la dernière affectation écrase toutes les autres. C’est plus prudent d’utiliser window.addEventListener("click", ... )
    La FAQ JavaScript – Les cours JavaScript
    Touche F12 = la console → l’outil indispensable pour développer en JavaScript !

  3. #3
    Membre actif Avatar de elcoyotos
    Homme Profil pro
    Amateur passionné
    Inscrit en
    Octobre 2006
    Messages
    490
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Amateur passionné

    Informations forums :
    Inscription : Octobre 2006
    Messages : 490
    Points : 294
    Points
    294
    Par défaut
    Merci Watilin pour ta réponse. J'ai réussi en faisant ça (avec des ID donc) :
    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    <!DOCTYPE html>
    <head>
        <title>sans titre</title>
        <meta http-equiv="content-type" content="text/html;charset=utf-8" />
        <style>
            /* The Modal (background) */
            .modal-box {
              display:none;
              position:fixed; 
              z-index:1;
              padding-top:100px;
              left:0;
              top:0;
              width: 100%;
              height: 100%;
              overflow: auto;
              background-color: rgb(0,0,0);
              background-color: rgba(0,0,0,0.4);
              line-height: 1.3;
            }
     
            /* Modal Content */
            .modal-content {
                background-color: #fff;
                margin: auto;
                padding: 10px;
                width: 350px;
                border: 5px solid #5372a8;
                height:auto;
                max-height:400px;
                overflow:auto;
                -webkit-animation-name: animatetop;
              -webkit-animation-duration: 0.4s;
              animation-name: animatetop;
              animation-duration: 0.4s
            }
            
            
            /* Add Animation */
            @-webkit-keyframes animatetop {
              from {top:-300px; opacity:0} 
              to {top:0; opacity:1}
            }
     
            @keyframes animatetop {
              from {top:-300px; opacity:0}
              to {top:0; opacity:1}
            }
     
            /* The Close Button */
            .modal-box-close {
              color: #aaaaaa;
              float: right;
              font-size: 28px;
              font-weight: bold;
            }
     
            .modal-box-close:hover,
            .modal-box-close:focus {
              color: #3b81be;
              text-decoration: none;
              cursor: pointer;
            }
        </style>    
    </head>
     
    <body>
        <a href="javascript:;" class="modal-box-lien" id="modal-box-lien-user">Utilisateurs</a> - <a href="javascript:;" class="modal-box-lien" id="modal-box-lien-aide">Aide</a>
     
            <div class="modal-box" id="modal-box-user">
     
                <div class="modal-content">
                    <span class="modal-box-close" id="modal-box-close-user">&times;</span>
                    <h3>User</h3>
                </div>
     
            </div>
     
     
            <div class="modal-box" id="modal-box-aide">
     
                <div class="modal-content">
                    <span class="modal-box-close"  id="modal-box-close-aide">&times;</span>
                    <h3>Aide</h3>
     
                </div>
     
            </div>
    </body>
     
        <script>    
        // Modal user
        var modalUser = document.getElementById("modal-box-user");
        var lienUser = document.getElementById("modal-box-lien-user");
        var spanUser = document.getElementById("modal-box-close-user");
     
        lienUser.onclick = function() {
          modalUser.style.display = "block";
        }
        spanUser.onclick = function() {
          modalUser.style.display = "none";
        }
        document.body.addEventListener("click", function (event) {
          if (event.target == modalUser) {
                modalUser.style.display = "none";
          }
        });     
     
     
     
        // Modal aide
        var modalAide = document.getElementById("modal-box-aide");
        var lienAide = document.getElementById("modal-box-lien-aide");
        var spanAide = document.getElementById("modal-box-close-aide");
     
        lienAide.onclick = function() {
          modalAide.style.display = "block";
        }
        spanAide.onclick = function() {
          modalAide.style.display = "none";
        }
        document.body.addEventListener("click", function (event) {
          if (event.target == modalAide) {
                modalAide.style.display = "none";
          }
        });    
     
        </script>
     
    </html>

    par contre je souhaitais simplifier le code mais çà ne fonctionne pas. Une idée pourquoi ?
    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    <!DOCTYPE html>
    <head>
        <title>sans titre</title>
        <meta http-equiv="content-type" content="text/html;charset=utf-8" />
        <style>
            /* The Modal (background) */
            .modal-box {
              display:none;
              position:fixed; 
              z-index:1;
              padding-top:100px;
              left:0;
              top:0;
              width: 100%;
              height: 100%;
              overflow: auto;
              background-color: rgb(0,0,0);
              background-color: rgba(0,0,0,0.4);
              line-height: 1.3;
            }
     
            /* Modal Content */
            .modal-content {
                background-color: #fff;
                margin: auto;
                padding: 10px;
                width: 350px;
                border: 5px solid #5372a8;
                height:auto;
                max-height:400px;
                overflow:auto;
                -webkit-animation-name: animatetop;
              -webkit-animation-duration: 0.4s;
              animation-name: animatetop;
              animation-duration: 0.4s
            }
            
            
            /* Add Animation */
            @-webkit-keyframes animatetop {
              from {top:-300px; opacity:0} 
              to {top:0; opacity:1}
            }
     
            @keyframes animatetop {
              from {top:-300px; opacity:0}
              to {top:0; opacity:1}
            }
     
            /* The Close Button */
            .modal-box-close {
              color: #aaaaaa;
              float: right;
              font-size: 28px;
              font-weight: bold;
            }
     
            .modal-box-close:hover,
            .modal-box-close:focus {
              color: #3b81be;
              text-decoration: none;
              cursor: pointer;
            }
        </style>    
    </head>
     
    <body>
        <a href="javascript:;" class="modal-box-lien" id="modal-box-lien-user" onclick="ModalBox('user')">Utilisateurs</a> - <a href="javascript:;" class="modal-box-lien" id="modal-box-lien-aide"  onclick="ModalBox('aide')">Aide</a>
     
            <div class="modal-box" id="modal-box-user">
     
                <div class="modal-content">
                    <span class="modal-box-close" id="modal-box-close-user">&times;</span>
                    <h3>User</h3>
                </div>
     
            </div>
     
     
            <div class="modal-box" id="modal-box-aide">
     
                <div class="modal-content">
                    <span class="modal-box-close"  id="modal-box-close-aide">&times;</span>
                    <h3>Aide</h3>
     
                </div>
     
            </div>
    </body>
     
        <script>
            
    function ModalBox(id) {
        
        var modal = document.getElementById("modal-box-"+id);
        var lien = document.getElementById("modal-box-lien-"+id);
        var span = document.getElementById("modal-box-close-"+id);
        
        console.log(modal);
        console.log(lien);
        console.log(span);
     
        lien.onclick = function() {
          modal.style.display = "block";
        }
        span.onclick = function() {
          modal.style.display = "none";
        }
        document.body.addEventListener("click", function (event) {
          if (event.target == modal+id) {
                modal.style.display = "none";
          }
        });         
     
    }    
        </script>
     
    </html>

    Merci d'avance...
    Écoute, sinon ta langue te perdra (proverbe Navajo)

  4. #4
    Membre actif Avatar de elcoyotos
    Homme Profil pro
    Amateur passionné
    Inscrit en
    Octobre 2006
    Messages
    490
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Amateur passionné

    Informations forums :
    Inscription : Octobre 2006
    Messages : 490
    Points : 294
    Points
    294
    Par défaut
    Bon, hier j'étais fatigué....

    Voici un code qui fonctionne :

    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    <!DOCTYPE html>
    <head>
        <title>sans titre</title>
        <meta http-equiv="content-type" content="text/html;charset=utf-8" />
        <style>
            /* The Modal (background) */
            .modal-box {
              display:none;
              position:fixed; 
              z-index:1;
              padding-top:100px;
              left:0;
              top:0;
              width: 100%;
              height: 100%;
              overflow: auto;
              background-color: rgb(0,0,0);
              background-color: rgba(0,0,0,0.4);
              line-height: 1.3;
            }
     
            /* Modal Content */
            .modal-content {
                background-color: #fff;
                margin: auto;
                padding: 10px;
                width: 350px;
                border: 5px solid #5372a8;
                height:auto;
                max-height:400px;
                overflow:auto;
                -webkit-animation-name: animatetop;
              -webkit-animation-duration: 0.4s;
              animation-name: animatetop;
              animation-duration: 0.4s
            }
            
            
            /* Add Animation */
            @-webkit-keyframes animatetop {
              from {top:-300px; opacity:0} 
              to {top:0; opacity:1}
            }
     
            @keyframes animatetop {
              from {top:-300px; opacity:0}
              to {top:0; opacity:1}
            }
     
            /* The Close Button */
            .modal-box-close {
              color: #aaaaaa;
              float: right;
              font-size: 28px;
              font-weight: bold;
            }
     
            .modal-box-close:hover,
            .modal-box-close:focus {
              color: #3b81be;
              text-decoration: none;
              cursor: pointer;
            }
        </style>    
    </head>
     
    <body>
        <a href="javascript:;" class="modal-box-lien" id="modal-box-lien-user" onclick="OuvrirModalBox('user')">Utilisateurs</a> - <a href="javascript:;" class="modal-box-lien" id="modal-box-lien-aide" onclick="OuvrirModalBox('aide')">Aide</a>
     
            <div class="modal-box" id="modal-box-user">
     
                <div class="modal-content">
                    <span class="modal-box-close" id="modal-box-close-user" onclick="FermerModalBox('user')">&times;</span>
                    <h3>User</h3>
                </div>
     
            </div>
     
     
            <div class="modal-box" id="modal-box-aide">
     
                <div class="modal-content">
                    <span class="modal-box-close"  id="modal-box-close-aide" onclick="FermerModalBox('aide')">&times;</span>
                    <h3>Aide</h3>
     
                </div>
     
            </div>
    </body>
     
        <script>
            
    function OuvrirModalBox(id) {
        var modal = document.getElementById("modal-box-"+id);
        var lien = document.getElementById("modal-box-lien-"+id);
        var span = document.getElementById("modal-box-close-"+id);
        
        modal.style.display = "block";
    } 
     
    function FermerModalBox(id) {
        var modal = document.getElementById("modal-box-"+id);
        var lien = document.getElementById("modal-box-lien-"+id);
        var span = document.getElementById("modal-box-close-"+id);
        
        modal.style.display = "none";  
    } 
     
    document.body.addEventListener("click", function (event) {
        var modalUser = document.getElementById("modal-box-user");
        var modalAide = document.getElementById("modal-box-aide");
        if (event.target == modalUser || event.target == modalAide) {
            modalUser.style.display = "none";
            modalAide.style.display = "none";
         }
        
    });
        </script>
     
    </html>

    Même si j'aurai aimé le simplifier au niveau de :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    document.body.addEventListener("click", function (event) {
    car ca me le fait écrire en dur...
    Si quelqu'un à une idée je suis preneur...
    Écoute, sinon ta langue te perdra (proverbe Navajo)

  5. #5
    Inactif  
    Homme Profil pro
    Webmaster
    Inscrit en
    Juin 2021
    Messages
    645
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 57
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Webmaster
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Juin 2021
    Messages : 645
    Points : 1 280
    Points
    1 280
    Par défaut
    Bonjour,

    une méthode plus simple :

    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
     
    Liens : 
    <a href="#" class="modal-box-open" data-modal="user">Utilisateurs</a> - 
    <a href="#" class="modal-box-open" data-modal="aide">Aide</a>
     
    <br />ou<br />
    Boutons : 
    <button type="button" class="modal-box-open" data-modal="user">Utilisateurs</button> 
    <button type="button" class="modal-box-open" data-modal="aide">Aide</button>
     
    <div class="modal-box" id="modal-box-user">
      <div class="modal-content">
        <span class="modal-box-close">&times;</span>
        <h3>User</h3>
      </div>
    </div>
     
     
    <div class="modal-box" id="modal-box-aide">
      <div class="modal-content">
        <span class="modal-box-close">&times;</span>
        <h3>Aide</h3>
      </div>
    </div>
    Code css : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    /* modale box */
    .modal-box {
      display:none;
      position:fixed; 
      z-index:1;
      left:0; right:0;
      top:0; bottom:0;
      width: 100vw;
      height: 100vh;
      overflow: auto;
      background-color: rgb(0,0,0);
      background-color: rgba(0,0,0,0.4);
      line-height: 1.3;
    }
     
    /* Modal Content */
    .modal-content {
      position:relative;
      background-color: #fff;
      width:50vw; min-width:640px;
      height:50vh; min-height:480px;
      margin:5vh auto;
      padding: 10px;
      border: 5px solid #5372a8;
      height:auto;
      overflow:auto;
      -webkit-animation-name: animatetop;
      -webkit-animation-duration: 0.4s;
      animation-name: animatetop;
      animation-duration: 0.4s
    }
     
    /* Add Animation */
    @-webkit-keyframes animatetop {
      from {top:-300px; opacity:0} 
      to {top:0; opacity:1}
    }
     
    @keyframes animatetop {
      from {top:-100vh; opacity:0; }
      to {top:0; opacity:1; }
    }
     
    /* The Close Button */
    .modal-box-close {
      position:absolute;
      top:-10px; right:5px;
      color: #aaaaaa;
      font-size: 48px;
      font-weight: bold;
    }
     
    .modal-box-close:hover,
    .modal-box-close:focus {
      color: #3b81be;
      text-decoration: none;
      cursor: pointer;
    }
    Code js : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    // on attend que le DOM soit chargé
    window.addEventListener("DOMContentLoaded", (event) => { 
      "use strict";
      // ---------------------------------
      // ouverture - fermeture des modales
      // ---------------------------------
      // boutons modal-box-open
      var btns_modal_open = document.querySelectorAll('.modal-box-open');
      btns_modal_open.forEach(function(btn){
        btn.addEventListener('click',function(e){ // au click sur le bouton open
          e.preventDefault(); // inhibe le comportement par defaut (ex. lien <a>)
          var modal_id = this.dataset.modal;
          // ---------------
          // modale
          var modal_box = document.getElementById("modal-box-"+modal_id);
          modal_box.style.display = "block"; // affichae la modale
          // ---------------
          // bouton close
          var modal_close = modal_box.querySelector('.modal-box-close');
          modal_close.addEventListener('click',function(e){ // au click sur le bouton close
            modal_box.style.display = "none"; // masquer la modale
          });
          // ---------------
          // fermeture au clic a l exterieur de la modale
          document.body.addEventListener("click", function (event) {
            if( event.target == modal_box )
            {
              modal_box.style.display = "none"; // masquer la modale
            }
          });
        });
      });
      // ---------------------------------
     
    });

  6. #6
    Membre actif Avatar de elcoyotos
    Homme Profil pro
    Amateur passionné
    Inscrit en
    Octobre 2006
    Messages
    490
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Amateur passionné

    Informations forums :
    Inscription : Octobre 2006
    Messages : 490
    Points : 294
    Points
    294
    Par défaut
    Tain jreaux62, c'est beau ! Mille mercis
    Écoute, sinon ta langue te perdra (proverbe Navajo)

  7. #7
    Inactif  
    Homme Profil pro
    Webmaster
    Inscrit en
    Juin 2021
    Messages
    645
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 57
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Webmaster
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Juin 2021
    Messages : 645
    Points : 1 280
    Points
    1 280
    Par défaut
    oups.. je n'ai pas mis le lien de mon codepen :

  8. #8
    Modérateur

    Avatar de NoSmoking
    Homme Profil pro
    Inscrit en
    Janvier 2011
    Messages
    16 959
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2011
    Messages : 16 959
    Points : 44 122
    Points
    44 122
    Par défaut
    Boujour,
    Citation Envoyé par elcoyotos
    Tain jreaux62, c'est beau !
    Ben non !!!


    Citation Envoyé par jreaux62
    N.B. Un n'est pas de refus...
    difficile de « plusser » sur ce coup

    Explication quand même :
    L'explication en est des plus simples, lorsque tu cliques sur un lien/bouton tu affectes un événement à ton bouton de fermeture et au document, seulement en faisant de la sorte tu les affectes à chaque clic ... on peut imaginer la pile !


    Citation Envoyé par =elcoyotos
    Même si j'aurai aimé le simplifier au niveau de :
    ...
    car ca me le fait écrire en dur...
    Si quelqu'un à une idée je suis preneur...
    tu pourrais simplement tester si le event.target possède la classe modal-box, ce qui pourrait donner :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    document.body.addEventListener("click", function(event) {
      const objClicked = event.target;
      if (objClicked.classList.contains("modal-box")) {
        objClicked.style.display = "none";
      }
    });

  9. #9
    Membre actif Avatar de elcoyotos
    Homme Profil pro
    Amateur passionné
    Inscrit en
    Octobre 2006
    Messages
    490
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Amateur passionné

    Informations forums :
    Inscription : Octobre 2006
    Messages : 490
    Points : 294
    Points
    294
    Par défaut
    Merci NoSmoking pour cette précision. J'ai adopter ton code même si cela me dépasse un peu...
    PS : Pour les admins : je ne reçois plus de mel me prévenant d'un nouveau message et ce depuis un bout de temps (j'ai vérifier aussi en spam, rien)
    Écoute, sinon ta langue te perdra (proverbe Navajo)

  10. #10
    Inactif  
    Homme Profil pro
    Webmaster
    Inscrit en
    Juin 2021
    Messages
    645
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 57
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Webmaster
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Juin 2021
    Messages : 645
    Points : 1 280
    Points
    1 280
    Par défaut
    J'ai cru pouvoir faire le malin en factorisant mon code....
    (je n'aurais pas fait cette erreur en jQuery)

    Cela dit, ça ne valait pas tous ces (auxquels j'ai très mal réagi... mea culpa)

    Mon code JS corrigé... :


    Code js : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    // on attend que le DOM soit chargé
    window.addEventListener("DOMContentLoaded", (event) => { 
      "use strict";
      // ---------------------------------
      // ouverture - fermeture des modales
      // ---------------------------------
      document.body.addEventListener('click', function (event) {
        // ---------------
        // boutons modal-box-open
        if( event.target.classList.contains('modal-box-open') )
        {
          event.preventDefault(); // inhibe le comportement par defaut (ex. lien <a>)
          document.getElementById('modal-box-'+event.target.dataset.modal).style.display = "block"; // affiche la modale
        }
        // ---------------
        // boutons modal-box-close
        else if( event.target.classList.contains('modal-box-close') )
        {
          event.preventDefault(); // inhibe le comportement par defaut (ex. lien <a>)
          event.target.closest('.modal-box').style.display = "none"; // masquer la modale
        }
        // ---------------
        // fermeture au clic a l exterieur de la modale
        else if( event.target.classList.contains('modal-box') )
        {
          event.target.style.display = "none"; // masquer la modale
        }
      });
      // ---------------------------------
     
    });

  11. #11
    Membre confirmé
    Homme Profil pro
    OoW
    Inscrit en
    Juin 2019
    Messages
    134
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : Ukraine

    Informations professionnelles :
    Activité : OoW

    Informations forums :
    Inscription : Juin 2019
    Messages : 134
    Points : 472
    Points
    472
    Par défaut
    \ô/
    Citation Envoyé par jreaux62
    Cela dit, ça ne valait pas tous ces (auxquels j'ai très mal réagi... mea culpa)
    Je parlerais plutôt de « KKnerveux » vu le message que tu avais posté, effacé depuis, et il semblerait que le proverbe « Qui sème le vent récolte la tempête. » prend tout son sens ici !

    As-tu tout dé-moinser ton courroux ?

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 1
    Dernier message: 03/07/2008, 20h48
  2. Possibilité d'ouvrir plusieurs fenêtre TOAD
    Par zainab2000 dans le forum Toad
    Réponses: 2
    Dernier message: 04/10/2007, 17h59
  3. Ouvrir plusieurs fenêtres dans une application
    Par okonkole dans le forum GTK+
    Réponses: 6
    Dernier message: 03/04/2007, 15h06
  4. [VB.NET] Comment ouvrir une fenêtre modale avec Thread ?
    Par Damien10 dans le forum Windows Forms
    Réponses: 1
    Dernier message: 19/11/2006, 11h28
  5. Réponses: 6
    Dernier message: 02/06/2006, 16h55

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo