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
| <!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Animation CSS simple</title>
<meta name="Author" content="NoSmoking">
<style>
html, body {
margin: 0;
padding: 0;
font: 1em/1.25 Verdana,sans-serif;
background: #FFF;
}
#main {
margin: auto;
max-width: 60em;
}
h1, h2, h3 {
margin: .25em 0;
color: #069;
}
section {
margin: 0 1em 2em;
}
button {
min-width: 10em;
font: inherit;
cursor: pointer;
}
button.on:before {
content: "Affiche ";
}
button.off:before {
content: "Masque ";
}
[id^=cadre] {
position: relative;
width: 12.5em;
height: 12em;
border: 1px solid #ccc;
overflow: hidden;
}
[id^=cadre] img {
position: absolute;
right: -100%;
transition: all .5s;
}
#cadre-1:hover img,
#cadre-2 img.visible {
right: 0;
}
#cadre-3 img.show-hide {
animation: show-hide 1s linear;
}
@keyframes show-hide {
0% {
right: -100%;
}
40% {
right: 0;
}
60% {
right: 0;
}
100% {
right: -100%;
}
}
code {
color : #00F;
color: #217ac0;
font: inherit;
}
</style>
</head>
<body>
<div id="main">
<h1>Animation CSS simple</h1>
<section>
<h2>Exemple au survol</h2>
<div id="cadre-1">
<img src="http://club.developpez.com/webdesign/Rubriques/Web/CSS/logo_outils_css.gif" alt="">
</div>
</section>
<section>
<h2>Exemple sur action</h2>
<div id="cadre-2">
<img src="http://club.developpez.com/webdesign/Rubriques/Web/CSS/logo_outils_css.gif" alt="">
</div>
<p><button class="on">image</button></p>
</section>
<section>
<h2>Exemple va et vient sur action</h2>
<div id="cadre-3">
<img src="http://club.developpez.com/webdesign/Rubriques/Web/CSS/logo_outils_css.gif" alt="">
</div>
<p><button>show-hide</button></p>
</section>
</div>
<script>
var oBtns = document.querySelectorAll('button');
var oImgs = document.querySelectorAll('img');
// ajout/suppression de la class au click ou autre
oBtns[0].addEventListener('click', function () {
this.classList.toggle('on');
this.classList.toggle('off');
oImgs[1].classList.toggle('visible');
});
// ajout de la class au click ou autre action
oBtns[1].addEventListener('click', function (e) {
oImgs[2].classList.add('show-hide');
});
// supprime la class en fin d'animation
oImgs[2].addEventListener('animationend', function (e) {
this.classList.remove('show-hide');
});
</script>
</body>
</html> |
Partager