In this tutorial I’ll show you how to achieve the split-flap display (aka airport departure board) effect with JavaScript. Now available also as Jquery plugin
Before we start with the coding, let my explain really quick what we’ll do. For the purpose of this tutorial I’ve prepared a table with strings. My if-act (effect) will be executed on mouse over and stopped on mouse out if necessary. Let’s get started!
When you look at an airport departure board what do you see? Yep, flipping characters! So lets begin by defining the chars we want to flip in a long string.
1 |
var chars="#@!$%&*()_-0123456789abcdefghijklmnpqrstuvwxyz "; |
Let’s put this variable in the body of a constructor function that will be our flipper class.
1 2 3 |
function flipper (obj) { var chars="#@!$%&*()_-0123456789abcdefghijklmnpqrstuvwxyz "; } |
As you can see the constructor has 1 parameter that is the object whose innerHTML we’ll flip. In order to do that we need the chars of the innerHTML in an array. Chopping a string to array is simple with the method .split() of the String class. I also use another two arrays. The first I called indexes holds the indexes of the chars that need to be flipped. The other one is the array that will be flipped and it holds indexes of chars in our chars variable. With a little bit of initialization we come to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function flipper (obj) { var chars="#@!$%&*()_-0123456789abcdefghijklmnpqrstuvwxyz "; this.obj = obj; this.textArray = this.obj.innerHTML.split(""); this.html = this.obj.innerHTML; this.indexes = []; this.cia = []; // CIA - just a coincidence. Stands for Character Indexes Array this.interval; var _this = this; for (var i = 0; i<this.textArray.length; i++) { this.cia.push(0); this.indexes.push(i); } } |
We’ll come to the rest of the variables in a moment but first take a look at the initialization loop. When the if-act starts every char from the innerHTML will start to flip from the char at position 0 in chars (#). I need all of them to start rolling and that’s why I push all indexes in the indexes array. Notice that indexes in this variable correspond to those of both the textArray and the cia. The flipping will be handled by a function called roll(). In its body there is a loop trough indexes. For every index I increment the value in cia and thus go another char forward from chars until I reach the char that is at the same index in textArray. In that case I exclude the index so that it’s not flipped anymore. The function looks like this:
1 2 3 4 5 6 7 8 9 10 11 |
this.roll = function () { for(var i = 0; i<this.indexes.length; i++) { this.cia[this.indexes[i]]++; if (chars.charAt(this.cia[this.indexes[i]]).toUpperCase() === this.textArray[this.indexes[i]].toUpperCase()) this.indexes.splice(i,1); } this.repaint(); if (this.indexes.length == 0) this.stop(); // if no chars need to be flipped - stop } |
The repaint function assembles the string and puts/shows it.
1 2 3 4 5 |
this.repaint = function () { var txt=''; for (var i = 0; i<this.cia.length; i++) txt+=chars.charAt(this.cia[i]); this.obj.innerHTML=txt; } |
The roll method is called every 70ms by setInterval(). That’s what I need the this.interval variable for.
1 2 3 4 5 6 7 8 |
this.timeInt = function () { this.interval=setInterval(function () {_this.roll()},70); } this.stop = function () { clearInterval(this.interval); // no more rolling this.obj.innerHTML = this.html; // restore the original innerHTML } |
_this holds the instance scope. And the code of the whole class goes like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
function flipper (obj) { var chars="#@!$%&*()_-0123456789abcdefghijklmnpqrstuvwxyz "; this.obj = obj; this.textArray = this.obj.innerHTML.split(""); this.html = this.obj.innerHTML; this.indexes = []; this.cia = []; this.interval; var _this = this; for (var i = 0; i<this.textArray.length; i++) { this.cia.push(0); this.indexes.push(i); } this.start = function () { this.timeInt(); } this.stop = function () { clearInterval(this.interval); this.obj.innerHTML = this.html; } this.repaint = function () { var txt=''; for (var i = 0; i<this.cia.length; i++) txt+=chars.charAt(this.cia[i]); this.obj.innerHTML=txt; } this.roll = function () { for(var i = 0; i<this.indexes.length; i++) { this.cia[this.indexes[i]]++; if (chars.charAt(this.cia[this.indexes[i]]).toUpperCase() === this.textArray[this.indexes[i]].toUpperCase()) this.indexes.splice(i,1); } this.repaint(); if (this.indexes.length == 0) this.stop(); } this.timeInt = function () { this.interval=setInterval(function () {_this.roll()},70); } } |
You can check out the demo here: split-flap display if-act
The second table holds another version of my class which starts to flip the chars one by one.
Tips and Hints: You can control the speed of the if-act by reducing/increasing the time in setInterval() and/or the number of the chars in chars.
For questions, suggestions or requests use the comment section below.