Home arrow JavaScript arrow Page 4 - Create Your Own DHTML Slideshow With a Fading Effect

Create Your Own DHTML Slideshow With a Fading Effect

Looking to add an image slideshow to your family or business web site? In this article Martin shows us exactly how it's done using DHTML...

Author Info:
By: Martin Tsachev
Rating: 4 stars4 stars4 stars4 stars4 stars / 51
December 14, 2002
  1. · Create Your Own DHTML Slideshow With a Fading Effect
  2. · The Script
  3. · The Slideshow Script
  4. · The Slideshow Script (contd.)
  5. · Conclusion

print this article

Create Your Own DHTML Slideshow With a Fading Effect - The Slideshow Script (contd.)
(Page 4 of 5 )

When all browser and DHTML compatibility calculations have been made, we actually apply the settings to the layer. In NN 4 itís through the clip.top/right/bottom/left properties and we also set it's visibility to show (Netscape's proprietary value).

After that, we have finished with the NN 4 code and we check for DOM 1 or IE 4 support. The task is easier here, because no matter what clipping is set, we can always get the image height/width. We then make the same test for fade in or fade out, and at the end of the test we apply the settings as a string to the clip property.

I made my first version of this slideshow with the units specified as pixels (px), but I didn't like it at all because I couldn't get it to work in NN 4 and Opera 5. I then found this layer scroll script and I got the feeling for the different browsers compatability and decided to make a version in which the fading can be switched off.

function fadeLayer(layername, amt, tim) {
if (!DHTML) return;
thelayer = new getObj( layername );
if (!thelayer) return;
amount = amt;
theTime = tim;

function stopFade() {
if (time) clearTimeout(time);

The fadeLayer function is used to set the global variables to the parameters passed to it and then invoke the realFade function to do the hard work.

The only thing that the stopFade function does is check the timer ID and if the timer is running, it stops it.

function realFade() {
clipTop += amount;
clipBottom -= amount;
if (clipTop < 0 || clipBottom > lyrheight || clipTop > middle) {
if ( clipTop > middle ) thelayer.style.visibility = 'hidden';
if ( firstFade ) nextFade();
if (document.getElementById || document.all) {
clipstring = 'rect('+clipTop+' '+clipWidth+' '+clipBottom+' 0)'
thelayer.style.clip = clipstring;
} else
if (document.layers) {
thelayer.style.clip.top = clipTop;
thelayer.style.clip.bottom = clipBottom;
time = setTimeout('realFade()',theTime);

var firstFade = true;
function nextFade() {
firstFade = false;
prepLyr(slides[curImg], false);
fadeLayer(slides[curImg], -10, 50);

The realFade function adds the amount to the clipTop and subtracts it from the clipBottom. We then check whether the clipping is off the image. If it is, then we check whether it is faded out. If so, then we hide the slide.

If the firstFade variable is true, then we execute the nextFade function, which fades in the next slide. In both case of fading we end the function, preventing the setTimeout invocation which would result in an infinite loop. The next thing to do is to actually apply the calculated clipping area to the layer. There are differences in the DOM 1 / IE 4 way and the NN 4 (described above in the prepLyr function explanation). After this, we set a timeout that executes the same function again.

The nextFade function then sets the fisrtFade variable to false, which prevents looping and invokes the prepLyr function with parameters for the next slide and false to initialize the image to fade in (clipping top and bottom to the middle). The realFade function is then invoked, passing it the current slide and a change amount of -10 pixels, which means that the clipping area should get bigger. We also pass a delay of 50 milliseconds to widen the area.
blog comments powered by Disqus

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials