Warning: Use of undefined constant ENABLE_CACHE - assumed 'ENABLE_CACHE' (this will throw an Error in a future version of PHP) in /nfs/c04/h02/mnt/66302/domains/zackgrossbart.com/html/hackito/wp-config.php on line 79
Give Your Next Presentation In Your Browser With JQuery

Give Your Next Presentation In Your Browser With JQuery

by Zack Grossbart on November 5, 2009

Fork me on GitHub

I’d rather watch linoleum curl than sit through one more robotic technology presentations. A monotone drone over poorly organized slides turns even the most interesting topics sour. There are many great blogs about giving better presentations. Presentation Zen and the Duarte Design blog are two worth reading. I’ve even written a little of my own advice.

Good presentations are well thought out, have strong supporting materials, and tell interesting stories. They also have a sense of pacing. When I present I want one topic to flow into the next, but nothing ruins pacing faster than watching the presenter fool around with PowerPoint when they should be presenting.

“OK. Now we’ll switch to the demo… Where’s that button… Hey Bob do you know how to make this PowerPoint window go away?”

See a presentation
in Action

Switching between applications during a presentation throws off your pacing and I don’t like to do it. I recently gave a presentation about CSS, with many examples that ran in a web browser. The solution was simple: put all of my presentation in the browser.

I created a new presentation program using simple HTML, CSS, and JavaScript. This slide show works in all browsers and on all platforms; sharing it is as simple as sending an URL. Just like SlideShare.net you can host anywhere without all that Flash. This approach also gives you the big advantage of writing your slides in HTML. The only drawback is you have to write your slides in HTML.

This article shows you how to create your own presentations with JQuery and gives you an in-depth look at how this program works. It is a useful introduction to JQuery programming and a working program for giving presentations. It’s also released under the Apache 2.0 License and is totally free.

See It In Action

This all started because I wanted to give a presentation about CSS that had many CSS examples. I figured I could combine the presentation and the examples in one and I got Zack’s (Not So) Basic CSS. I kept this presentation simple because I wanted to keep the CSS simple.

A few weeks later I gave a presentation about JQuery that got a little more complex. Zack’s JQuery Jumpstart added a navigation footer and a much nicer key explaining the keyboard navigation.

This framework is a flexible alternative to PowerPoint. You can change these presentations with CSS and make them look any way you want. Let’s take a look at how it works.

The HTML

You do most of your work for these presentations in HTML. Each slide is a div tag with a class of slide. The easiest type of slide is a content slide like this:

<div class="slide">
    <p class="titleslide">
        My Title Slide
    </p>
</div>

There are a few other built-in slide types including content slides:

<div class="slide contentslide">
    <p>
        I'm going to put a lot of text here.
    </p>
</div>

and iframe slides for embedding other website into your presentation:

<div class="slide">
    <iframe src="an url" frameborder="0" scrolling="no" width="100%" height="99.7%"></iframe>
</div>

Each slide is HTML and the sky’s the limit. As long as your data is contained in a div tag and with the class slide you can put anything you want in there. The slides will appear in your presentation in the order they are written in the HTML file.

The HTML also contains the controls like the navigation key and the footer that automatically integrate with your presentation. Any slide you have will display above the footer.

The CSS

You can add whatever CSS you want to support your slides, but the presentation needs some basic CSS to make each slide work.

.slide {
    position: absolute;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 100%;
    background: white;
}

.titleslide {
    position: relative;
    top: 33%;
    font-size: 72px;
    text-align: center;
    vertical-align: middle;
}

.contentslide {
    margin: 2em;
    height: 85%;
    width: 85%;
    display: inline;
    left: 10%;
    top: 10%;
}

This CSS basically does three things:

  1. Make a single slide take up the whole screen
  2. Make the text in title slides center aligned and really big
  3. Add a little spacing to content slides

The JavaScript

The JavaScript is where the magic happens, but it’s more rabbit out of a hat than Harry Potter.

$(".slide").each(function () {
  slideCount++;
  $(this).css("display", "none");
  slideArray[slideCount] = $(this);
});
    
slideArray[currentSlide].css("display", "block");

First we iterate over all the slides and add them to an array. While we iterate we also hide every slide. Once we’ve done that we just make the first slide visible.

After we add this stack of slides to an array positioning the slide deck is easy. We simply hide the current slide and show the requested slide.

