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 06-01-2012, 01:23 PM   PM User | #1
resin
Regular Coder

 
Join Date: Nov 2011
Posts: 142
Thanks: 5
Thanked 0 Times in 0 Posts
resin is an unknown quantity at this point
simple image cycler?

Hello I'm having trouble coming up with a javascript that can cycle through a set of images, and is able to set length of time that each image is displayed? I don't want any extra buttons, fade transitions, etc.. Just a simple automatic image cycler. Javascript or jQuery is fine, whatever can get the job done. Any idea?
resin is offline   Reply With Quote
Old 06-01-2012, 01:49 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,102
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by resin View Post
Hello I'm having trouble coming up with a javascript that can cycle through a set of images, and is able to set length of time that each image is displayed? I don't want any extra buttons, fade transitions, etc.. Just a simple automatic image cycler. Javascript or jQuery is fine, whatever can get the job done. Any idea?
Have a look at the many fine scripts at http://www.vicsjavascripts.org.uk/

Here is a very simple script which may be sufficient for your purposes. The time each image is displayed is 2000 millseconds = 2 seconds, naturally you can alter that.


Code:
<html>
<head>
</head>
<body onload = "show(); setInterval(show,2000)">

<img id="photoslider">

<script type = "text/javascript">

var photos=[];
photos[0]="myimage1.jpg";
photos[1]="myimage2.jpg";
photos[2]="myimage3.jpg";
photos[3]="myimage4.jpg";
photos[4]="myimage5.jpg";
photos[5]="myimage6.jpg";
photos[6]="myimage7.jpg";
photos[7]="myimage8.jpg";
photos[8]="myimage9.jpg";
photos[9]="myimage10.jpg";

var count = 0;
function show() {
document.getElementById("photoslider").src=photos[count];
count ++;
if (count >9) {count = 0}
}

</script>
</body>
</html>
All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 06-01-2012 at 02:08 PM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
resin (06-02-2012)
Old 06-01-2012, 02:06 PM   PM User | #3
resin
Regular Coder

 
Join Date: Nov 2011
Posts: 142
Thanks: 5
Thanked 0 Times in 0 Posts
resin is an unknown quantity at this point
Wow lots of good stuff there, however these are all too fancy for what I need here, unless I missed it in the list. I don't want anything besides the images themselves to appear (I don't want previous/next buttons, thumbnails, transitions, etc).
resin is offline   Reply With Quote
Old 06-01-2012, 02:09 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,102
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by resin View Post
Wow lots of good stuff there, however these are all too fancy for what I need here, unless I missed it in the list. I don't want anything besides the images themselves to appear (I don't want previous/next buttons, thumbnails, transitions, etc).
See the simple script which I added to my post two minutes before your second post.

Edit: I interpreted your request to set the length of time as applying to all the images, but vic below allows you to show each image for a different length of time if you wish.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 06-01-2012 at 02:53 PM..
Philip M is offline   Reply With Quote
Old 06-01-2012, 02:49 PM   PM User | #5
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,382
Thanks: 3
Thanked 466 Times in 453 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
with changes for individual delays

Code:
<html>
<head>
<script type = "text/javascript">

var photos=[
["http://www.vicsjavascripts.org.uk/StdImages/Egypt5.jpg",1000],
["http://www.vicsjavascripts.org.uk/StdImages/Egypt6.jpg",2000],
["http://www.vicsjavascripts.org.uk/StdImages/Egypt7.jpg",1000],
["http://www.vicsjavascripts.org.uk/StdImages/Egypt8.jpg",3000]
];

var photos2=[
["http://www.vicsjavascripts.org.uk/StdImages/Egypt5.jpg",2000],
["http://www.vicsjavascripts.org.uk/StdImages/Egypt6.jpg",3000],
["http://www.vicsjavascripts.org.uk/StdImages/Egypt7.jpg",4000],
["http://www.vicsjavascripts.org.uk/StdImages/Egypt8.jpg",5000]
];

function show(id,ary,cnt){
 clearTimeout(show[id]);
 cnt=cnt||0;
 var nu=cnt;
 document.getElementById(id).src=ary[nu][0];
 cnt=++cnt%ary.length;
 show[id]=setTimeout(function(){ show(id,ary,cnt);  },ary[nu][1]||2000);
}

</script>
</head>
<body onload = "show('photoslider',photos);show('photoslider2',photos2);">

<img id="photoslider">

<img id="photoslider2">
</body>
</html>
__________________
Vic

God Loves You and will never love you less.

http://www.vicsjavascripts.org.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is online now   Reply With Quote
Users who have thanked vwphillips for this post:
resin (06-02-2012)
Old 06-01-2012, 04:25 PM   PM User | #6
resin
Regular Coder

 
Join Date: Nov 2011
Posts: 142
Thanks: 5
Thanked 0 Times in 0 Posts
resin is an unknown quantity at this point
.....

