Note that there are some explanatory texts on larger screens.

plurals
  1. POImage randomizer - jQuery
    primarykey
    data
    text
    <p>I am learning jQuery and I want to implement an image randomizer. </p> <p>I have a list of images that need to be displayed. I want them to be displayed randomly.</p> <p>This is what I have implemented, but it sill has not satisfied me: </p> <pre><code>/** Initialisierung*/ function showFotos() { // Nummern der Bilder im Verzeichnis 'images' start_nr = 1; end_nr = 5; // Bilder eventuell vorladen for (i=start_nr; i&lt;=end_nr; i++ ) { preload_img = new Image(); preload_img.src = 'images/' + i + '.jpg'; } // #show-1, #show-2, #show-3 bekommen einen zunächst 'leeren' img-tag mit den Dimensionen der Bilder zugefügt $('#show-1').append('&lt;img src="" width="250" height="250" /&gt;'); // Diese Function wird einmalig aufgerufen und somit Start-Bilder initialisiert randomize(); } // Random Funktion -&gt; kleinster, grösster Wert function randomXToY(minVal,maxVal,floatVal) { var randVal = minVal+(Math.random()*(maxVal-minVal)); return typeof floatVal=='undefined'?Math.round(randVal):randVal. toFixed(floatVal); } // die 'Slotmachine' function randomize() { // die fünf Slots for (i=1; i&lt;=5; i++ ) { // Zufallszahl num = randomXToY(start_nr, end_nr); // Zufallszahl = Bildnummer $('#show-' + i + ' img').attr('src', 'images/' + num + '.jpg'); } </code></pre> <p>This is what I want: </p> <p>When I click on "continue", the next image will be displayed. </p> <p>When I click on back, the further image will be displayed.</p> <p>How can I do it?</p> <p><img src="https://i.stack.imgur.com/Krp3T.jpg" alt="enter image description here"></p> <p>UPDATE!!!! I solved the problem!!!</p> <pre><code>&lt;!doctype html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;title&gt;jQuery UI Spinner - Default functionality&lt;/title&gt; &lt;link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css"&gt; &lt;script src="http://code.jquery.com/jquery-1.9.1.js"&gt;&lt;/script&gt; &lt;script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"&gt;&lt;/script&gt; &lt;style type="text/css"&gt; #items { position : relative; width : 400px; height : 200px; top : 20px; left : 20px; } .item { position : absolute; border : 1px solid #ccc; width : 398px; height : 198px; display :none; text-align : right; font-size : 40px; } .first{ display : block; } #controls { margin-top : 30px; } li { display : inline-block; padding : 5px; border : 1px solid #ccc; background-color : #eee; cursor : pointer; } #play { display : none; } .first#item1 { background-image: url(D:/images/images1.jpg); background-repeat: no-repeat; width : 398px; height : 198px; } .item#item2 { background-image: url(D:/images/images2.jpg); background-repeat: no-repeat; width : 398px; height : 198px; } .item#item3 { background-image: url(D:/images/images2.jpg); background-repeat: no-repeat; width : 398px; height : 198px; } .item#item4{ background-image: url(D:/images/images4.jpg); background-repeat: no-repeat; width : 398px; height : 198px; } .item#item5{ background-image: url(D:/images/images5.jpg); background-repeat: no-repeat; width : 398px; height : 198px; } &lt;/style&gt; &lt;script&gt; $(function() { //To store timeout id var timeoutId; var slideImage = function( step ) { if ( step == undefined ) step = 1; //Clear timeout if any clearTimeout ( timeoutId ); //Get current image's index var indx = $('.item:visible').index('.item'); //If step == 0, we don't need to do any fadein our fadeout if ( step != 0 ) { //Fadeout this item $('.item:visible').fadeOut(); } //Increment for next item indx = indx + step ; //Check bounds for next item if ( indx &gt;= $('.item').length ) { indx = 0; } else if ( indx &lt; 0 ) { indx = $('.item').length - 1; } //If step == 0, we don't need to do any fadein our fadeout if ( step != 0 ) { //Fadein next item $('.item:eq(' + indx + ')').fadeIn(); } //Set Itmeout timeoutId = setTimeout ( slideImage, 5000 ); }; //Start sliding slideImage(0); //When clicked on prev $('#prev').click(function() { //slideImage with step = -1 slideImage ( -1 ); }); //When clicked on next $('#next').click(function() { //slideImage with step = 1 slideImage ( 1 ); }); //When clicked on Pause $('#pause').click(function() { //Clear timeout clearTimeout ( timeoutId ); //Hide Pause and show Play $(this).hide(); $('#play').show(); }); //When clicked on Play $('#play').click(function() { //Start slide image slideImage(0); //Hide Play and show Pause $(this).hide(); $('#pause').show(); }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id='items'&gt; &lt;div id= 'item1' class='item first'&gt;item 1&lt;/div&gt; &lt;div id= 'item2' class='item'&gt;item2&lt;/div&gt; &lt;div id= 'item3' class='item'&gt;item3&lt;/div&gt; &lt;div id= 'item4' class='item'&gt;item4&lt;/div&gt; &lt;div id= 'item5' class='item'&gt;item5&lt;/div&gt; &lt;/div&gt; &lt;ul id='controls'&gt; &lt;li id='prev'&gt; &lt;&lt; Zurueck&lt;/li&gt; &lt;li id='next'&gt;Weiter &gt;&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>It works very good but not with fire fox...I don't know why...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload