Javascript slideshow in random order -
my slideshow displays images in non-random order @ moment, i'd random.
jsfiddle:
javascript:
function loadnextimage(userandomselection) { if(imagepool.length > 0) { if(userandomselection) { var randomindex = math.floor(math.random() * imagepool.length); var imgdata = imagepool.splice(randomindex,1); } else { var imgdata = imagepool.shift(); } $(".back-image-container").append("<img id='back-image-" + imgdata.id + "' class='back-image' src='" + imgdata.url + "'/>"); var imgobj = $("#back-image-" + imgdata.id); imgobj.hide(); if ( imgobj.complete || imgobj.readystate == 4 || imgobj.readystate == "complete") { loadcompleted(imgobj,userandomselection); } else { imgobj.load(function() { loadcompleted(imgobj,userandomselection); }); } } }
html:
<div class="back-image-container"> <img id="back-image-0" class="back-image" src="http://cdn2.bigcommerce.com/n-pktq5q/b2aus7wd/product_images/theme_images/scorpiongoldberlin.jpg?t=1403886381" style="width: 1347px; height: 898px; left: -63px; display: inline;"> <img id="back-image-1" class="back-image" src="http://cdn2.bigcommerce.com/n-pktq5q/b2aus7wd/product_images/theme_images/brelingirlcross2.jpg?t=1403886381" style="width: 1347px; height: 898px; left: -63px; display: none;"> <img id="back-image-2" class="back-image" src="http://cdn2.bigcommerce.com/n-pktq5q/b2aus7wd/product_images/theme_images/brelinafternoon.jpg?t=1403886381" style="width: 1347px; height: 898px; left: -63px; display: none;"> </div>
i don't think problem in loadnextimage
, way called. notice loadnextimage
has parameter defines if order should random or not.
in fiddle call loadnextimage
done such:
loadnextimage(false);
this means random behavior bypassed. try inverting boolean value:
loadnextimage(true);
also, line seems have error:
var imgdata = imagepool.splice(randomindex,1);
splice returns array of results removed, because there can more one. in case returning array single item, image data shifted. fix reference need first element of array:
var imgdata = imagepool.splice(randomindex,1)[0];
with these changes slide functions correctly.
Comments
Post a Comment