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

jQuery Discussion :

2 pop-up sur la même page jQuery


Sujet :

jQuery

  1. #1
    Futur Membre du Club
    Femme Profil pro
    Webmaster
    Inscrit en
    Octobre 2011
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Loire (Rhône Alpes)

    Informations professionnelles :
    Activité : Webmaster
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Octobre 2011
    Messages : 12
    Points : 5
    Points
    5
    Par défaut 2 pop-up sur la même page jQuery
    Bonjour à tous,

    Mon objectif est d'avoir plusieurs pop-up différentes sur la même page.
    Ce que j'ai en codage, m'ouvre la 1ère pop-up mais n'a aucune action pour la deuxième.
    J'ai vraiment tenté beaucoup de choses mais en vain.
    Si je mets un href différent pour chaque pop-up par exemple, pas d'effet !

    Pouvez-vous si cela ne vous prendre pas trop de temps essayer de m'aider.

    Merci d'avance.

    Voici les codes que j'ai centralisés sur une même page pour cette demande :
    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
    <!doctype html>
    <html lang="fr">
    <head>
    <title>test pop up</title>
    <meta charset="utf-8">
    <meta name="robots" content="noindex"/>
    <script src="<a href="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js" target="_blank">https://cdnjs.cloudflare.com/ajax/li.../jquery.min.js</a>"></script>
    <script src="main.js"></script>
    <style type="text/css">
    body {
    background-color: #CCC;
    font-family: 'Oswald', sans-serif;
    color: #000;
    }
    #advert-once {
    position: fixed;
    width: 700px;
    bottom: 20px;
    right: 20px;
    left: 20px;
    top: 20px;
    display: none;
    margin: auto;
    background-color:#FFF;
     
    -webkit-background-size: cover;
    background-size: cover;
    padding: 20px;
    z-index: 99999;
    }
    #advert-once .advert-button {
        position: absolute;
        width: 10px;
        height: 10px;
        font-size: 14px;
        line-height: 12px;
        right: -6px;
        top: -7px;
        background-color: red;
        color: white;
        border-radius: 50%;
        padding: 5px;
        cursor: pointer;
    }
     
    #reset-session, #refresh-page {
    display: block;
    width: 250px;
    text-align: center;
    font-family: 'Oswald', sans-serif;
    border-radius: 5px;
    border: none;
    padding: 2px 5px;
    cursor: pointer;
    background-color: #ddd;
    margin: 5px 0;
    }
     
    #reset-session:hover, #refresh-page:hover {
    background-color: #f96e5b;
    }
    @media only screen and (max-width: 600px) {
    #advert-once {
    width: 90%;
    height: 400px;
    -webkit-background-size: 215%;
    background-size: 215%;
    }
    }
        </style>
    </head>
     
    <!--POP UP 1   °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°-->
    <div>
        <a style="background-color: white;color: black;padding: 10px 15px;display: inline-block;" href="javascript:void(0)" id="click">Lire son mail</a>
            <div id="advert-once">
                   Cette pop-up marche, elle s'affiche.
                <div class="advert-button">X</div>
            </div>
    </div>
    <!--FIN POP UP   °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°--><br><br><br><br>
     
     
    <!--POP UP 2   °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°-->
    <div>
        <a style="background-color: white;color: black;padding: 10px 15px;display: inline-block;" href="javascript:void(0)" id="click">Compte rendu</a>
            <div id="advert-once">
    Celle-ci non !
     
                <div class="advert-button">X</div>
            </div>
    </div><br /><br />
     
     
     
    </body>
    </html><br><br>


    Et pour le js :
    Code : 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
    Query(document).ready(function($){  
    $('#click').on('click',function(){
    $('#advert-once').delay(1000).fadeIn();
    });
     
    $('#advert-once .advert-button').on('click',function(){
    $('#advert-once').fadeOut();
    });
    $(document).keydown(function(e) {
        // ESCAPE key pressed
        if (e.keyCode == 27) {
            $('#advert-once').fadeOut();
        }
    });
    $(document).click(function(event) {
      //if you click on anything except the modal itself or the "open modal" link, close the modal
      if (!$(event.target).closest("#advert-once").length && !$(event.target).closest("#click").length) {
        $("body").find("#advert-once").fadeOut();
      }
    });
     
    $('#reset-session').on('click',function(){
    sessionStorage.setItem('advertOnce','');
    });
     
    });

  2. #2
    Expert éminent sénior
    Avatar de mathieu
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    10 234
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 10 234
    Points : 15 531
    Points
    15 531
    Par défaut
    je n'ai pas lu tout votre code mais déjà je vois que vous avez plusieurs balises html avec le même "id". un identifiant doit être unique sur toute la page.

  3. #3
    Membre averti Avatar de ASCIIDEFOND
    Homme Profil pro
    Autodidacte passionné
    Inscrit en
    Novembre 2002
    Messages
    213
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Landes (Aquitaine)

    Informations professionnelles :
    Activité : Autodidacte passionné

    Informations forums :
    Inscription : Novembre 2002
    Messages : 213
    Points : 400
    Points
    400
    Par défaut
    Bonsoir à tous,

    Comme le signalait Mathieu, problème de doublon sur les noms des ID.
    Un exemple sans l’utilisation d’ID.

    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
    <style type="text/css">
        body {
            background-color: #CCC;
            font-family: 'Oswald', sans-serif;
            color: #000;
        }
     
        a {
            cursor: pointer;
        }
     
        .advert-once {
            position: fixed;
            width: 700px;
            bottom: 20px;
            right: 20px;
            left: 20px;
            top: 20px;
            display: none;
            margin: auto;
            background-color: #FFF;
            -webkit-background-size: cover;
            background-size: cover;
            padding: 20px;
            z-index: 2;
        }
     
        .masque {
            background-color: rgba(9, 113, 210, 0.5);
            position: absolute;
            top: 0px;
            left: 0px;
            width: 100vw;
            height: 100vh;
            overflow: hidden;
            display: none;
            z-index: 1;
        }
     
        .advert-button {
            position: relative;
            width: 18px;
            height: 18px;
            float: right;
            background-color: red;
            color: white;
            border-radius: 50%;
            cursor: pointer;
            top: -29px;
            right: -29px;
            font-weight: bold;
            text-align: center;
        }
     
        #reset-session,
        #refresh-page {
            display: block;
            width: 250px;
            text-align: center;
            font-family: 'Oswald', sans-serif;
            border-radius: 5px;
            border: none;
            padding: 2px 5px;
            cursor: pointer;
            background-color: #ddd;
            margin: 5px 0;
        }
     
        #reset-session:hover,
        #refresh-page:hover {
            background-color: #f96e5b;
        }
     
        @media only screen and (max-width: 600px) {
            #advert-once {
                width: 90%;
                height: 400px;
                -webkit-background-size: 215%;
                background-size: 215%;
            }
        }
    </style>
    </head>
     
    <body>
        <div class="masque"></div>
        <!--POP UP 1   °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°-->
        <div>
            <a style="background-color: white;color: black;padding: 10px 15px;display: inline-block;">Lire son mail</a>
            <div class="advert-once">
                Cette pop-up marche, elle s'affiche.
                <div class="advert-button">X</div>
            </div>
        </div>
        <!--FIN POP UP   °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°--><br><br><br><br>
     
     
        <!--POP UP 2   °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°-->
        <div>
            <a style="background-color: white;color: black;padding: 10px 15px;display: inline-block;">Compte rendu</a>
            <div class="advert-once">
                Celle-ci aussi s'affiche à présent ;) !
                <div class="advert-button">X</div>
            </div>
        </div><br /><br />
    </body>

    Code JQuery : 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
     
    (function ($) {
     
        $('a').on('click', function () {
            $('div.masque').fadeIn();
            $(this).nextAll('.advert-once:first').delay(1000).fadeIn();
        });
     
       $(document).on('click keydown', '*', function (e) {
           if ($(this).hasClass('masque') || $(this).hasClass('advert-button') || e.key === 'Escape') {
               if ($('.advert-once').is(':visible')) {
                   $('.advert-once').fadeOut();
                   $('.masque').fadeOut();
               }
           }
       });
     
        $('#reset-session').on('click', function () {
            sessionStorage.setItem('advertOnce', '');
        });
     
    })(jQuery);

  4. #4
    Expert confirmé Avatar de Toufik83
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2012
    Messages
    2 405
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2012
    Messages : 2 405
    Points : 4 841
    Points
    4 841
    Par défaut
    Bonjour,

    Je rajoutes qu'on peut modifier la ligne de la condition suivante if($(this).css('display') != 'none') et utiliser $.is() avec le sélecteur :visible :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    if($(this).css('display') != 'none') 
    //est équivalente à 
    if($(this).is(':visible'))

  5. #5
    Membre averti Avatar de ASCIIDEFOND
    Homme Profil pro
    Autodidacte passionné
    Inscrit en
    Novembre 2002
    Messages
    213
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Landes (Aquitaine)

    Informations professionnelles :
    Activité : Autodidacte passionné

    Informations forums :
    Inscription : Novembre 2002
    Messages : 213
    Points : 400
    Points
    400
    Par défaut
    Bonjour à tous,

    Citation Envoyé par Toufik83 Voir le message
    Bonjour,
    Je rajoutes qu'on peut modifier la ligne de la condition suivante if($(this).css('display') != 'none') et utiliser $.is() avec le sélecteur :visible :
    Effectivement c’est plus simple et compréhensible, merci.
    J’en ai aussi profité pour simplifier le déclenchement des 'fadeOut()' et les conditions.

  6. #6
    Futur Membre du Club
    Femme Profil pro
    Webmaster
    Inscrit en
    Octobre 2011
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Loire (Rhône Alpes)

    Informations professionnelles :
    Activité : Webmaster
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Octobre 2011
    Messages : 12
    Points : 5
    Points
    5
    Par défaut Pas d'action au clic !
    Tout d'abord merci à vous tous pour l'aide apportée.
    J'ai crée deux fichiers :index.html et js.js.
    Je ne vois pas ce que je ne comprends pas !
    Pouvez-vous encore y jeter un oeil ?
    Merci.

    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
    <html lang="fr">
    <head>
    <title>test pop up</title>
    <meta charset="utf-8">
    <meta name="robots" content="noindex"/>
    <script src="js.js"></script>
     
    <style type="text/css">
        body {
            background-color: #CCC;
            font-family: 'Oswald', sans-serif;
            color: #000;
        }
     
        a {
            cursor: pointer;
        }
     
        .advert-once {
            position: fixed;
            width: 700px;
            bottom: 20px;
            right: 20px;
            left: 20px;
            top: 20px;
            display: none;
            margin: auto;
            background-color: #FFF;
            -webkit-background-size: cover;
            background-size: cover;
            padding: 20px;
            z-index: 2;
        }
     
        .masque {
            background-color: rgba(9, 113, 210, 0.5);
            position: absolute;
            top: 0px;
            left: 0px;
            width: 100vw;
            height: 100vh;
            overflow: hidden;
            display: none;
            z-index: 1;
        }
     
        .advert-button {
            position: relative;
            width: 18px;
            height: 18px;
            float: right;
            background-color: red;
            color: white;
            border-radius: 50%;
            cursor: pointer;
            top: -29px;
            right: -29px;
            font-weight: bold;
            text-align: center;
        }
     
        #reset-session,
        #refresh-page {
            display: block;
            width: 250px;
            text-align: center;
            font-family: 'Oswald', sans-serif;
            border-radius: 5px;
            border: none;
            padding: 2px 5px;
            cursor: pointer;
            background-color: #ddd;
            margin: 5px 0;
        }
     
        #reset-session:hover,
        #refresh-page:hover {
            background-color: #f96e5b;
        }
     
        @media only screen and (max-width: 600px) {
            #advert-once {
                width: 90%;
                height: 400px;
                -webkit-background-size: 215%;
                background-size: 215%;
            }
        }
    </style>
    </head>
     
    <body>
        <div class="masque"></div>
        <!--POP UP 1   °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°-->
        <div>
            <a style="background-color: white;color: black;padding: 10px 15px;display: inline-block;">Lire son mail</a>
            <div class="advert-once">
                Cette pop-up marche, elle s'affiche.
                <div class="advert-button">X</div>
            </div>
        </div>
        <!--FIN POP UP   °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°--><br><br><br><br>
     
     
        <!--POP UP 2   °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°-->
        <div>
            <a style="background-color: white;color: black;padding: 10px 15px;display: inline-block;">Compte rendu</a>
            <div class="advert-once">
                Celle-ci aussi s'affiche à présent ;) !
                <div class="advert-button">X</div>
            </div>
        </div><br /><br />
    </body>


    et

    Code : 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
    (function ($) {
     
        $('a').on('click', function () {
            $('div.masque').fadeIn();
            $(this).nextAll('.advert-once:first').delay(1000).fadeIn();
        });
     
       $(document).on('click keydown', '*', function (e) {
           if ($(this).hasClass('masque') || $(this).hasClass('advert-button') || e.key === 'Escape') {
               if ($('.advert-once').is(':visible')) {
                   $('.advert-once').fadeOut();
                   $('.masque').fadeOut();
               }
           }
       });
     
        $('#reset-session').on('click', function () {
            sessionStorage.setItem('advertOnce', '');
        });
     
    })(jQuery);

    Merci.

  7. #7
    Expert confirmé Avatar de Toufik83
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2012
    Messages
    2 405
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2012
    Messages : 2 405
    Points : 4 841
    Points
    4 841
    Par défaut
    Bonjour,

    Il faut charger jQuery avant le script js, et ouvrir l'outil de développement sous l'onglet console pour voir s'il y'a des erreurs...

Discussions similaires

  1. Réponses: 1
    Dernier message: 29/01/2013, 08h40
  2. Deux bibliothèques jQuery sur une même page
    Par hous04 dans le forum jQuery
    Réponses: 3
    Dernier message: 27/12/2012, 10h22
  3. Deux scripts jquery sur la même page ?
    Par beegees dans le forum jQuery
    Réponses: 1
    Dernier message: 01/03/2011, 23h34
  4. pop-up sur la même page
    Par jbtz64 dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 29/05/2008, 16h13

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