Change Background Image of DIV using jQuery

This tutorial explains how to change background image of a div at specific intervals using jQuery. Every element can have only one b...

This tutorial explains how to change background image of a div at specific intervals using jQuery. Every element can have only one background image at any point of time according to CSS and to make an element use multiple backgrounds, we have to cycle the images to change at regular intervals. This can be achieved by dynamically assigning background images using css() jquery method and adding a simple fadein/fadeout effect would make it look more neat.

This can be used for simple background image switches, for other image and content cycling that require heavy animations it is highly recommended to use one of the jQuery slider plugins or create one using jQuery UI effects.

1. Prepare background images and place it in a folder. Create a new html file in the same folder and copy paste the below code.

<script src="" type="text/javascript"></script>
<script type="text/javascript">
  $(window).load(function() {           
  var i =0; 
  var images = ['image2.png','image3.png','image1.png'];
  var image = $('#slideit');
                //Initial Background image setup
  image.css('background-image', 'url(image1.png)');
                //Change image at regular intervals
   image.fadeOut(1000, function () {
   image.css('background-image', 'url(' + images [i++] +')');
   if(i == images.length)
    i = 0;
  }, 5000);            
      <div id="slideit" style="width:700px;height:391px;">  

Initially, when the page loads the first image is set as background image. After that for a period of every 5000 milliseconds the image is changed using setInterval function that executes periodically. To make the transition look more neat, I have added fadeIn/fadeout effects.

If you think this article and other information on this website is useful to you, please keep yourself subscribed via email or other social networking sites to be notified whenever more useful articles are posted here. Thanks for reading!



Latest 1421167339148854348

Post a Comment

  1. A very nice tutorials! Thanks for the updates

  2. Is this method reliable for browser compatibility?

    1. Hi Irene,

      We have used jQuery Fade in -out effects. As far as I know, it works well in all modern browsers and in IE from version 9 onwards. There are workarounds for making it work in IE8.

      Hope this helps!


  3. Nice tutorial, is there anyway we can add pagination in the code?


  4. how you can make a fade avoiding overlapping images to see for example the white background of the images?

  5. Gracias por excelente material. Le quité el efecto de fadeIn-fadeOut y funciona de lo mejor.

  6. You can use other functions other effect than the "#slideit"

  7. Nice. But which function should I ad, so that the images are random? tx.

  8. as you are using jquery already I would use .each to save two lines of code :)

  9. This article is really helpful for me. I am regular visitor to this blog. Share such kind of article more in future. Personally i like this article a lot and you can have a look at my services also: I was seriously search for a Salesforce training institutes in ameerpet which offer job assistance and Salesforce training institutes in Hyderabad who are providing certification material. It's worth to join Salesforce training institutes in India because of their real time projects material and 24x7 support from customer desk. You can easily find the best Salesforce training institutes in kukatpally kphb which are also a part of Pega training institutes in hyderabad. This is amazing to join Data science training institutes in ameerpet who are quire popular with Selenium training institutes in ameerpet and trending coureses like Java training institutes in ameerpet and data science related programming coures python training institutes in ameerpet If you want HCM course then this workday training institutes in ameerpet is best for you to get job on workday.

  10. What an incredibly beautiful story, despite the fact that it is rugged but the result turned out to be kind and good and now it has become a tradition that is passed on in every generation 먹튀검증

  11. This changing image process is difficult but not for experts because they can complete it in very short time. jQuery field has big scope and you can adjust in it easily. Dissertation writing service.

  12. Thanks for the quality content that you have here. The National examination council has revealed that NECO Result Checker 2021 has been released. The result was released following the update of the NECO Scratch Card Price. Finally students are advised to get NECO Token for free here.

  13. I have bookmarked your blog, the articles are better than other similar blogs.. thanks for a great blog!
    infected days

  14. Amazing website, Love it. Great work done. Nice website. Love it. This is really nice.
    Hbomax/tvsignin login/begin

  15. If you want to save a large amount of your money then click the link. So, visit here skillz discount code

  16. If you want to save a large amount of your money then click the link and make a joyful future for you.
    Kyte Baby coupon

  17. wordle website is an internet phrase recreation with a fun-filled puzzle theme for everybody. The participant's job is to guess and resolve the hidden phrases on the board. Gamers could have six makes an attempt to conjecture a five-letter phrase, with suggestions given for every guess within the type of coloured tiles indicating when the letters match or occupy the right place. The mechanics of this recreation are nearly just like the pen and paper Jotto recreation. Are you prepared to overcome the challenges? Be a part of Wordle at this time.

  18. We offer a wide range of high-quality frames and sizes to suit any need and budget. portrait illustration

  19. My friend from dissertation editing services told me that we have to cycle the images to change at regular intervals by dynamically assigning background images using css

  20. This is a great tutorial for anyone wanting to learn how to change their background image using jQuery. The author provides clear and concise instructions, along with well-commented code snippets, making it easy to follow. I would definitely recommend their services to anyone in need of MBA assignment help in the New Zealand.