Hi, I'm trying to get it so that a random picture displays from a gallery file one at a time. I'm probably making a basic syntax error somewhere as I'm fairly new to JavaScript/jQuery, but please can someone help? I can't get a picture to display in the div at all. Any help would be great, thanks!!
N.B. the pictures are called e.g. 101.jpg, 102.jpg - so I thought a random number generator would be best to get a random picture.
<script>
$(document).ready(function generatePic(){
function randomPicture() {
var min=101;
var max=120;
var picture=Math.floor(Math.random()*(max-min+1))+min;
}
function bannerDisplay(){
$("#banner").fadeOut('400',function(){
$("#banner").load("pages/gallery/PhotoWall/images/'$picture'.jpg", function(){
$("#banner").fadeIn('600', randomPicture())
})})}
I've been playing around with this a bit, and have tried using arrays - now I can get something to appear in the div, but it looks like this:
�V��$��F��Ⱦ36�H�W���/+�N�FC�C b8͔��@�5^��
etc... so I assume it is pulling it up in the wrong format or something? Also, the image isn't changing, it only changes when I refresh the page - am I not calling the function back correctly?
Any help on how to correct this would be much appreciated, thanks!!
Yes, sorry, as I said, I haven't tested it. Here is an working code
Code:
function bannerDisplay(){
$("#banner").fadeOut('400',function(){
var img = new Image()
var imageSrc = "pages/gallery/PhotoWall/images/"+randomPicture()+".jpg";
$(img).load(function(){
$("#banner").html('').append($(this)).fadeIn('600');
}).attr('src', imageSrc);
})
}
Thanks so much - sorry I'm fairly new at all of this so I don't know how to do all of that!
Got this now - but problem is that now nothing displays in the div at all - if I remove the bannerGen function completely then a picture does display in the div, but I need the picture to change, about every 4 seconds - how do I do this!? Use a timeout or a pause or something? I need to be able to get the bannerGen function to loop indefinately.
Thanks for your help!
Code:
<script>
$(document).ready(function(){
function bannerGen() {
function randomPicture() {
var min=101;
var max=120;
var picture=Math.floor(Math.random()*(max-min+1))+min;
return picture;
}
function bannerDisplay(){
$("#banner").fadeOut('400',function(){
var img = new Image()
var imageSrc = "pages/gallery/PhotoWall/images/"+randomPicture()+".jpg";
$(img).load(function(){
$("#banner").html('').append($(this)).fadeIn('600');
}).attr('src', imageSrc);
})
}
}
bannerGen();
)})
</script>
<div id="banner"></div>
That code does get a random image to appear where I want it to - but I need the image to refresh every 4 seconds or so and I don't know how to get the whole function to cycle after this time frame. I'm fairly new to JS/jQ, so I apologise if this is a fairly noob question - I don't fully understand the code you have written enough to edit it myself. Thank you :0)
$(document).ready(function(){
function randomPicture() {
var min=101;
var max=120;
var picture=Math.floor(Math.random()*(max-min+1))+min;
return picture;
}
function bannerDisplay(){
$("#banner").fadeOut('400',function(){
var img = new Image()
var imageSrc = "pages/gallery/PhotoWall/images/"+randomPicture()+".jpg";
var imageSrc = "auto2moto.png";
$(img).load(function(){
$("#banner").html('').append($(this)).fadeIn('600');
}).attr('src', imageSrc);
})
}
bannerDisplay();
window.setInterval(bannerDisplay, 2000);
})
Yes, sorry, that was mine. Instead of creating directory "pages/gallery/PhotoWall/images/ I put a fake image, so I can see it.
Glad it works as expected.
Bookmarks