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
| <!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>animationend et animationiteration</title>
<meta name="Author" content="NoSmoking">
<meta name="DVP-discussion" content="d2163647">
<meta name="category" content="css,js">
<meta name="description" content="Utilisation animationend et animationiteration au lieu de setTimeout">
<style>
html, body {margin: 0;padding: 0;font: 1em/1.5 Verdana,sans-serif;}
h1, h2, h3 {margin: .25em 0;color: #069;}
#barre-defilement {
z-index: 1;
width: 0;
height: 1em;
background-color: #069;
animation: left-to-right 3s linear infinite 0s;
}
#barre-defilement.stop-animation {
animation-play-state: paused;
}
@keyframes left-to-right {
from {width: 0;}
to {width: 100%;}
}
#wrapper > * {
display: none;
margin: auto;
max-width: 30em;
text-align: center;
}
#wrapper > *[aria-hidden="false"] {
display: block;
}
button svg {
width: 100%;
height: 100%;
}
#btn-stop-animation {
z-index: 1;
position: fixed;
top: 2px;
right: 2px;
width: 5em;
border: 1px solid #888;
overflow: hidden;
font-size: 1em;
line-height: 0;
background-color: #FFF8;
cursor: pointer;
aspect-ratio: 1 /1;
}
#btn-stop-animation:hover {
background-color: #FFF;
}
#btn-stop-animation.play-animation svg:first-child {
display: none;
}
</style>
<div id="barre-defilement"></div>
<button id="btn-stop-animation">
<svg><use href="#icon-pause"></use></svg>
<svg><use href="#icon-play"></use></svg>
</button>
<div id="wrapper">
<div aria-hidden="false">
<h1>Titre #1</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Vestibulum ultricies laoreet orci, sit amet molestie nulla efficitur ac.</p>
</div>
<div>
<h1>Titre #2</h1>
<p>Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</p>
<p>Maecenas tellus elit, suscipit at laoreet et, tempor a orci.</p>
</div>
<div>
<h1>Titre #3</h1>
<p>Nulla in tortor nec est placerat condimentum in nec magna.</p>
<p>Nunc sollicitudin sem eget purus ultrices, bibendum pharetra augue aliquet.</p>
</div>
<div>
<h1>Titre #4</h1>
<p>Mauris consectetur lectus ut laoreet finibus. In gravida, massa eget tempus fringilla, eros nisi gravida massa, at accumsan massa nisl vel quam.</p>
<p>Donec maximus molestie enim, ac elementum nisi posuere a. Vivamus eu varius erat.</p>
</div>
</div>
<script>
"use strict";
const elemDisplay = [...document.querySelectorAll("#wrapper > *")]; // children
const elemAvance = document.getElementById("barre-defilement");
elemAvance.addEventListener("animationiteration", (e) => {
elemDisplay[0].setAttribute("aria-hidden", true);
elemDisplay.push(elemDisplay.shift());
elemDisplay[0].setAttribute("aria-hidden", false)
});
const btnStop = document.getElementById("btn-stop-animation");
btnStop.addEventListener("click", (e) => {
btnStop.classList.toggle("play-animation");
elemAvance.classList.toggle("stop-animation");
});
</script>
<svg style="display:none">
<symbol id="icon-pause" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M5 4h3v12H5V4Zm7 0h3v12h-3V4Z"/></symbol>
<symbol id="icon-play" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="m5 4 12 6-12 6z"/></symbol>
</svg>
</body>
</html> |
Partager