function positionSlide(/*int*/ slide) {
    if (slide > 0 && slide <= slideCount) {
        slideArray[currentSlide].hide();
        currentSlide = slide;
        slideArray[currentSlide].show();
    }
}

If you wanted to get fancy you could do this with a JQuery Effect. Just replace the calls to hide and show to calls for slideIn or fadeIn and you are all set. I kept it simple because I often present remotely where animations look choppy.

The rest of code takes care of enabling and disabling buttons and hiding and showing controls. Switching from one slide to another is fast and easy. I can use my keyboard, the arrow buttons, or jump to a specific slide. You can see all the source code.

A Little Bonus

One of my favorite presenters likes to start her presentations with an animation. The animation gives the audience something to look at while they are finding a place to sit and waiting for the presentation to start. PowerPoint makes animations like this pretty easy and I wanted to do the same with JQuery

See the animation
in Action

For my recent JQuery presentation I started things off with a simple JQuery animation.

The animation uses a special type of slide called an animatedslide. In this slide you give every word you want animated a different class:

<div class="slide">
    <p class="titleslide animateslide">
        <span class="animcell">Welcome</span> 
        <span class="animcell">To</span> 
        <span class="animcell">Zack's</span> 
        <span class="animcell">JQuery</span> 
        <span class="animcell">Jumpstart</span>
    </p>
</div>

Then the JavaScript kicks in:

function anim(/*JQuery*/ obj) {

    /*
     * JavaScript doesn't give you direct access to threads
     * so starting and stopping animations doesn't work 
     * very well.  We hack around that by only running the
     * animation a certain number of times.
     */
    var count = 0;

    /*
     * These variables hold the current position of the element.
     */
    var x = 0;
    var y = 0;
    
    while(count < 300) {

        /*
         * Step 1. Generate a random number for the direction to 
         * move the text.
         */
        var direction = Math.floor(Math.random() * 5);
        
        count++;
        if(x > 300 || x < -300 ||
           y > 300 || y < -300) {
            /*
             * Step 2a. We don't want the text to move too far out of
             * the screen so we'll push it back to the center when it
             * is too close to the edge.
             */ 
            obj.animate({ 
                left: "0px",
                top: "0px"
                }, 3000, "linear");
            
            x = 0;
            y = 0;
        } else {
            /*
             * Step 2b.  We increase or decrease the X or the Y depending
             * on the direction we want the text to move.
             */
            if (direction == 1) { //go left
                x -= 100 + Math.floor(Math.random() * 201);
            } else if (direction == 2) { //go right
                x += 100 + Math.floor(Math.random() * 201);
            } else if (direction == 3) { //go top
                y -= 100 + Math.floor(Math.random() * 201);
            } else if (direction == 4) { //go bottom
                y += 100 + Math.floor(Math.random() * 201);
            }

            /*
             * Step 3.  We run the animation.
             */
            obj.animate({ 
                left: x + "px",
                top: y + "px"
                }, 3000, "linear");
        }
    }
}

I know it looks a little complicated, but it's really pretty simple. Each word chooses a random direction and moves a random amount in that direction. If the words gets too close to the edge of the screen it bounces back to where it started. The effect is simple, but strangely hypnotic.

I must confess there is a hack here. The animation will repeat 300 times and stop. I've done multi-threaded programming in Java, C++, Erlang, and Scala, but the threading model of JavaScript is still a mystery to me. If someone knows a better way to make this work I would love to hear about it.

Build Your Own Presentation

Make your own
presentation

Now that you've seen how it works you're ready to build your own presentation. Just download the sample presentation and add your slides to the index.html file. When you're done just post it to your favorite web server. The presentation will run with just about every browser and every operating system.

Conclusion

Building all of your presentations in HTML is just silly, but when you need to include many live web pages this is a good solution for the "um... where is the button to get out of PowerPoint?" problem. The results are small, fast, and easy to share. They'll also get indexed by Google unlike SlideShare presentations.

Beyond the usefulness of this application I like the idea of turning a web browser into something else. Browsers are an interesting platform, but if they want to be taken seriously they need to stop just supporting web pages.

Name This Application Contest

You may have noticed that this poor application doesn't have a name. I'm holding a contest to suggest a name. Just post your suggestions as a comment. The winner gets a free copy of this open source software. Good luck!