Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-17-2012, 02:25 PM   PM User | #1
krillezzz
New Coder

 
Join Date: Sep 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
krillezzz is an unknown quantity at this point
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() 
{
	var index = 1;
	var totalCount = 3;

	setInterval(function()
	{
		if (index > totalCount) 
		{
			index = 1;
		}
		$(body).css('background-image', 'url(/images/kontor-' + index++ + '.png)');
	}, 5000);
}
krillezzz is offline   Reply With Quote
Old 09-17-2012, 02:38 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 945
Thanks: 7
Thanked 97 Times in 97 Posts
WolfShade is an unknown quantity at this point
Put the setInterval after the function.

function bildbyte(){
code
}
setInterval('bildbyte()',5000);
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 09-17-2012, 04:10 PM   PM User | #3
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 959
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Are you actually calling bildbyte() anywhere?
Logic Ali is offline   Reply With Quote
Old 09-17-2012, 04:17 PM   PM User | #4
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
This is another example:

Code:
<html>
<body onload="window_onload()">
JS:
Code:
var currentImgNumber = 1;
var numberOfImages = 3;

function switchImage(){
   currentImgNumber++;
   document.imgAdvert.src = 'images/YourImage' + currentImgNumber + '.jpg';
   if(currentImgNumber == numberOfImages){
       currentImgNumber = 0;
   }
}

function window_onload(){
   setInterval("switchImage()", 3000);
}
This code changes the image within a div. You could slightly modify this to suit the background images.

Regards,

LC.

Last edited by LearningCoder; 09-17-2012 at 04:30 PM..
LearningCoder is offline   Reply With Quote
Old 09-17-2012, 04:35 PM   PM User | #5
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Maybe changing the function to something like this:
Code:
var currentImgNumber = 1;
var numberOfImages = 3;

function switchImage(){
   currentImgNumber++;
   document.body.style.background = 'url(images/AdvertImage' + currentImgNumber + '.jpg)';
   if(currentImgNumber == numberOfImages){
       currentImgNumber = 0;
   }
}
Set your additional attributes for the image just like you would do in the css like no repeats x+y etc. You can just add to that statement your other declarations.

Regards,

LC.
LearningCoder is offline   Reply With Quote
Old 09-17-2012, 05:37 PM   PM User | #6
krillezzz
New Coder

 
Join Date: Sep 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
krillezzz is an unknown quantity at this point
Quote:
Originally Posted by LearningCoder View Post
Maybe changing the function to something like this:
Code:
var currentImgNumber = 1;
var numberOfImages = 3;

function switchImage(){
   currentImgNumber++;
   document.body.style.background = 'url(images/AdvertImage' + currentImgNumber + '.jpg)';
   if(currentImgNumber == numberOfImages){
       currentImgNumber = 0;
   }
}
Set your additional attributes for the image just like you would do in the css like no repeats x+y etc. You can just add to that statement your other declarations.

Regards,

LC.
Thanks for all your tips, I've been modifying my code into this:

Code:
var currentIndex = 1;
var totalIndex = 3;

function switchImage() 
{
	currentIndex++;
	document.content.style.background = 'url(images/kontor-' + currentIndex + '.png)';
	if (currentIndex == totalIndex) 
	{
		currentIndex = 1;
	}
}

function windowOnload() 
{
	setInterval("switchImage()", 3000);
}
My CSS look like this:

Code:
.content
{
	height: 600px;
	width: auto;
	padding: 0px;
	background-image: url("images/kontor-1.png");
	background-repeat: no-repeat;
	background-position: center;
}
And i run windowOnload() in
Code:
<body onload="windowOnload()">
,
the problem is what i think in the line:
Code:
document.content.style.background = 'url(images/kontor-' + currentIndex + '.png)';
I'm not sure that this make me change this url in the class content in my CSS, because it's still not working
krillezzz is offline   Reply With Quote
Old 09-17-2012, 05:39 PM   PM User | #7
blaze4218
Regular Coder

 
Join Date: Apr 2005
Location: Texas
Posts: 448
Thanks: 24
Thanked 63 Times in 63 Posts
blaze4218 is an unknown quantity at this point
Quote:
Originally Posted by WolfShade View Post
Put the setInterval after the function.

function bildbyte(){
code
}
setInterval('bildbyte()',5000);
That should read

Code:
setInterval( bildbyte , 5000 );
And to address
Quote:
why doesn't it work?
I would venture:
-either Logic Ali's reply
-or the $() is expecting a string. ie: $('body')
__________________
Allwisend bin ich nicht, doch viel ist mir bewursst
-Goethe
blaze4218 is offline   Reply With Quote
Old 09-17-2012, 05:43 PM   PM User | #8
blaze4218
Regular Coder

 
Join Date: Apr 2005
Location: Texas
Posts: 448
Thanks: 24
Thanked 63 Times in 63 Posts
blaze4218 is an unknown quantity at this point
You cannot access a className this way.
If you used an id instead
Code:
#content
{
	height: 600px;
	width: auto;
	padding: 0px;
	background-image: url("images/kontor-1.png");
	background-repeat: no-repeat;
	background-position: center;
}
Code:
document.getElementById('content').style.background = 'url(images/kontor-' + currentIndex + '.png)';
but as long as you are using jQuery, you would't have to switch to an id...
__________________
Allwisend bin ich nicht, doch viel ist mir bewursst
-Goethe
blaze4218 is offline   Reply With Quote
Old 09-17-2012, 05:46 PM   PM User | #9
krillezzz
New Coder

 
Join Date: Sep 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
krillezzz is an unknown quantity at this point
Quote:
Originally Posted by blaze4218 View Post
You cannot access a className this way.
If you used an id instead
Code:
#content
{
	height: 600px;
	width: auto;
	padding: 0px;
	background-image: url("images/kontor-1.png");
	background-repeat: no-repeat;
	background-position: center;
}
Code:
document.getElementById('content').style.background = 'url(images/kontor-' + currentIndex + '.png)';
but as long as you are using jQuery, you would't have to switch to an id...
I'm very new to this like you probably already noticed, but i'm trying to use JQuery, but cant this to work =/
krillezzz is offline   Reply With Quote
Old 09-17-2012, 05:50 PM   PM User | #10
krillezzz
New Coder

 
Join Date: Sep 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
krillezzz is an unknown quantity at this point
But it's still not working with

Code:
document.getElementById('content').style.background = 'url(images/kontor-' + currentIndex + '.png)';
Is it because content is a class? I dont really now how to give that class an ID. Because this image as set as an background-image in .content in the CSS, i dont really have any HTML-tags around it.

Code:
<div class="content">
				<ul id="nav">
					<li><a class="anav" href="#">| Home</li>
					<li><a class="anav" href="about.html">| About</li>
					<li><a class="anav" href="#">| Games</li>
					<li><a class="anav" href="#">| Jobs</li>
					<li><a class="anav" href="#">| Valfri</li>
				</ul>
			</div>
krillezzz is offline   Reply With Quote
Old 09-17-2012, 05:51 PM   PM User | #11
blaze4218
Regular Coder

 
Join Date: Apr 2005
Location: Texas
Posts: 448
Thanks: 24
Thanked 63 Times in 63 Posts
blaze4218 is an unknown quantity at this point
Code:
<div class="content" id="content">
				<ul id="nav">
					<li><a class="anav" href="#">| Home</li>
					<li><a class="anav" href="about.html">| About</li>
					<li><a class="anav" href="#">| Games</li>
					<li><a class="anav" href="#">| Jobs</li>
					<li><a class="anav" href="#">| Valfri</li>
				</ul>
			</div>
__________________
Allwisend bin ich nicht, doch viel ist mir bewursst
-Goethe
blaze4218 is offline   Reply With Quote
Old 09-17-2012, 06:06 PM   PM User | #12
krillezzz
New Coder

 
Join Date: Sep 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
krillezzz is an unknown quantity at this point
I found a way now to change the background-image.

Code:
var currentIndex = 1;
var totalIndex = 3;

function switchImage() 
{
	currentIndex++;
	$(".content").css('background-image', 'url("images/'+currentIndex+'.png")');
	if (currentIndex == totalIndex) 
	{
		currentIndex = 1;
	}
}

function windowOnload() 
{
	setInterval("switchImage()", 3000);
}
The problem is that it's not switching to any picture, it just go blank
krillezzz is offline   Reply With Quote
Old 09-17-2012, 06:08 PM   PM User | #13
krillezzz
New Coder

 
Join Date: Sep 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
krillezzz is an unknown quantity at this point
Quote:
Originally Posted by krillezzz View Post
I found a way now to change the background-image.

Code:
var currentIndex = 1;
var totalIndex = 3;

function switchImage() 
{
	currentIndex++;
	$(".content").css('background-image', 'url("images/'+currentIndex+'.png")');
	if (currentIndex == totalIndex) 
	{
		currentIndex = 1;
	}
}

function windowOnload() 
{
	setInterval("switchImage()", 3000);
}
The problem is that it's not switching to any picture, it just go blank
If i write
Code:
$(".content").css('background-image', 'url("images/2.png")');
instead of
Code:
$(".content").css('background-image', 'url("images/'+currentIndex+'.png")');
then it changes to the right picture, but its not working with '+currentIndex+'
krillezzz is offline   Reply With Quote
Old 09-17-2012, 06:09 PM   PM User | #14
blaze4218
Regular Coder

 
Join Date: Apr 2005
Location: Texas
Posts: 448
Thanks: 24
Thanked 63 Times in 63 Posts
blaze4218 is an unknown quantity at this point
is it because you forgot the kontor-?
again:
Code:
setInterval("switchImage()", 3000);
should read
Code:
setInterval( switchImage , 3000 );
__________________
Allwisend bin ich nicht, doch viel ist mir bewursst
-Goethe
blaze4218 is offline   Reply With Quote
Old 09-17-2012, 06:11 PM   PM User | #15
krillezzz
New Coder

 
Join Date: Sep 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
krillezzz is an unknown quantity at this point
I just thought i solved it...i had these two global variables local instead.

Code:
function switchImage() 
{
	var currentIndex = 1;
	var totalIndex = 3;
	currentIndex++;
	$(".content").css('background-image', 'url("images/'+currentIndex+'.png")');
	if (currentIndex == totalIndex) 
	{
		currentIndex = 1;
	}
}

function windowOnload() 
{
	setInterval("switchImage()", 3000);
}
When i load my webpage it switches to from the first to the second image...but there it stops
krillezzz is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:49 PM.


Advertisement
Log in to turn off these ads.