JAVASCRIPT IMAGE CYCLER GUIDE In Two Styles To Fit Your Needs
INTRODUCTION
Firstly: all of my love to Aivilo! She was instrumental in helping me figure out Javascript when I kept screwing it up! You can see the thread here where we puzzled it out, hopefully it will come in useful to anyone else looking to get into learning Javascript because it shows some rookie mistakes.
This function cycles through images when they are clicked. Originally I wanted to use it on a biography to cycle through pictures of my deer La, but never ended up drawing enough pictures for it to be useful, so it's just sitting around. It has potential for anyone looking to make any kind of slideshow effect on their page. Because I was so lost at first and originally had one idea for code, then solved that (thanks again Aivilo~!), then went back to the problem, this page has two different codes that you can copy, and they have slightly different benefits. You can pick the one that is best for your needs, I will explain them below.
You will probably need an ok understanding of HTML and CSS in order to understand this. I apologize that if I have failed to make this more transparent. You can use this anywhere for whatever, credit never necessary. Have fun and don't be afraid to ask questions if I am ever unclear with anything or if you have a specific issue, but I'm still learning myself. I want everyone to be able to make the kinds of pages they want to make. It's possible I may update the code or page from time to time if there is an error or if there is something new I want to add. I hope my trackers won't mind too much.
EXAMPLE
Click the number below several times. It will change through three different images.
This example uses only three images, but you can use as many images as you like with just a little tweaking of the code. You can also use things other than images to cycle through, such as div tags or span tags, or nearly any other HTML element that you can put on a page.
CODE, FIRST VERSION
The first version of the code uses three different functions. This is the code that Aivilo solved, and it is superior if you are interested in having your images appear in different places on your page, such as clicking one image to make another image appear in a different place. If you want your images in the same place, use the second version of the code.
<span id="image1" style="display:inline"><a href="javascript:;" onmousedown="var a=document.getElementById('image1');var b=document.getElementById('image2'); if (a.style.display=='inline') {b.style.display='inline'; a.style.display='none'; }"><img src="URLOFYOURFIRSTIMAGEHERE"></a></span><span id="image2" style="display:none"><a href="javascript:;" onmousedown="var b=document.getElementById('image2'); var c=document.getElementById('image3'); if (b.style.display=='inline') {c.style.display='inline'; b.style.display='none';}"><img src="URLOFYOURSECONDIMAGEHERE"></a></span><span id="image3" style="display:none"><a href="javascript:;" onmousedown="var a=document.getElementById('image1'); var c=document.getElementById('image3'); if (c.style.display=='inline') { a.style.display='inline'; c.style.display='none'; }"><img id="image3" style="display:inline" src="URLOFYOURTHIRDIMAGEHERE"></a></span>
EXPLANATION AND PSEUDOCODE
When image1 is clicked, then image2 is displayed and image1 is hidden
When image2 is clicked, then image3 is displayed and image2 is hidden
when image3 is clicked, then image1 is displayed and image3 is hidden
CODE, SECOND VERSION
The second version of the code uses a single function. This is the code that I originally wanted, but figured out only after Aivilo helped with the first version and I was able to correct syntax errors. It is superior if you want to have your images cycle in the same place on your page every time.
<a href="javascript:;" onmousedown="var a=document.getElementById('image1'); var b=document.getElementById('image2'); var c=document.getElementById('image3'); if (a.style.display=='inline') { b.style.display='inline'; a.style.display='none'; } else if (b.style.display=='inline') { c.style.display='inline'; b.style.display='none'; } else { a.style.display='inline'; c.style.display='none'; }"><span id="image1" style="display:inline"><img src="URLOFYOURFIRSTPICTURE"></span><span id="image2" style="display:none"><img src="URLOFYOURSECONDPICTURE"></span><span id="image3" style="display:none"><img src="URLOFYOURTHIRDPICTURE"></span></a>
EXPLANATION AND PSEUDOCODE
When there is a click
Check to see if it was image1 that was clicked.
If image1 was clicked, then show image2 and hide image1
If image1 was not clicked, then check to see if image2 was clicked
If image2 was clicked, then show image3 and hide image2
If image2 was not clicked, then show image1 and hide image3
INSTRUCTIONS
You will have to change your blog format to BBCode, NOT filtered HTML for this to work. You will have to place the URL links to the images that you want to use into the code I have provided, but otherwise the code is ready to use.
If you want to use the first code and put the images in different places, break it apart by the span tags and put each span tag where you want it.
It will not work to use two sets of this function on your page unless you change the IDs of the span tags that hold the images and the parameters of the methods in the JS. The reason for this is that the method I am using in Javascript called getElementByID will only use the first element on the page with a given ID. In general, don't have multiple things with the same ID attribute anyway, use class for that instead.
SOURCES, RESOURCES, AND RECOMMENDED READING
All of the explanation for this code can be found on W3Schools, this is actually very simple stuff. It still took me too long to learn how to do it, but I only needed two things (not counting the operators page).
This would totally work with divs, yep. As it is, the code has images inside of divs, and what is actually being hidden and showed are the divs containing the images that you need to click on to change the divs. So you could put whatever you wanted in the divs and have that be clickable to move to the next one in the cycle.
The javascript tabs some people use work on a pretty similar principle I think, except that they are all independent buttons and this code needs to work in a cycle.
Was Hraeth the one that figured out that you could get javascript onto the site in the anchor tags? That guy/gal deserves a medal. I've seen the javascript tabs people are using too, those are neat. I got the idea of switching between display:none and display:block from them because I am not far enough in reading stuff with javascript to be able to tell it to create a brand new div all on its own, for instance. Maybe someday.
But I've mostly been learning from the W3Schools site for CSS and JS and when I wrote this thing initially it was from scratch. One part if else and one part getElementById. Which was why it was initially so full of errors, lol. So many errors. I kept mixing up the quotes and forgetting that Javascript is case sensitive and that to change something I needed one = and not ==. It was a mess. Aivilo is the hero of the story who solved the puzzle and from there I was able to find all of my errors and debug because she nailed it.
My favorite trick so far is still this one, you can click it and it will show how big your screen is.
Ah, yeah, I don't doubt it could be built from scratch. There's lots of neat things you can do with the code initially introduced. At first glance, it did appear to be the same code(essentially it is,) and so figured you'd taken the base code and merely altered it to do what you'd set out to accomplish with it, and so was wondering where credit was, where credit was due. But, if you alongside someone else managed to put it together from scratch, props to you. BUT ANYWAY ya, something incredibly similar is already circulating about. Good job, though.
PS; no need to explain yourself further. I'll get outta your hair.
That's what I thought you were getting at. I don't blame you. I just wanted to show you that even though I needed a lot of help in the end I was still able to do a javascript thing.
But yeah, it did end up being kind of similar to the tabs. Oh well. Initially I wanted it all to be one code using if else if but that wouldn't work, but split into three if else functions it eventually did.
EDIT: Nevermind, just got the if else if to work, I'm putting that in the OP.
I haven't even finished all the pictures I wanted it to cycle through so even I haven't used it yet haha.
But so far the best idea I've had for it was to put it in a grid of 3x3 and have the pictures be blank, circle, and X so people could play tictactoe on a page. Otherwise it's just an image gallery thing I guess.
And slideshow stuff in general. I was going to have a little series of pictures set up on La's page so people could go through them and generally just have it feel more interactive. She's all about books herself so the more I can make her page feel like a storybook, the better.
But I know this community is full of super creative people who will be able to come up with all kinds of awesome uses for these. I thought of another one where if you use the first code with the three separate functions and use them to make different divs appear you could "chase" something all over the page by making them appear in different places. Like a little monster or something, click it and it vanishes and another one pops up in the corner so you click that and then another one shows up elsewhere and so on. But eventually it would cycle back.
AMG That's too cool.
That's too cool.
Tracking~ Thank you, Alison and Aivilo.
This makes me curious, would it some how work with boxes instead of images?
Avvie by Hadoukin
Tracking this for future use.
Discord:Nazzard#9068 ||Click for bios.
This would totally work with
The javascript tabs some people use work on a pretty similar principle I think, except that they are all independent buttons and this code needs to work in a cycle.
-distant screech-
Thank you, Tuo and Kohva!
stalking so hARD.
Very interesting, thank
Looks like I'll go do some late night testing, then. C;
Avvie by Hadoukin
This is awesome. I was
Track for later ♥
Glad people are interested.
Going to track thisss.
This is making some awesome
Was Hraeth the one that
display:none
anddisplay:block
from them because I am not far enough in reading stuff with javascript to be able to tell it to create a brand new div all on its own, for instance. Maybe someday.But I've mostly been learning from the W3Schools site for CSS and JS and when I wrote this thing initially it was from scratch. One part if else and one part getElementById. Which was why it was initially so full of errors, lol. So many errors. I kept mixing up the quotes and forgetting that Javascript is case sensitive and that to change something I needed one = and not ==. It was a mess. Aivilo is the hero of the story who solved the puzzle and from there I was able to find all of my errors and debug because she nailed it.
My favorite trick so far is still this one, you can click it and it will show how big your screen is.
Ah, yeah, I don't doubt it
PS; no need to explain yourself further. I'll get outta your hair.
That's what I thought you
But yeah, it did end up being kind of similar to the tabs. Oh well. Initially I wanted it all to be one code using if else if but that wouldn't work, but split into three if else functions it eventually did.
EDIT: Nevermind, just got the if else if to work, I'm putting that in the OP.
This is so cool! May
track.
tracking for later. :3
Trackin'. I think I'll use
I think I'll use dis neat thingy~
I haven't even finished all
But so far the best idea I've had for it was to put it in a grid of 3x3 and have the pictures be blank, circle, and X so people could play tictactoe on a page. Otherwise it's just an image gallery thing I guess.
^It'd be useful in a
And slideshow stuff in
But I know this community is full of super creative people who will be able to come up with all kinds of awesome uses for these. I thought of another one where if you use the first code with the three separate functions and use them to make different divs appear you could "chase" something all over the page by making them appear in different places. Like a little monster or something, click it and it vanishes and another one pops up in the corner so you click that and then another one shows up elsewhere and so on. But eventually it would cycle back.