Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
docs:programming:javascript:sliding_div [2007/03/15 16:42] – billh | docs:programming:javascript:sliding_div [2008/08/03 00:25] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Sliding Div ====== | ||
+ | ===== How to Create an Animated, Sliding, Collapsible DIV with Javascript and CSS ===== | ||
+ | So, expanding on the last tutorial, I’ve coded a small Javascript library that will allow that functionality. It’s not meant to be a mootools or a script.aculo.us replacement… those are far more advanced libraries. This is just a simple, quick bit of code you can apply to your websites to improve the user experience a bit. | ||
+ | |||
+ | In this tutorial, we will create functions to slide DIVs up and down. It supports multiple animations happening simultaneously, | ||
+ | |||
+ | < | ||
+ | |||
+ | First, create a new file on your webserver called motionpack.js - this is where we will store all the javascript functions for the animations, rather than cluttering up the top of the webpage itself, and slowing down it’s load time. In the beginning of that file, let’s first declare some initial variables:< | ||
+ | var timerlen = 5; | ||
+ | var slideAniLen = 500; | ||
+ | </ | ||
+ | These are the variables you will change if you want to tweak the speed of the animation on your site. timerlen is how often the Javascript function will run to alter the DIV’s properties (altering height or opacity)… it’s probably best to leave this at 5. Any lower will be slightly smoother, but will require your visitors have a better computer (otherwise it’ll be choppy), and much higher will be choppy on everyone, because it’s not updating enough to look smooth. The next variable, slideAniLen, | ||
+ | |||
+ | Now, the next part of the JS file:< | ||
+ | var timerID = new Array(); | ||
+ | var startTime = new Array(); | ||
+ | var obj = new Array(); | ||
+ | var endHeight = new Array(); | ||
+ | var moving = new Array(); | ||
+ | var dir = new Array(); | ||
+ | </ | ||
+ | These are some variables we’ll use throughout the code below to keep track of our animations. We use arrays for each object to allow multiple animations to happen simultaneously. We’ll explain where each of these are used later on, but for now, simply paste them into your JS file. | ||
+ | |||
+ | Now we’re getting to the actual functions. These first functions are what we call from our website itself. They essentially serve as the API for this mini-library.< | ||
+ | function slidedown(objname){ | ||
+ | if(moving[objname]) | ||
+ | return; | ||
+ | |||
+ | if(document.getElementById(objname).style.display != " | ||
+ | return; // cannot slide down something that is already visible | ||
+ | |||
+ | moving[objname] = true; | ||
+ | dir[objname] = " | ||
+ | startslide(objname); | ||
+ | } | ||
+ | |||
+ | function slideup(objname){ | ||
+ | if(moving[objname]) | ||
+ | return; | ||
+ | |||
+ | if(document.getElementById(objname).style.display == " | ||
+ | return; // cannot slide up something that is already hidden | ||
+ | |||
+ | moving[objname] = true; | ||
+ | dir[objname] = " | ||
+ | startslide(objname); | ||
+ | } | ||
+ | </ | ||
+ | This code adds the functions to call when requesting a DIV start sliding up or down. Let’s first look at the slideup() function. We pass it a single parameter- the DIV’s id (objname). The first line checks if the moving variable is set to true for that DIV, so we don’t begin animation on one that’s already moving. Once an animation starts, it will finish uninterrupted. If it is moving, on line 3, we return (exit from the function) so no further animation begins. The next line is a sanity checking if-statement. This function is beginning the slide down process of a DIV, and if a DIV’s display attribute is not set to none (not visible), we assume it’s already slid down and visible, so we can exit the function (abort the process), since there’s nothing else for us to do here. If those checks complete without any complications, | ||
+ | |||
+ | Now, I hope you guys are hanging in there, we’re almost through the tricky parts. :) | ||
+ | |||
+ | The next function we’ll throw in the file is the startslide() function mentioned above:< | ||
+ | function startslide(objname){ | ||
+ | obj[objname] = document.getElementById(objname); | ||
+ | |||
+ | endHeight[objname] = parseInt(obj[objname].style.height); | ||
+ | startTime[objname] = (new Date()).getTime(); | ||
+ | |||
+ | if(dir[objname] == " | ||
+ | obj[objname].style.height = " | ||
+ | } | ||
+ | |||
+ | obj[objname].style.display = " | ||
+ | |||
+ | timerID[objname] = setInterval(' | ||
+ | } | ||
+ | </ | ||
+ | Okay, the last two functions were for making sure we really want to start the slide, and doing all the initial preparations. This function, startslide(), | ||
+ | |||
+ | Now, the function the above timer called was slidetick(), | ||
+ | function slidetick(objname){ | ||
+ | var elapsed = (new Date()).getTime() - startTime[objname]; | ||
+ | |||
+ | if (elapsed > slideAniLen) | ||
+ | endSlide(objname) | ||
+ | else { | ||
+ | var d =Math.round(elapsed / slideAniLen * endHeight[objname]); | ||
+ | if(dir[objname] == " | ||
+ | d = endHeight[objname] - d; | ||
+ | |||
+ | obj[objname].style.height = d + " | ||
+ | } | ||
+ | |||
+ | return; | ||
+ | } | ||
+ | </ | ||
+ | This function is what actually does the animation itself. The first line checks how long the animation has been in progress, by subtracting the animation’s start time from the current time. There’s then an if-statement that checks if the animation has exceeded the preset time (at the top of this file) for an animation’s length, and if so, it calls the endSlide() function, which cleans up (discussed below). If the elapsed time is not greater than the max time (the else-statement), | ||
+ | |||
+ | Now, the last function we’re needing in the motionpack.js file is the endSlide() function:< | ||
+ | function endSlide(objname){ | ||
+ | clearInterval(timerID[objname]); | ||
+ | |||
+ | if(dir[objname] == " | ||
+ | obj[objname].style.display = " | ||
+ | |||
+ | obj[objname].style.height = endHeight[objname] + " | ||
+ | |||
+ | delete(moving[objname]); | ||
+ | delete(timerID[objname]); | ||
+ | delete(startTime[objname]); | ||
+ | delete(endHeight[objname]); | ||
+ | delete(obj[objname]); | ||
+ | delete(dir[objname]); | ||
+ | |||
+ | return; | ||
+ | } | ||
+ | </ | ||
+ | This is the function that’s called when the time runs out on an animation. It finishes everything and cleans up. The first line kills the animation timer, so it doesn’t keep trying to slide. The if-statement checks, and if it was sliding up, it hides the DIV by changing it’s display attribute to “none”. It readjusts the DIV’s height to the original height, so future animations will work fine on it. Then finally there are all those delete lines, deleting any array element we created to keep track of the different animations throughout the process. This cleans up a bit to ensure there are no memory leaks (for sites with lots of animations), | ||
+ | |||
+ | Now you can save that file, with all the pieces above saved up, and upload it to your webserver. | ||
+ | |||
+ | < | ||
+ | |||
+ | Now, we have our entire mini-library done…. so how do we use it? | ||
+ | |||
+ | First we need to include it in our website. This is done by adding this quick line to your website on every page that you’re using the animation, preferably in the HEAD tag:< | ||
+ | <script language=" | ||
+ | </ | ||
+ | This code assumes the file you uploaded is named “motionpack.js” and is in the same folder as your website source code itself. Another common practice is to create a folder called js/ and put all your javascript include files there together, and in that case, you’d change the src tag above line to “js/ | ||
+ | |||
+ | Now that page of your website has sliding abilities! But how do we use them? | ||
+ | |||
+ | First, let’s create a DIV, like we did with our last collapsible DIV tutorial:< | ||
+ | <div id=" | ||
+ | </ | ||
+ | Notice we did make one change though. We added an “overflow: | ||
+ | |||
+ | The difference is this time our link calls functions from our library above. Here’s an example of what a Slide Down link might look like:< | ||
+ | <a href=" | ||
+ | </ | ||
+ | Notice in the onmousedown function we call the slidedown() function from our above library. This will begin the animation and slide the DIV down, until it’s completely visible. You can replace slidedown() with slideup() to do just that, slide the DIV up. | ||
+ | |||
+ | As a challenge to you, try using what you’ve learned in this tutorial and the last to create a toggleSlide() function. It would be a simple if-statement to check if the link’s “display” property, and either slide it up or down, depending on it’s state. We did something very similar in the last tutorial, so try applying that here! | ||
+ | |||
+ | ===== External Links ===== | ||
+ | * source: http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||