Last edited by resin; 06-02-2012 at 07:33 PM..
resin is offline   Reply With Quote
Old 06-01-2012, 04:39 PM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,102
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
There are two reasons why multiple scripts in the same page will not work together.

a) duplicate global variable and/or function names (including loop counters etc.)
b) multiple onload statements.

Have a look at:-
http://www.javascriptkit.com/javatut...iplejava.shtml
http://www.dyn-web.com/tutorials/combine.php

Or you can simply fire a set of functions when the page loads.
Code:
<script type="text/javascript">
window.onload = function() {
functionOne();
functionTwo();
}
</script>
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 06-02-2012, 11:49 AM   PM User | #8
resin
Regular Coder

 
Join Date: Nov 2011
Posts: 142
Thanks: 5
Thanked 0 Times in 0 Posts
resin is an unknown quantity at this point
ok...

Last edited by resin; 06-02-2012 at 07:33 PM..
resin is offline   Reply With Quote
Old 06-02-2012, 07:38 PM   PM User | #9
resin
Regular Coder

 
Join Date: Nov 2011
Posts: 142
Thanks: 5
Thanked 0 Times in 0 Posts
resin is an unknown quantity at this point
Is it possible to somehow add css styling to each individual image under the var photos list? Say if I wanted to move third image down 5px, how would I do that?
resin is offline   Reply With Quote
Old 06-03-2012, 12:46 AM   PM User | #10
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,797
Thanks: 30
Thanked 462 Times in 456 Posts
jmrker will become famous soon enough
Question

Quote:
Originally Posted by resin View Post
Is it possible to somehow add css styling to each individual image under the var photos list? Say if I wanted to move third image down 5px, how would I do that?
Which post above is the current code you are trying to modify?
jmrker is offline   Reply With Quote
Old 06-03-2012, 05:27 AM   PM User | #11
resin
Regular Coder

 
Join Date: Nov 2011
Posts: 142
Thanks: 5
Thanked 0 Times in 0 Posts
resin is an unknown quantity at this point
The code in vwphillips post above.
resin is offline   Reply With Quote
Old 06-03-2012, 09:10 AM   PM User | #12
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,382
Thanks: 3
Thanked 466 Times in 453 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Code:
<html>
<head>
<script type = "text/javascript">

var photos=[
["http://www.vicsjavascripts.org.uk/StdImages/Egypt5.jpg",1000],
["http://www.vicsjavascripts.org.uk/StdImages/Egypt6.jpg",2000,5],
["http://www.vicsjavascripts.org.uk/StdImages/Egypt7.jpg",1000],
["http://www.vicsjavascripts.org.uk/StdImages/Egypt8.jpg",3000]
];

var photos2=[
["http://www.vicsjavascripts.org.uk/StdImages/Egypt5.jpg",2000],
["http://www.vicsjavascripts.org.uk/StdImages/Egypt6.jpg",3000],
["http://www.vicsjavascripts.org.uk/StdImages/Egypt7.jpg",4000],
["http://www.vicsjavascripts.org.uk/StdImages/Egypt8.jpg",5000]
];

function show(id,ary,cnt){
 clearTimeout(show[id]);
 cnt=cnt||0;
 var nu=cnt;
 document.getElementById(id).src=ary[nu][0];
 document.getElementById(id).style.top=(ary[nu][2]||0)+'px';
 cnt=++cnt%ary.length;
 show[id]=setTimeout(function(){ show(id,ary,cnt);  },ary[nu][1]||2000);
}

</script>
</head>
<body onload = "show('photoslider',photos);show('photoslider2',photos2);">

<img id="photoslider" style="position:relative;" >

<img id="photoslider2">
</body>
</html>
__________________
Vic

God Loves You and will never love you less.

http://www.vicsjavascripts.org.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is online now   Reply With Quote
Old 06-03-2012, 10:35 AM   PM User | #13
resin
Regular Coder

 
Join Date: Nov 2011
Posts: 142
Thanks: 5
Thanked 0 Times in 0 Posts
resin is an unknown quantity at this point
Wow good call, how did you know I missed that lol? This is working perfect now. Thanks again

Last edited by resin; 06-03-2012 at 02:29 PM..
resin is offline   Reply With Quote
Old 06-03-2012, 02:03 PM   PM User | #14
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,382
Thanks: 3
Thanked 466 Times in 453 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Code:
<img id="photoslider" style="position:relative;" >
__________________
Vic

God Loves You and will never love you less.

http://www.vicsjavascripts.org.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is online now   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 03:45 PM.


Advertisement
Log in to turn off these ads.