0

i'm tring to make a javascript source that load all images in a folder as backgroundImage of my div

<script type="text/javascript">
    $(document).ready(function() {
        $('body').css('background-image', 'url(/web/images/001.png)');
    });
</script>

How i can load all images (.png) in my folder (web/images) as background-image of a div?

Thanks for support, M.

Mirko Brombin
  • 1,002
  • 3
  • 12
  • 34

3 Answers3

1

Try something like this:

$(document).ready(function() {
    var imgno=1;
    for(var imgno=1;imgno<50;imgno++){
       $('div').css({'width':'200px','height':'200px','background-image': 'url(/web/images/'+imgno+'.png)'});
    }

});
Pioter
  • 465
  • 3
  • 8
  • 21
1

refer this link https://stackoverflow.com/a/18480589/4599982 and you can use jquery setTimeout(settimeout ) funcion for change of background after particular time.

Community
  • 1
  • 1
Shridhar
  • 339
  • 2
  • 15
1

If you want to change the background using a interval you should try this

$(document).ready(function() {
    // Function to add leading zeroes
    function pad(num, size) {
        var s = num + "";
        while (s.length < size) s = "0" + s;
        return s;
    }

    // Variable to store the current image index
    var currentIdx = 1; 
    var max = 200; // Qtd of images in the folder

    setInterval(function() {
        // Reset the index when overflow
        if(currentIdx > max) currentIdx = 1;
        // Change the background
        $('body').css('background-image', 'url(/web/images/' + pad(currentIdx,4) + '.png)');
        currentIdx ++;
    }, 5000); // 5000ms == 5 seconds
});

Here is a Working Sample

nanndoj
  • 6,580
  • 7
  • 30
  • 42