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 :

delay et duration aléatoires dans un slideshow


Sujet :

jQuery

Vue hybride

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

    Informations forums :
    Inscription : Juillet 2008
    Messages : 32
    Par défaut delay et duration aléatoires dans un slideshow
    bonjour,

    je souhaiterais faire un slideshow dont le delay et la duration prennent des valeurs aléatoires comprises entre par exemple 500 et 2000 ms.

    je ne vois pas comment faire...
    (j'ai tenté des recherches, mais je tombe toujours sur des sujets traitant d'images aléatoires...)

    quelqu'un aurait une idée?

    merci d'avance!

  2. #2
    Modérateur

    Avatar de NoSmoking
    Homme Profil pro
    Inscrit en
    Janvier 2011
    Messages
    17 209
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2011
    Messages : 17 209
    Par défaut
    Bonjour,
    quelqu'un aurait une idée?
    renseigne toi sur la méthode qui gère l'aléatoire

  3. #3
    Rédacteur

    Avatar de danielhagnoul
    Homme Profil pro
    Étudiant perpétuel
    Inscrit en
    Février 2009
    Messages
    6 389
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant perpétuel
    Secteur : Enseignement

    Informations forums :
    Inscription : Février 2009
    Messages : 6 389
    Billets dans le blog
    125
    Par défaut
    Bonjour

    Il suffit d'utiliser Math.random() ou un générateur de nombre aléatoire plus efficace, exemple avec Alea et une animation de la couleur de fond :

    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
    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
    <!DOCTYPE html>
    <html lang="fr" dir="ltr">
    <head>
    	<meta charset="utf-8">
    	<meta name="viewport" content="initial-scale=1.0">
    	<meta name="author" content="Daniel Hagnoul">
    	<title>Forum jQuery</title>
    	<script src="http://cdnjs.cloudflare.com/ajax/libs/headjs/0.99/head.min.js"></script>
    	<script>
    		"use strict";
     
    		head.js( 
    			"http://d3js.org/d3.v3.min.js",
    			"http://code.jquery.com/jquery-2.1.0-beta1.js",
    			"http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/i18n/jquery-ui-i18n.min.js",
    			"http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js",
    			"http://cdnjs.cloudflare.com/ajax/libs/jquery-color/2.1.2/jquery.color.min.js",
    			"http://danielhagnoul.developpez.com/lib/dvjh/d3Base.js", function(){
     
    /*
     * Usage :
     *
     * var seed = "jh56ki23";
     * var random = new Alea(seed);
     * var x = random(); // [0, 1[
     *
     * seed doit être aléatoire et différent
     * pour chaque initialisation de l'objet Alea.
     *
     * Pour un usage non critique, animation, on peut
     * se contenter de l'initialisation par défaut :
     *
     * var random = new Alea();
     */
     
    // From http://baagoe.com/en/RandomMusings/javascript/
    // Johannes Baagøe <baagoe@baagoe.com>, 2010
    function Mash() {
      var n = 0xefc8249d;
     
      var mash = function(data) {
        data = data.toString();
     
        for (var i = 0; i < data.length; i++) {
          n += data.charCodeAt(i);
          var h = 0.02519603282416938 * n;
          n = h >>> 0;
          h -= n;
          h *= n;
          n = h >>> 0;
          h -= n;
          n += h * 0x100000000; // 2^32
        }
     
        return (n >>> 0) * 2.3283064365386963e-10; // 2^-32
      };
     
      mash.version = 'Mash 0.9';
     
      return mash;
    }
     
    // From http://baagoe.com/en/RandomMusings/javascript/
    // Johannes Baagøe <baagoe@baagoe.com>, 2010
    function Alea() {
      return (function(args) {
        var s0 = 0;
        var s1 = 0;
        var s2 = 0;
        var c = 1;
     
        if (args.length == 0) {
          args = [+new Date];
        }
     
        var mash = Mash();
     
        s0 = mash(' ');
        s1 = mash(' ');
        s2 = mash(' ');
     
        for (var i = 0; i < args.length; i++) {
          s0 -= mash(args[i]);
          if (s0 < 0) {
            s0 += 1;
          }
          s1 -= mash(args[i]);
          if (s1 < 0) {
            s1 += 1;
          }
          s2 -= mash(args[i]);
          if (s2 < 0) {
            s2 += 1;
          }
        }
     
        mash = null;
     
        var random = function() {
          var t = 2091639 * s0 + c * 2.3283064365386963e-10; // 2^-32
          s0 = s1;
          s1 = s2;
     
          return s2 = t - (c = t | 0);
        };
     
        random.uint32 = function() {
          return random() * 0x100000000; // 2^32
        };
     
        random.fract53 = function() {
          return random() + (random() * 0x200000 | 0) * 1.1102230246251565e-16; // 2^-53
        };
     
        random.version = 'Alea 0.9';
        random.args = args;
     
        return random;
     
      } (Array.prototype.slice.call(arguments)));
    };
     
    $( function(){
     
    });
     
    $( window ).load( function(){
     
        var random = new Alea();
     
        // retourne X tel que : min <= X <= max
        function intRandom( min, max ){
            return Math.floor( random() * ( max - min + 1 ) + min );
        }
     
    	setInterval( function(){
    		$( "section.conteneur" ).finish().animate({ "background-color" : "#abcdef" }, intRandom( 500, 2000 ), function(){
    			$( this ).animate({ "background-color" : "#a11cdf" }, intRandom( 500, 2000 ), function(){
    				$( this ).animate({ "background-color" : "@ffffff" }, intRandom( 500, 2000 ) );
    			});
    		});
    	}, intRandom( 2000, 5000 ) );
     
    });
     
    		});
    	</script>
    	<link href='http://fonts.googleapis.com/css?family=Sofia|Ubuntu:400|Kreon'>
    	<link rel="stylesheet" href="http://danielhagnoul.developpez.com/styles/dvjhRemBase.css">
    	<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/sunny/jquery-ui.min.css">
    	<style>
    		/* TEST -- Nota bene : ici 1 rem est égal à 10 px, voir dvjhRemBase.css */
     
     
    	</style>
    </head>
    <body>
    	<header>
    		<hgroup>
    			<h1>Forum jQuery</h1>
    			<h2>
    				<a href="">Lien</a>
    			</h2>
    		</hgroup>
    	</header>
    	<section class="conteneur">
     
     
    	</section>
    	<footer itemscope itemtype="http://danielhagnoul.developpez.com/">
    		<time datetime="2013-09-21T12:05:10.119+02:00" pubdate>2013-09-21T12:05:10.119+02:00</time>
    		<span itemprop="name">Daniel Hagnoul</span>
    		<a href="http://www.developpez.net/forums/u285162/danielhagnoul/" itemprop="url">@danielhagnoul</a>
    		<a href="http://danielhagnoul.developpez.com/" itemprop="url">Mon cahier d’exercices</a>
    		<a href="http://javascript.developpez.com/faq/jquery/" itemprop="url">FAQ</a>
    		<a href="http://javascript.developpez.com/cours/?page=frameworks#jquery" itemprop="url">Tutoriels</a>
    	</footer>
    </body>
    </html>

    Blog

    Sans l'analyse et la conception, la programmation est l'art d'ajouter des bogues à un fichier texte vide.
    (Louis Srygley : Without requirements or design, programming is the art of adding bugs to an empty text file.)

  4. #4
    Membre averti
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    32
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 32
    Par défaut
    merci pour vos réponses! je vais me pencher là-dessus!

    (désolé de répondre tardivement, j'avais oublié d'activer l'email de notification de réponse...)

Discussions similaires

  1. Réponses: 6
    Dernier message: 30/12/2011, 09h35
  2. Réponses: 4
    Dernier message: 24/08/2005, 16h01
  3. [FLASH MX] Choisir un nombre aléatoire dans une liste
    Par grenatdu55 dans le forum Flash
    Réponses: 4
    Dernier message: 23/04/2005, 21h09
  4. Réponses: 4
    Dernier message: 02/11/2004, 20h11
  5. Selection aléatoire dans une fouchette de 10%
    Par RobertDeNiroZ dans le forum Requêtes
    Réponses: 4
    Dernier message: 14/06/2004, 09h22

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