Tutorials from 2009

jQuery? You’ve got to toggle

jQuery?  You’ve got to toggle

Toggling makes it easy to turn things on and off

Everyone likes writing jQuery for one main reason: it’s really easy to use. One feature that makes life a lot easier is toggling. Let’s say you have an image that you need to show when a button is clicked and then hide when it is clicked again. Instead of two separate functions, one to show and another to hide, use jQuery’s handy shorthand: toggle:

$(‘button’).click( function() {

But jQuery’s support of toggling goes way beyond hiding and showing. It has toggleClass() which adds a class or removes it depending on if the class exists. Also slideToggle() which slides an object up and then down.

And besides these simple shorthands for existing functions, jQuery has a variety of support for more custom toggle functions. … (more…)

Appending Grayed-Out Overlays with jQuery and CSS

Grayed-out overlay example

Probably one of the most widely adopted “web 2.0″ features is the grayed out overlay. Rather than always redirecting users to a new page, overlays allow the current page to get “grayed out” with an overlaid panel. With the wide use of Javascript libraries like jQuery, the prevalence of these grayed-out overlays is understandable: they are very easy to build.

Let’s start with the markup and CSS. We’ll need two wrappers, appended at the end of the DOM. Although we will append these later with jQuery, for now just put them right in the HTML (or wait until we append them).

The idea for these wrappers is that the first ‘overlay’ div will be the grayed-out background and the ‘overlay-panel’ will be the HTML panel that sits on top of the overlay.

Now let’s get started by styling the overlay. The first problem we’ll have to tackle … (more…)

Best Practices: How to Include IE-Specific CSS Styles and Stylesheets

IE specific CSS stylesheets

Although good CSS should be written to be as browser-universal as possible, even the best front end developers find it necessary at times to target specific browsers for certain styles. In most cases this means writing a set of special CSS blocks to handle eccentricities in IE and its various versions.

There’s a wide variety of reasons to target specific browsers: IE6′s lack of native support for transparent png24′s, pesky ‘has-layout’ bugs, and IE6′s lack of min-height support are just a few.

One way to target specific browsers is through CSS selector hacks, which take advantage of quirks in different browser implementations. Perhaps the best known browser hack is the “star html” hack. Basically you prepend * html to any normal selectors in order to target IE6 specifically:

Controlling Animation Timing in jQuery

Timing animations in jQuery can appear confusing

Animation and function timing can often seem like an uphill battle in Javascript. Thankfully jQuery’s variety of timing control mechanisms provide excellent alternatives to Javascript’s standard order of function processing.

With both callback functions and chainable methods, jQuery allows much greater control over animation timing than Javascript alone. Callback functions provide the ability to execute a function once an animation has completed. Chainable methods allow us to stack a series of operations on a single object. Combining the two, jQuery provides near perfect control over the timing of animations.

Build a Simple Flash MP3 Player in AS2

Making a MP3 player is really simple using Flash and AS2. First open up an FLA in Actionscript 2.0 format. In the first frame, open the actions window and write:

var simple_mp3:Sound = new Sound();
simple_mp3.loadSound( ‘example.mp3′, true );

Here we’re loading the MP3 ‘example.mp3′. The second variable in loadSound() is whether to stream the media. If you would like to wait until the MP3 loads completely, just set this to false.

Supporting the Browser Back Button with Javascript

How to support the browser back button

When developing a Javascript-heavy, AJAX-riddled site, I often run into a 2.0 type of problem: supporting the browser back button. While it’s wonderful to build a site where users are brought to various content pages without the window refreshing, this excellent user experience will be completely ruined if you hit the back button and it returns you to the root of the site (or worse the last site you visited).

Thus, integrating the browser becomes an integral part of any Javascript developer’s toolkit.

Making the browser recognize Javascript changes

The first problem we have to solve is making the browser respect a Javascript change as a page change. However we have to be sure not to change the actual location of the page, or our AJAX or Javascript experience will be shot.

One thing to look at is the window location’s … (more…)