@manhng

Welcome to my blog!

Change Background Image Every 5 Seconds

July 25, 2018 15:25

Change Background Image Every 5 Seconds (edit)

<script>
$(function () {
var body = $('body');
var backgroundImages = [
'url("/Content/frontend/img/bg1.jpg")',
'url("/Content/frontend/img/bg2.png")'];
var current = 0;

function nextBackground() {
body.css(
'background-image',
backgroundImages[current = ++current % backgroundImages.length]);

setTimeout(nextBackground, 5000); //automatically change background-image every 5 seconds
}
setTimeout(nextBackground, 5000); //automatically change background-image every 5 seconds
body.css('background-image', backgroundImages[0]);
});
</script>

https://stackoverflow.com/questions/22075468/jquery-changing-background-image-with-timer

https://css-tricks.com/forums/topic/change-body-background-every-10-sec/

Categories

Recent posts