INTRODUCTION
Here I will narrate the JavaScript programming code used to run a slideshow that rotates “n” number of images. I have provided the code below for this function. It runs well on Internet Explorer, Firefox, Safari, Opera, Chrome as well as iPod, iPhone, iPad and Android.
GET SET UP FOR THE SLIDESHOW
Before the slideshow can begin, there is a patch of JavaScript that assigns the “n” number of image files to the sequenced JavaScript variables as follows.
// The purpose of this code is to assign the file names of image files that are utilized for the slideshow to
// javascript variables which will be rotated in sequence by the programming code.
- <script>
- <!--
-
-
-
- var number_of_images = {“n” maximum number}
- var img_counter = 1
- var slideimage = new Array();
-
- while ( img_counter <= number_of_images )
- {
- slideimage[img_counter] = new Image()
- slideimage[img_counter].src = " imagefile "+img_counter+".jpg"
- img_counter++
- }
-
-
- </script>
The images, (imagefile1.jpg, imagefile 2.jpg, imagefile 3.jpg, ….. imagefile n.jpg) are assigned in sequence to JavaScript image objects “slideimage[1], slideimage[2], slideimage[3], ….. slideimage[n]”.
Next, the initial slideshow image will be displayed with the appropriate height and width values. The image, “<img src= eval('slideimage[1].src') name='genericname' id='generic_id' width=”widthvalue” height=”heightvalue”>”, will have the name component “genericname” and the element id “generic_id”.
The slide show counter variable is set to 2. This is because the first slideshow image is already displayed. The slideshow counter increments by 1 for each new image displayed until the counter reaches the “n” maximum number, then it resets back to 1 to coincide with the display of the first image. Next, the opacity of the slide image is set to 0.25. This is used for all browsers other than Microsoft Internet Explorer (except IE 11).
Now begin the JavaScript function, “generic_javascript_function”. After it completes, it will be called again creating “an infinite loop”. Once inside “generic_javascript_function”, the type of browser being used needs to be detected. At this point, the JavaScript will branch one of two ways. The older Microsoft Internet Explorer (before IE 11) or all others (generally, this is Mozilla Firefox, Apple Safari, Opera, Google Chrome, IE 11 and mobile devices like Apple and Android).
IT'S AN OLDER MICROSOFT INTERNET EXPLORER, SO…
If the visitor enters the web page through an older version of Microsoft Internet Explorer (before IE 11), then the proprietary MSIE “blendTrans” function will be called. First for 1 second ‘document.images.genericname.style.filter="blendTrans(duration=1)"’ and then for 2 seconds ‘document.images.genericname.style.filter="blendTrans(duration=2)"’. Next, the ‘document.images.genericname.filters.blendTrans.Apply()’ directive will be called to apply the filter. After this, the image will be updated with the current JPEG image file, ‘document.images.genericname.src=eval("slideimage["+kounter+"].src")’. Notice that ‘"slideimage["+kounter+"].src"’ is a concatenated expression that creates the JavaScript variable that contains the current JPEG image file. Then it is played with the ‘document.images.genericname.filters.blendTrans.Play()’ directive. These JavaScript expressions all reference “genericname”, that is the name component of the HTML image tag that displays the JPEG images used in the slideshow.
If the slideshow counter variable is less than or equal to the “n” maximum number, then increment it by 1 (advance to the next image in the slideshow sequence). If the slideshow counter variable is greater than the “n” maximum number, then reset it to 1 (wrap around to the first image in the slideshow sequence). Then display the image for 4 seconds before exiting the “generic_javascript_function” function. Next, call the function to repeat the process for the next image in sequence.
AND IF IT ISN'T MICROSOFT INTERNET EXPLORER, THEN…
If the visitor enters the web page through a browser other than the older Microsoft Internet Explorer (before IE 11) or by way of a mobile device, then the ‘document.getElementById('generic_id').style.opacity = opac’ directive is used to set the current slideshow image to the current opacity setting. Notice, the use of the “generic_id” element id from the HTML image tag to effect this opacity change. Next, update the slideshow image with whatever the current JPEG is with ‘document.images.genericname.src=eval("slideimage["+kounter+"].src")’. The “genericname” name component from the HTML image tag is used for this. This could be the same image as before with a different opacity or it can be the next image in the slideshow sequence, because the opacity of the previous slideshow image exceeded 1.0 and has now been set back to 0.25.
Now, increment the opacity variable by 0.25 more than its current value. This will make the next slide presentation less opaque than the previous one. If the new opacity is greater than 1.0, then do the following.
Set the opacity variable to 0.25. If the slideshow counter variable is less than or equal to the “n” maximum number, then increment it by 1 (advance to the next image in the slideshow sequence). If the slideshow counter variable is greater than the “n” maximum number, then reset it to 1 (wrap around to the first image in the sequence). Then display the image for 1 second before exiting the “generic_javascript_function” function and then making a call to the function to repeat the process for the next image in sequence. Unlike the older Internet Explorer version, this may use the same image with a different opacity unless the opacity is greater than 1.0. Then the next image in the sequence will be used with an initial opacity equal to 0.25.
- <script>
- <!--
- document.write("<img src= eval('slideimage[1].src') name='genericname' id='generic_id' width=220
- height=140>");
-
- var kounter = 2
-
-
- var opac = 0.25
-
-
- function generic_javascript_function() {
-
-
- browser_type = navigator.appName;
-
-
-
-
- if (browser_type == "Microsoft Internet Explorer") {
-
- document.images.genericname.style.filter="blendTrans(duration=1)"
-
- document.images.genericname.style.filter="blendTrans(duration=2)"
-
- document.images.genericname.filters.blendTrans.Apply()
-
- document.images.genericname.src=eval("slideimage["+kounter+"].src")
-
- document.images.genericname.filters.blendTrans.Play()
-
-
- if ( kounter <= number_of_images ) {
- kounter++
- }
-
-
- if ( kounter > number_of_images ) {
- kounter = 1
- }
-
- setTimeout("generic_javascript_function()", 4000)
- }
-
-
-
-
- if (browser_type != "Microsoft Internet Explorer") {
-
- document.getElementById('generic_id').style.opacity = opac
-
-
-
- document.images.genericname.src= eval("slideimage["+kounter+"].src")
-
-
- opac = opac + 0.25
-
-
- if (opac > 1.0) {
-
- opac = 0.25
-
-
- if ( kounter <= number_of_images ) {
- kounter++
- }
-
-
- if ( kounter > number_of_images ) {
- kounter = 1
- }
- }
-
- setTimeout("generic_javascript_function()", 1000)
- }
-
-
- }
-
-
-
- generic_javascript_function()
-
-
- </script>
CONCLUSIONWhatever code you are using for
application development should perform seamlessly across multiple browsers and mobile devices. This code seems to do this function well. I should mention that the smaller sized image files seemed to run smoothly with no problems using this code. However, the larger ones of 1 to 3 MB either materialized very slowly or just froze up. So the file size of the image used appears to be a factor in how well this will work. Aside from that, I haven't noticed any other problems.