![]() |
How to use setInterval to change background-image?
Hi, i'm trying to make my homepage switch between 3 different images using setInterval().
This is how far I have come, why doesn't it work? The images is named kontor-1, kontor-2 and kontor-3 Code:
function bildbyte() |
Put the setInterval after the function.
function bildbyte(){ code } setInterval('bildbyte()',5000); |
Are you actually calling bildbyte() anywhere?
|
This is another example:
Code:
Code:
var currentImgNumber = 1;Regards, LC. |
Maybe changing the function to something like this:
Code:
var currentImgNumber = 1;Regards, LC. |
Quote:
Code:
var currentIndex = 1;Code:
.contentCode:
<body onload="windowOnload()">the problem is what i think in the line: Code:
document.content.style.background = 'url(images/kontor-' + currentIndex + '.png)'; |
Quote:
Code:
setInterval( bildbyte , 5000 );Quote:
-either Logic Ali's reply -or the $() is expecting a string. ie: $('body') |
You cannot access a className this way.
If you used an id instead Code:
#contentCode:
document.getElementById('content').style.background = 'url(images/kontor-' + currentIndex + '.png)'; |
Quote:
|
But it's still not working with
Code:
document.getElementById('content').style.background = 'url(images/kontor-' + currentIndex + '.png)';Code:
<div class="content"> |
Code:
<div class="content" id="content"> |
I found a way now to change the background-image.
Code:
var currentIndex = 1; |
Quote:
Code:
$(".content").css('background-image', 'url("images/2.png")');Code:
$(".content").css('background-image', 'url("images/'+currentIndex+'.png")'); |
is it because you forgot the
kontor-?again: Code:
setInterval("switchImage()", 3000);Code:
setInterval( switchImage , 3000 ); |
I just thought i solved it...i had these two global variables local instead.
Code:
function switchImage() |
| All times are GMT +1. The time now is 05:09 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.