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 :

Ouverture fermeture d'une DIV dynamiquement sans JQuery


Sujet :

JavaScript

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Juillet 2004
    Messages
    100
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2004
    Messages : 100
    Par défaut Ouverture fermeture d'une DIV dynamiquement sans JQuery
    Bonjour,

    voici mon code complet :

    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
    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="utf-8" />
    <title>Progressiv_Div</title>
    <style type="text/css">
    html, body, * {
            margin: 0;
            padding: 0;
    }
    body {
            font-family: helvetica;
    }
    #global {
            display: block;
            margin: 50px;
    }
    #dv {
            display: none;
            margin: 20px 0;
            padding: 10px;
            width: 400px;
            border: 1px solid #000;
            height: auto;
            overflow: hidden;
    }
    </style>
    <script type="text/javascript">
     
    var prg = function() {
     
            this.div;
        this.osh;
        this.ind = 0;
        var that = this;
     
        this.action = function()
            {
                    if (that.ind <= that.osh)
                    {
                            that.ind += 2;
                            that.div.style.height = that.ind + "px";
                            setTimeout(that.action, 10);
                    }
                    else
                    { 
                     that.ind = 0; 
                    }
            }; 
    };
     
    function ouvrir(ref) {
            var nprg = new prg();
     
        nprg.div = document.getElementById(ref);
        nprg.div.style.display = "block";
        nprg.osh = nprg.div.offsetHeight;
        nprg.div.style.height = "0px";
        nprg.action();
    }
     
     
     
     
    function ferme(ref) {
     
    /*là je ne sais pas comment faire */ 
     
     
    }
     
     
     
    function visibilite(thingId)
    {
            var targetElement;
            
            targetElement = document.getElementById(thingId);
            
            if (targetElement.style.display == "none")
            {
                    ouvrir('dv');
                    
                    document.getElementById('a').innerHTML = 'Fermer le cadre ...';
                    
            }
            else
            {
                    ferme('dv');
     
                    document.getElementById('a').innerHTML = 'Ouvrir le cadre ...'; 
            }
    }
    </script>
    </head>
    <body>
    <div id="global">
      <div id="dv" style="display:none">
        <h1>contenu</h1>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
      </div>
    </div>
    <a id="a" href="javascript:visibilite('dv');">Ouvrir le cadre ...</a>
    </body>
    </html>

    l'ouverture de la DIV fonctionne bien ... Par contre pour la fermeture (ligne 67 j'ai testé des solutions en m'inspirant de la fonction ouvrir mais je n'y arrive pas.
    Quelqu'un peut-il m'aider ?

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

    Informations professionnelles :
    Activité : Autodidacte

    Informations forums :
    Inscription : Novembre 2002
    Messages : 235
    Par défaut
    Salut zouetchou,

    Et comme çà ?
    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
    <!doctype html>
    <html lang="en">
     
        <head>
            <meta charset="utf-8" />
            <title>Progressiv_Div</title>
            <style>
            body {
                font-family: helvetica;
                margin-left: 20px;
            }
     
            #global {
                margin: 50px;
            }
     
            #dv {
                margin: 20px 0;
                padding: 10px;
                width: 400px;
                border: 1px solid #000;
                overflow: hidden;
            }
        </style>
        </head>
     
        <body>
            <div id="global">
                <div id="dv" style="display: none; max-height: 280px;">
                    <h1>contenu</h1>
                    <p>Lorem Ipsum is simply dummy text of the printing and
                        typesetting industry. Lorem Ipsum has been the
                        industry s standard dummy text ever since the 1500s, when an
                        unknown printer took a galley of type and
                        scrambled it to make a type specimen book. It has survived
                        not only five centuries, but also the leap
                        into electronic typesetting, remaining essentially
                        unchanged. It was popularised in the 1960s with the
                        release of Letraset sheets containing Lorem Ipsum passages,
                        and more recently with desktop publishing
                        software like Aldus PageMaker including versions of Lorem
                        Ipsum</p>
                </div>
            </div>
            <a id="a" href="javascript:visibilite('dv');">Ouvrir le cadre ...</a>
     
            <script>
            var ind = osh = parseInt(document.getElementById('dv').style.maxHeight)
     
            var prg = function () {
                this.div;
                var that = this;
     
                this.actionOpen = function () {
                    if (ind < osh) {
                        ind += 2
                        that.div.style.height = ind + "px";
                        setTimeout(that.actionOpen, 10);
                    }
                }
     
                this.actionClose = function () {
                    if (ind > 0) {
                        ind -= 2;
                        that.div.style.height = ind + "px";
                        setTimeout(that.actionClose, 10);
                    } else {
                        that.div.style.display = "none"
                    }
                }
            }
     
            function ouvrir(ref) {
                console.log(ind)
                let nprg = new prg();
                nprg.div = document.getElementById(ref);
                nprg.div.style.display = "block";
                ind = 0
                nprg.actionOpen();
            }
     
            function ferme(ref) {
                console.log(ind)
                let nprg = new prg();
                nprg.div = document.getElementById(ref);
                ind = nprg.div.offsetHeight
                nprg.actionClose();
            }
     
            function visibilite(thingId) {
                let targetElement = document.getElementById(thingId),
                openclose = document.getElementById('a')
     
                openclose.innerText == 'Ouvrir le cadre ...' ? [
                    openclose.innerText = 'Fermer le cadre ...',
                    ouvrir('dv')
                ] :
                    [openclose.innerText = 'Ouvrir le cadre ...',
                    ferme('dv')
                    ]
            }
        </script>
     
        </body>
     
    </html>

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Juillet 2004
    Messages
    100
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2004
    Messages : 100
    Par défaut
    Bonjour ASCIIDEFOND,

    Cela fonctionne bien. Merci pour votre réponse et votre rapidité.
    Si je peux me permettre ... est-il possible de faire en sorte que l'on puisse refermer la DIV en cliquant sur le lien pendant son ouverture et à l'inverse rouvrir la DIV en cliquant sur le lien pendant sa fermeture ?

  4. #4
    Membre confirmé
    Profil pro
    Inscrit en
    Juillet 2004
    Messages
    100
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2004
    Messages : 100
    Par défaut
    Comment faire également pour ouvrir des DIV différentes sur la même page sans réécrire les fonctions javascript à chaque fois ?

    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
    <!doctype html>
    <html lang="en">
     
        <head>
            <meta charset="utf-8" />
            <title>Progressiv_Div</title>
            <style>
            body {
                font-family: helvetica;
                margin-left: 20px;
            }
     
            #global {
                margin: 50px;
            }
     
            #dv {
                margin: 20px 0;
                padding: 10px;
                width: 400px;
                border: 1px solid #000;
                overflow: hidden;
            }
        </style>
        </head>
     
        <body>
        	<a id="a" href="javascript:visibilite('dv');">Ouvrir le cadre ...</a><br/>
            <a id="b" href="javascript:visibilite('dv2');">Ouvrir le cadre ...</a>
            <div id="global">
                <div id="dv" style="display: none; max-height: 280px;">
                    <h1>contenu 1</h1>
                    <p>Lorem Ipsum is simply dummy text of the printing and
                        typesetting industry. Lorem Ipsum has been the
                        industry s standard dummy text ever since the 1500s, when an
                        unknown printer took a galley of type and
                        scrambled it to make a type specimen book. It has survived
                        not only five centuries, but also the leap
                        into electronic typesetting, remaining essentially
                        unchanged. It was popularised in the 1960s with the
                        release of Letraset sheets containing Lorem Ipsum passages,
                        and more recently with desktop publishing
                        software like Aldus PageMaker including versions of Lorem
                        Ipsum</p>
                </div>
                <div id="dv2" style="display: none; max-height: 280px;">
                    <h1>contenu 2</h1>
                    <p>Lorem Ipsum is simply dummy text of the printing and
                        typesetting industry. Lorem Ipsum has been the
                        industry s standard dummy text ever since the 1500s, when an
                        unknown printer took a galley of type and
                        scrambled it to make a type specimen book. It has survived
                        not only five centuries, but also the leap
                        into electronic typesetting, remaining essentially
                        unchanged. It was popularised in the 1960s with the
                        release of Letraset sheets containing Lorem Ipsum passages,
                        and more recently with desktop publishing
                        software like Aldus PageMaker including versions of Lorem
                        Ipsum</p>
                </div>
     
            </div>
            <script>
            var ind = osh = parseInt(document.getElementById('dv').style.maxHeight)
     
            var prg = function () {
                this.div;
                var that = this;
     
                this.actionOpen = function () {
                    if (ind < osh) {
                        ind += 2
                        that.div.style.height = ind + "px";
                        setTimeout(that.actionOpen, 10);
                    }
                }
     
                this.actionClose = function () {
                    if (ind > 0) {
                        ind -= 2;
                        that.div.style.height = ind + "px";
                        setTimeout(that.actionClose, 10);
                    } else {
                        that.div.style.display = "none"
                    }
                }
            }
     
            function ouvrir(ref) {
                console.log(ind)
                let nprg = new prg();
                nprg.div = document.getElementById(ref);
                nprg.div.style.display = "block";
                ind = 0
                nprg.actionOpen();
            }
     
            function ferme(ref) {
                console.log(ind)
                let nprg = new prg();
                nprg.div = document.getElementById(ref);
                ind = nprg.div.offsetHeight
                nprg.actionClose();
            }
     
            function visibilite(thingId) {
                let targetElement = document.getElementById(thingId),
                openclose = document.getElementById('a')
     
                openclose.innerText == 'Ouvrir le cadre ...' ? [
                    openclose.innerText = 'Fermer le cadre ...',
                    ouvrir('dv')
                ] :
                    [openclose.innerText = 'Ouvrir le cadre ...',
                    ferme('dv')
                    ]
            }
        </script>
     
        </body>
     
    </html>

    Merci pour votre aide,

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

    Informations professionnelles :
    Activité : Autodidacte

    Informations forums :
    Inscription : Novembre 2002
    Messages : 235
    Par défaut
    Citation Envoyé par zouetchou Voir le message
    ...est-il possible de faire en sorte que l'on puisse refermer la DIV en cliquant sur le lien pendant son ouverture et à l'inverse rouvrir la DIV en cliquant sur le lien pendant sa fermeture ?
    Pour ça ce n'est pas encore au point, je cherche.

    Sinon pour plusieurs DIV différentes sur le même page.
    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
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    <!doctype html>
    <html lang="en">
     
    <head>
        <meta charset="utf-8" />
        <title>Progressiv_Div</title>
        <style>
            body {
                font-family: helvetica;
                margin-left: 20px;
            }
     
            #global {
                display: grid;
                grid-template-columns: repeat(3, 1Fr);
            }
     
            .dv {
                position: relative;
                padding: 10px;
                width: 400px;
                border: 1px solid #000;
                overflow: hidden;
                height: 0px;
                margin: 0 auto;
            }
     
            .parent-dv {
                height: 340px;
            }
     
            .bouton {
                width: max-content;
                height: 30px;
                font-weight: bold;
                margin: 0 auto;
                display: block;
                border: none;
                background-color: transparent;
                cursor: pointer;
                color: #32bbf2;
                font-size: 1em;
            }
        </style>
    </head>
     
    <body>
        <div id="global">
            <div class="parent-dv">
                <div class="dv" style="display: none; max-height: 280px;">
                    <h1>contenu #1</h1>
                    <p>Lorem Ipsum is simply dummy text of the printing and
                        typesetting industry. Lorem Ipsum has been the
                        industry s standard dummy text ever since the 1500s,
                        when an
                        unknown printer took a galley of type and
                        scrambled it to make a type specimen book. It has
                        survived
                        not only five centuries, but also the leap
                        into electronic typesetting, remaining essentially
                        unchanged. It was popularised in the 1960s with the
                        release of Letraset sheets containing Lorem Ipsum
                        passages,
                        and more recently with desktop publishing
                        software like Aldus PageMaker including versions of
                        Lorem
                        Ipsum</p>
                </div>
                <button class="bouton" onclick="action(this)">Ouvrir le cadre
                    ...</button>
            </div>
     
            <div class="parent-dv">
                <div class="dv" style="display: none; max-height: 280px;">
                    <h1>contenu #2</h1>
                    <p>Lorem Ipsum is simply dummy text of the printing and
                        typesetting industry. Lorem Ipsum has been the
                        industry s standard dummy text ever since the 1500s,
                        when an
                        unknown printer took a galley of type and
                        scrambled it to make a type specimen book. It has
                        survived
                        not only five centuries, but also the leap
                        into electronic typesetting, remaining essentially
                        unchanged. It was popularised in the 1960s with the
                        release of Letraset sheets containing Lorem Ipsum
                        passages,
                        and more recently with desktop publishing
                        software like Aldus PageMaker including versions of
                        Lorem
                        Ipsum</p>
                </div>
                <button class="bouton" onclick="action(this)">Ouvrir le cadre
                    ...</button>
            </div>
            <div class="parent-dv">
                <div class="dv" style="display: none; max-height: 280px;">
                    <h1>contenu #3</h1>
                    <p>Lorem Ipsum is simply dummy text of the printing and
                        typesetting industry. Lorem Ipsum has been the
                        industry s standard dummy text ever since the 1500s,
                        when an
                        unknown printer took a galley of type and
                        scrambled it to make a type specimen book. It has
                        survived
                        not only five centuries, but also the leap
                        into electronic typesetting, remaining essentially
                        unchanged. It was popularised in the 1960s with the
                        release of Letraset sheets containing Lorem Ipsum
                        passages,
                        and more recently with desktop publishing
                        software like Aldus PageMaker including versions of
                        Lorem
                        Ipsum</p>
                </div>
                <button class="bouton" onclick="action(this)">Ouvrir le cadre
                    ...</button>
            </div>
        </div>
     
        <script>
            let mesBoutons = document.querySelectorAll('.bouton'),
                mesDivs = document.querySelectorAll('.dv'), timer
     
            var prg = function () {
                this.div;
                var that = this;
     
                this.actionOpen = function () {
                    if (that.div.ind < that.div.osh) {
                        that.div.ind += 2
                        that.div.style.height = that.div.ind + "px";
                        timer = setTimeout(that.actionOpen, 10);
                    }
                }
     
                this.actionClose = function () {
                    if (that.div.ind > 0) {
                        that.div.ind -= 2;
                        that.div.style.height = that.div.ind + "px";
                        timer = setTimeout(that.actionClose, 10);
                    } else {
                        that.div.style.display = "none"
                    }
                }
            }
     
            function ouvrir(ref) {
                let nprg = new prg()
                nprg.div = ref
                nprg.div.osh = parseInt(nprg.div.style.maxHeight)
                nprg.div.ind = 0
                nprg.div.style.display = "block";
                nprg.actionOpen();
     
            }
     
            function ferme(ref, e) {
                let nprg = new prg();
                nprg.div = ref
                nprg.div.ind = nprg.div.offsetHeight
                nprg.actionClose();
            }
     
            function action(bouton) { // Bouton cliqué
                let parentDiv = bouton.parentElement.children[0]  // DIV correspondante
                bouton.innerText == 'Ouvrir le cadre ...' ? [
                    bouton.innerText = 'Fermer le cadre ...',
                    ouvrir(parentDiv)
                ] :
                    [bouton.innerText = 'Ouvrir le cadre ...',
                    ferme(parentDiv)
                    ]
            }
     
        </script>
     
    </body>
     
    </html>

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

    Informations professionnelles :
    Activité : Autodidacte

    Informations forums :
    Inscription : Novembre 2002
    Messages : 235
    Par défaut
    Voilà tout fonctionne. J'ai déplacé les boutons en haut des div pour faciliter les cliques pour les ouvertures et fermetures.
    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
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    <!doctype html>
    <html lang="en">
     
    <head>
        <meta charset="utf-8" />
        <title>Progressiv_Div</title>
        <style>
            body {
                font-family: helvetica;
                margin-left: 20px;
            }
     
            #global {
                display: grid;
                grid-template-columns: repeat(3, 1Fr);
                gap: 1em;
            }
     
            .dv {
                position: relative;
                padding: 10px;
                width: 400px;
                border: 1px solid #000;
                overflow: hidden;
                margin: 0 auto;
            }
     
            .parent-dv {
                height: 340px;
            }
     
            .bouton {
                width: max-content;
                height: 30px;
                font-weight: bold;
                margin: 0 auto;
                display: block;
                border: none;
                background-color: transparent;
                cursor: pointer;
                color: #32bbf2;
                font-size: 1em;
            }
        </style>
    </head>
     
    <body>
        <div id="global">
            <div class="parent-dv">
                <button class="bouton">Ouvrir le cadre...</button>
                <div class="dv" style="display: none; max-height: 280px;">
                    <h1>contenu #1</h1>
                    <p>Lorem Ipsum is simply dummy text of the printing and
                        typesetting industry. Lorem Ipsum has been the
                        industry s standard dummy text ever since the 1500s,
                        when an
                        unknown printer took a galley of type and
                        scrambled it to make a type specimen book. It has
                        survived
                        not only five centuries, but also the leap
                        into electronic typesetting, remaining essentially
                        unchanged. It was popularised in the 1960s with the
                        release of Letraset sheets containing Lorem Ipsum
                        passages,
                        and more recently with desktop publishing
                        software like Aldus PageMaker including versions of
                        Lorem
                        Ipsum</p>
                </div>
            </div>
            <div class="parent-dv">
                <button class="bouton">Ouvrir le cadre...</button>
                <div class="dv" style="display: none; max-height: 280px;">
                    <h1>contenu #2</h1>
                    <p>Lorem Ipsum is simply dummy text of the printing and
                        typesetting industry. Lorem Ipsum has been the
                        industry s standard dummy text ever since the 1500s,
                        when an
                        unknown printer took a galley of type and
                        scrambled it to make a type specimen book. It has
                        survived
                        not only five centuries, but also the leap
                        into electronic typesetting, remaining essentially
                        unchanged. It was popularised in the 1960s with the
                        release of Letraset sheets containing Lorem Ipsum
                        passages,
                        and more recently with desktop publishing
                        software like Aldus PageMaker including versions of
                        Lorem
                        Ipsum</p>
                </div>
            </div>
            <div class="parent-dv">
                <button class="bouton">Ouvrir le cadre...</button>
                <div class="dv" style="display: none; max-height: 280px;">
                    <h1>contenu #3</h1>
                    <p>Lorem Ipsum is simply dummy text of the printing and
                        typesetting industry. Lorem Ipsum has been the
                        industry s standard dummy text ever since the 1500s,
                        when an
                        unknown printer took a galley of type and
                        scrambled it to make a type specimen book. It has
                        survived
                        not only five centuries, but also the leap
                        into electronic typesetting, remaining essentially
                        unchanged. It was popularised in the 1960s with the
                        release of Letraset sheets containing Lorem Ipsum
                        passages,
                        and more recently with desktop publishing
                        software like Aldus PageMaker including versions of
                        Lorem
                        Ipsum</p>
                </div>
            </div>
        </div>
     
        <script>
            let prg, timer = [], nbPx = 4
     
            prg = function (x) { //x est la valeur du data-index de la div
                let that = this
     
                that.actionOpen = function () {
                    if (that.div.ind < that.div.osh) {
                        that.div.ind += nbPx
                        that.div.style.height = `${that.div.ind}px`
                        timer[x] = setTimeout(that.actionOpen, 10)
                    }
                }
     
                that.actionClose = function () {
                    if (that.div.ind > 0) {
                        that.div.ind -= nbPx
                        that.div.style.height = `${that.div.ind}px`
                        timer[x] = setTimeout(that.actionClose, 10)
                    } else {
                        that.div.style.display = "none"
                    }
                }
            }
     
            function ouvrir(ref, inDx) {
                clearTimeout(timer[inDx]) // stop le setTimeout en cours sur le div sélectionnée
                let nprg = new prg(inDx)
                nprg.div = ref
                nprg.div.osh = parseInt(nprg.div.style.maxHeight)
                nprg.div.ind = ref.offsetHeight
                nprg.div.style.display = "block";
                nprg.actionOpen();
            }
     
            function fermer(ref, inDx) {
                clearTimeout(timer[inDx])
                let nprg = new prg(inDx);
                nprg.div = ref
                nprg.div.ind = nprg.div.offsetHeight
                nprg.actionClose();
            }
     
            document.querySelectorAll('.bouton').forEach((boutonSelect, index) => {
                boutonSelect.addEventListener('click', () => {
                    let parentDiv = boutonSelect.parentElement.children[1]
                    boutonSelect.innerText == 'Ouvrir le cadre...' ? [
                        boutonSelect.innerText = 'Fermer le cadre...',
                        ouvrir(parentDiv, index)
                    ] :
                        [boutonSelect.innerText = 'Ouvrir le cadre...',
                        fermer(parentDiv, index)
                        ]
                })
            })
     
        </script>
     
    </body>
     
    </html>

  7. #7
    Membre actif
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2015
    Messages
    44
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Janvier 2015
    Messages : 44
    Par défaut Oui mais...
    Oui, "ça marche" comme on dit. Je vois cependant plusieurs problèmes :
    - C'est JavaSript qui pilote l'animation, donc aucune chance de pouvoir modifier l'animation sans toucher au script.
    - La hauteur du contenu est fixe elle est définie en dur. Si j'ajoute du contenu, ou si je traduis à la volée dans une langue plus verbeuse que la nôtre, je perds du contenu.
    - L'accessibilité est absente.

    Plusieurs solutions :
    - S'en tenir au HTML avec Details/Summary (exemple : Details/Summary). Pour du basique ça reste la meilleure option, car de loin la plus légère puisque native.
    - Utiliser un framework tel que Bootstrap pour ne pas avoir de surprises (Bootstrap, Accordion)
    - Tout coder sois-même reste une option, mais le faire correctement prend du temps (je l'ai fais ici : Scriptura, Accordions). Si vous faites cette option, bon courage car ça prend du temps, mais si vous suivez cette voie vous allez apprendre beaucoup de choses.

  8. #8
    Membre confirmé
    Profil pro
    Inscrit en
    Juillet 2004
    Messages
    100
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2004
    Messages : 100
    Par défaut
    Ai je dis une bêtise ? Il aurait fallu que texte1 s'affiche dans cadre 1, texte2 dans cadre 2...etc.
    Comment faire en l'état ?

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

    Informations professionnelles :
    Activité : Autodidacte

    Informations forums :
    Inscription : Novembre 2002
    Messages : 235
    Par défaut
    Pour changer les titres et les textes.
    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
    ...
    <button class="bouton">Ouvrir le cadre...</button>
      <div class="dv">
        <h1>Ici un titre</h1>
        <p>Le texte</p>
      </div>
    
      <button class="bouton">Ouvrir le cadre...</button>
      <div class="dv">
        <h1>Ici un titre</h1>
        <p>Le texte</p>
      </div>
    
      <button class="bouton">Ouvrir le cadre...</button>
      <div class="dv">
        <h1>Ici un titre</h1>
        <p>Le texte</p>
      </div>
    ...
      <script>
    
    //Supprimer dans le script tout ce qui est en rouge
        let prg, timer = [], heights = [],  
    texte = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.Lorem Ipsum has been the industry s standard dummy text ever since the 1500s, when an  unknown printer took a galley of type and scrambled it to make a type specimen book.It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum"
    ...
    
        for (let baliseP of document.querySelectorAll('div > p')) {
          baliseP.innerText = texte
        }
    ...
    </script>

    Et si tu veux rajouter du texte, il y a juste à ajouter ces lignes dans le html.
    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
      <button class="bouton">Ouvrir le cadre...</button>
      <div class="dv">
        <h1>Ici un titre</h1>
        <p>Le texte</p>
      </div>

  10. #10
    Membre confirmé
    Profil pro
    Inscrit en
    Juillet 2004
    Messages
    100
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2004
    Messages : 100
    Par défaut
    Bonjour,

    j'ai testé en suppriment directement les informations liées à la variable 'texte' et cela fonctionne parfaitement ...

    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
    136
    137
    138
    139
    140
    141
    142
    143
    <!doctype html>
    <html lang="en"> 
    <head>
      <meta charset="utf-8" />
      <title>Progressiv_Div</title>
      <style>
        * {
          box-sizing: border-box;
        }
     
        body {
          background-color: #383737;
        }
            
             .conteneur{
                    display:inline-block;
                    vertical-align:top;
             }
             
        .dv {
          transition: height .80s ease-in;
          overflow: hidden;
          max-width: 480px;
          border: 1px solid grey;
          font-size: 1em;
          color: #ffffff;
        }
     
        h1 {
          margin: 0 0 0 10px;
        }
     
        p {
          padding: 10px;
        }
     
        .dv:not(.active) {
          display: none;
        }
     
        .disabled {
          pointer-events: none;
          opacity: 0.2;
        }
     
        .bouton {
              width: 500px;
          height: 30px;
          font-weight: bold;
              text-align:left;
     
          border: none;
          background-color: transparent;
          cursor: pointer;
          color: #0399d4;
          font-size: 1em;
        }
            
            @media screen and (max-width: 1000px) {
                    .dv,.bouton{
                            max-width: 350px;
                    }
            }
      </style>
    </head>
     
    <body>
     <div class="conteneur">
      <button class="bouton">Ouvrir le cadre...</button>
      <div class="dv">
        <h1>contenu #1</h1>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.Lorem Ipsum has been the industry s standard dummy text ever <strong>since the 1500s</strong>, when an  unknown printer took a galley of type and scrambled it to make a type specimen book.It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
      </div>
     </div>
      <div class="conteneur">
      <button class="bouton">Ouvrir le cadre...</button>
      <div class="dv">
        <h1>contenu #2</h1>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.Lorem Ipsum has been the industry s standard dummy text ever since the 1500s, when an  unknown printer took a galley of type and scrambled it to make a type specimen book.It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.Lorem Ipsum has been the industry s standard dummy text ever since the 1500s, when an  unknown printer took a galley of type and scrambled it to make a type specimen book.It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
      </div>
     </div>
      <div class="conteneur">
      <button class="bouton">Ouvrir le cadre...</button>
      <div class="dv">
        <h1>contenu #3</h1>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.Lorem Ipsum has been the industry s standard dummy text ever since the 1500s, when an  unknown printer took a galley of type and scrambled it to make a type specimen book.It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.Lorem Ipsum has been the industry s standard dummy text ever since the 1500s, when an  unknown printer took a galley of type and scrambled it to make a type specimen book.It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.Lorem Ipsum has been the industry s standard dummy text ever since the 1500s, when an  unknown printer took a galley of type and scrambled it to make a type specimen book.It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
      </div>
     </div>
      <script>
        let prg, timer = [], heights = []
     
        prg = function (x) {
          let that = this
     
          that.openClose = function () {
     
            if (!that.div.classList.contains('active')) {
              that.div.classList.add('active')
              that.bout.innerText = 'Fermer le cadre...'
              that.div.style.height = 'auto'
     
              heights[x] = `${that.div.clientHeight}px`
              that.div.style.height = '0px'
     
              timer[x] = setTimeout(function () {
                that.div.style.height = heights[x]
              }, 0)
            } else {
              that.bout.classList.add('disabled')
              that.div.style.height = '0px'
     
              that.div.addEventListener('transitionend', function () {
                that.div.classList.remove('active')
                that.bout.classList.remove('disabled')
                that.bout.innerText = 'Ouvrir le cadre...'
              }, {
                once: true
              })
            }
          }
        }
     
        document.querySelectorAll('.bouton').forEach((boutonSelect, index) => {
          boutonSelect.addEventListener('click', function (event) {
            event.preventDefault();
     
            let nprg = new prg(index)
            nprg.div = document.querySelectorAll('.dv')[index]
            nprg.bout = boutonSelect
            nprg.openClose(index)
          })
        })
     
        onresize = (event) => {
          for (let divs of document.querySelectorAll('.dv')) {
            divs.style.height = 'auto';
            divs.style.height = divs.clientHeight + 'px'
          }
        }
      </script>
     
    </body>
    </html>

    il a fallu supprimer ces lignes car le texte html dans les DIV était remplacé par le texte de la variable 'texte ='
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
       for (let baliseP of document.querySelectorAll('div > p')) {
          baliseP.innerHTML = texte
        }
    Merci beaucoup ASCIIDEFOND pour votre aide et votre disponibilité

  11. #11
    Membre confirmé
    Profil pro
    Inscrit en
    Juillet 2004
    Messages
    100
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2004
    Messages : 100
    Par défaut
    dans la serie tu avances d'un pas tu recules de deux ...

    j'ai maintenant cette erreur
    <b>Notice</b>: Undefined variable: _that_i in <b>/home/dith1146/public_html/includes/fonctions_tpl.php(223) : eval()'d code</b> on line <b>186</b><br />
    <br />
    <b>Notice</b>: Undefined variable: _div_i in <b>/home/dith1146/public_html/includes/fonctions_tpl.php(223) : eval()'d code</b> on line <b>186</b><br />
    lorsque j'utilise le code Javascript sur mon site en construction...

    Il semble y avoir un conflit avec une fonction utilisée par le système de template que j'utilise (phpBB)

Discussions similaires

  1. Fermeture d'une div lisant une vidéo sous ie
    Par Madoka dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 10/07/2009, 12h17
  2. fermeture d'une pop-up SANS javascript
    Par sarah65536 dans le forum Balisage (X)HTML et validation W3C
    Réponses: 21
    Dernier message: 18/05/2009, 14h15
  3. Réponses: 6
    Dernier message: 09/10/2008, 19h43
  4. Réponses: 2
    Dernier message: 30/09/2008, 14h18
  5. ouverture/fermeture d'une fenêtre modale showModelessDialog
    Par marti dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 04/01/2006, 05h03

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