jquery simple animate/rotate of a css background-image inside a div?

后端 未结 2 856
野的像风
野的像风 2021-01-25 00:35

I was wondering if anyone could help me out, or point me in the right direction. I\'m looking for a snippet of jquery that will automatically change the background image of a di

2条回答
  •  伪装坚强ぢ
    2021-01-25 01:15

    Something like this should do the trick?

    jQuery( function( $ ) {
        var images = [ "bg-1.jpg", "bg-2.jpg", "bg-3.jpg", "bg-4.jpg", "bg-5.jpg" ];
        var currentImage = 0;
    
        function changeBackground() {
            $( '#page_bg' ).css( { backgroundImage: 'url(' + images[ ++currentImage ] + ')' } );
            if ( currentImage >= images.length - 1 ) {
                currentImage -= images.length;
            }
        }
    
        setInterval( changeBackground, 5000 );  
    });
    

提交回复
热议问题