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
| <!DOCTYPE html>
<html lang="fr" dir="ltr">
<head>
<meta charset="utf-8">
<meta name="Author" content="Daniel Hagnoul">
<title>Forum jQuery</title>
<style>
/* BASE */
body {
background-color:#dcdcdc;
color:#000000;
font-family:sans-serif;
font-size:medium;
font-style:normal;
font-weight:normal;
line-height:normal;
letter-spacing:normal;
}
h1,h2,h3,h4,h5 {
font-family:serif;
}
div,p,h1,h2,h3,h4,h5,h6,ul,ol,dl,form,table,img {
margin:0px;
padding:0px;
}
p {
padding:6px;
}
ul,ol,dl {
list-style:none;
padding-left:6px;
padding-top:6px;
}
li {
padding-bottom:6px;
}
/* dvjh */
h1 {
text-align:center;
font-style:italic;
text-shadow: 4px 4px 4px #bbbbbb;
}
h2 {
text-align:center;
}
div#conteneur {
width:95%;
min-height:800px;
margin:12px auto;
padding:6px;
background-color:#FFFFFF;
color:#000000;
border:1px solid #666666;
}
/* TEST */
#randImgID {
margin: 12px;
border:0px;
}
</style>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script>
// retourne X tel que : min <= X <= max
function intRandom( min, max ){
return Math.floor( Math.random() * ( max - min + 1 ) + min );
}
// retourne un array dans un ordre aléatoire
function brouille( tab ){
var pos,
temp;
for ( var j = tab.length - 1; j > 0; j-- ){
pos = intRandom( 0, j + 1 );
temp = tab[ j ];
tab[ j ] = tab[ pos ];
tab[ pos ] = temp;
}
return tab;
}
var fadingDelay = 1000,
setTimeoutDelay = 2000,
images = [],
taille,
n,
dataset = [
"http://danielhagnoul.developpez.com/images/imageTest.png",
"http://danielhagnoul.developpez.com/images/boule1.png",
"http://danielhagnoul.developpez.com/images/boule2.png",
"http://danielhagnoul.developpez.com/images/boule3.png",
"http://danielhagnoul.developpez.com/images/boule4.png",
"http://danielhagnoul.developpez.com/images/boule5.png",
"http://danielhagnoul.developpez.com/images/boule6.png",
"http://danielhagnoul.developpez.com/images/boule7.png",
"http://danielhagnoul.developpez.com/images/canvas.png",
"http://danielhagnoul.developpez.com/images/canvas4.png",
"http://danielhagnoul.developpez.com/images/canvas6.png",
"http://danielhagnoul.developpez.com/images/canvas7.png",
"http://danielhagnoul.developpez.com/images/avatarDVJH.jpg"
];
function randFadeOut(){
$( "#randImgID" ).fadeOut( fadingDelay, function(){
$( this ).attr( "src", $( images[ n ] ).attr( "src" ) );
randFadeIn()
});
}
function randFadeIn(){
$( "#randImgID" ).fadeIn( fadingDelay, function(){
if ( n < 1 ){
n = taille;
// mélange aléatoire des images
brouille( images );
} else {
--n;
}
setTimeout( randFadeOut, setTimeoutDelay );
});
}
$( function(){
$.each( dataset, function( i, item ){
images.push( new Image() );
$( images[ i ] ).attr( "src", item ).load();
});
});
$( window ).load( function(){
// mélange aléatoire des images
brouille( images );
taille = images.length - 1;
n = taille;
$( "#randImgID" ).fadeOut( fadingDelay, function(){
$( this ).attr( "src", $( images[ n ] ).attr( "src" ) );
randFadeIn();
});
});
</script>
</head>
<body>
<h1>Forum jQuery</h1>
<div id="conteneur">
<h2>Affichage perpétuel d'un lot d'images. Dans un ordre aléatoire avec brouille().</h2>
<div>
<img id="randImgID" src="http://danielhagnoul.developpez.com/images/load.gif">
</div>
</div>
</body>
</html> |