dimanche 28 mai 2017

jQuery random image

I have an index.html page in which I want to show 1 random image from a folder.

I have found a script that I like because I dont have to write each image url. But I'm not shure what the "10 + 1" means (max 10 images in the folder?)

The thing is, I just don't know what to do with this script!

In the "head", I think I need to link to a jquery librairy. Should the script be in a ".js" file? Or can it be simply put in the "body" of the page? (or elsewhere?)

Thanks for helping!

The HTML would be:

img src="" class="myClass1" width="100px" height="auto" alt="no image" />

And the script:

$('.myClass1').each(function() {

var num = Math.floor(Math.random() * 10 + 1), img = $(this);

img.attr('src', 'http://ift.tt/2qp5sTh' + num + '.jpg'); img.attr('alt', 'Src: ' + img.attr('src'));

});




Aucun commentaire:

Enregistrer un commentaire