I have a big div element at header and there are many text contents and some boxes in the div. and i have a big img as bg for this div, now i need to make a slideshow for this div's background :/
How can I make slideshow for a div's background image?
I researched a lot, but could not find anything :/
Thanks a lot! appreciate!
One way of doing this is to make an array of background-images:
var bgArr = ["img/bg1.jpg", "img/bg2.jpg"]; //and so on...
function backgroundSlide(i) {
$("#header").css("background-image", "url("+bgArr[i++]+")");
if (i == bgArr.length) i = 0;
var st = setTimeout(arguments.callee(i), 1000);
}
backgroundSlide(0)
This will just loop and loop...
I too was looking for a css background element slideshow, Thanks for your input. I explored your solution with the setTimeout method, however had massive troubles with it (I'm a noob).
I've adapted peirix's solution with a setInterval alternative and came up with this;
var bgArr = ["images/TopImage-01.jpg", "images/TopImage-02.jpg", "images/TopImage-03.jpg" ];
var i=0;
// Start the slide show
var interval = self.setInterval("swapBkgnd()", 5000)
function swapBkgnd() {
if (i>(bgArr.length-1) ) {
i=0
$("#header").css("background-image", "url("+bgArr[i]+")");
}
else {
$("#header").css("background-image", "url("+bgArr[i]+")");
}
i++;
};
Now I need to add a fade effect and image preloader and I'll be away. Would be interested to know if there's a tidier way to achieve this.
You can use the following to change the background image:
$("#mydiv").css("background-image", "url(1.jpg)");
In order to create a slideshow, call it at regular intervals by using settimeout.
Couple of day before I have been looking for similar explanation. Answer number two was pretty good but as I can see it needs fade effect. I have accomplished that (and more). Here is the code:
var bgArr = ['img1.png', 'img2.png', 'img3.png', 'img4.png'];
var bgText = ['text1', 'text2', 'text3', 'text4'];
var i=0;
var left = 0;
$(document).ready(function() {
timer = setTimeout("changeBG()", 7000);
$("#slider_left").on("click", function(){
i = i-2;
if(i<-1) i = bgArr.length-2;
left = 1;
clearTimeout(timer);
changeBG();
});
$("#slider_right").on("click", function(){
if(i>(bgArr.length-1)) i = 0;
clearTimeout(timer);
changeBG();
});
});
function changeBG() {
i++;
if(i>(bgArr.length-1)) i = 0;
$('#slider_up').stop().fadeIn(1000, function(){
if(left == 1){
var a = i+1;
if(i == bgArr.length-1)
a = 0;
}else{
var a = i-1;
if(i == 0)
a = bgArr.length-1;
}
left = 0;
$("#slider_up").css("background", "url("+bgArr[a]+") no-repeat center center white");
$("#slider_up").css('opacity',0).show().animate({opacity:1});
$("#slider_down").css("background", "url("+bgArr[i]+") no-repeat center center white");
$("#slider_up").css('opacity',1).show().animate({opacity:0});
$(".text_holder").fadeOut(1000, function() {
$(this).html(bgText[i]);
$(this).fadeIn(500);
});
});
timer = setTimeout("changeBG()", 7000);
};
I have explained it on my blog: http://blog.braovic.com/css-background-slider-and-slideshow/
Hope this helps you all.
Best,
Ante.
This may help you - https://github.com/im4aLL/bose
$("#header").bose({
images : [ "imagePath_1", "imagePath_2", "imagePath_3"]
});
But only fade transition. Good luck!