Archive by Author

Episode 13: Animating Color

Animating Color

If you want to easily create animations in jQuery, you can use the animate() method to animate any numerical CSS property – font-size, width, padding, margin, left, top, etc. However, even though colors in CSS are technically numerical, the jQuery animate() method won’t animate colors.

The jQuery team released the jQuery Color plugin which adds support for color animation, but with CSS3 and the transition property, we don’t even need the plugin to create color animations. We can use jQuery in combination with CSS for animating color without using the Color plugin.

We get started by creating a simple grayscale style for a <div>. Then we add a hover style to the <div> to show how we can use CSS to change the color scheme of our <div> on hover using just CSS. It’s okay, but it’s just a sudden color change.

So we take a look at how we can use the CSS transition property to create an animation for that color change. All we have to do is add the transition property to the grayscale styling for our <div>. We specify which property or properties we want to add an animated transition to, then we say how long the animation should take to complete. You can get even more specific about your transition, so check out this article for more information on what you can do.

CSS alone works great as long as we only want the animation to happen when we’re hovering, but if we want to animate the color change on click, we have to step into using a bit of jQuery. We can use a CSS class in combination with a click event handling function to animate the color change when we click. All we have to do is use jQuery’s addClass method to add the class.

And if we want to toggle the class on and off, we can just use jQuery’s toggleClass method instead. We take a look at how we can use different color schemes for each individual <div> so that each changes to a different color when clicked.

CSS3 makes some pretty amazing things possible without even using JavaScript, but when we can use CSS3 in combination with JavaScript, we have the power to do some pretty awesome stuff with just a teeny bit of code.

Plugin: Minimap

Create a small preview of a full page or just a part of it that supports navigation and flexible positioning. Super cool.

Episode 12: No Conflict Mode

No Conflict Mode

As you’ve probably noticed in our code examples, jQuery uses the $ to select elements that we want to work with. The only problem is, jQuery isn’t the only JavaScript library that thought the $ made a handy shortcut. If you want to use more than one JavaScript library on your pages, you can tell jQuery that you don’t want it to use the $ any more so that the other library can have it.

You do that by putting jQuery into no conflict mode.

jQuery’s no conflict mode gets used by WordPress – if you load jQuery up with either a theme or a plugin, jQuery’s in no conflict mode to make sure that it won’t clash with any other JavaScript libraries loaded on the page.

Putting jQuery into no conflict mode is easy enough, you just call the noConflict() method like this;

jQuery.noConflict();

Now that jQuery no longer uses the $, you have a few options for how you’ll go about taking advantage of the jQuery library’s awesomeness without using the $.

First, of course, you can just use the full name, jQuery. So a selector might look like this:

var listItems = jQuery('ul li');

That works fine, but it’s extra typing. If you’re just adding a couple of lines of JavaScript, you probably won’t mind, but if you’re writing a lot, it might make a lot of extra work.

You can also use a special kind of function, called a self-invoking function (big programmer words!), to contain the $. It’s a little confusing if you try to explain it, so let’s just look at some code:

(function($) {
var listItems = $('ul li');
})(jQuery);

Basically, for any code you write inside that function, the $ is equal to jQuery, but for any code outside that function, the $ is not equal to jQuery. If you were using two JavaScript libraries and had put jQuery into no conflict mode, you could write all of your jQuery code inside that function and all of your other JavaScript outside that function, and everything would work as expected.

Finally, you can also give jQuery a different nickname. That looks like this:

var $j = jQuery.noConflict();
var listItems = $j('ul li');

You can make that shorthand any legal variable name. And it’s fine if the variable name contains a $, as long as there’s at least one other character there too. You can choose a short 1 or 2-character name, and save yourself a lot of typing.

Using jQuery in no conflict mode is a great way to make sure you don’t accidentally clash with other JavaScript libraries that might get loaded on the page. It’s an especially handy technique when you’re working with something like WordPress, where a site owner might install a theme or plugins that use different JavaScript libraries.

See you next week for more jQuery learning!

Google+