Archive for the ‘Javascript’ Category

Dealing With PNG Transparency and Microsoft

By Helium Developer

Tuesday, June 30th, 2009

If there is one thing more frustrating than Internet Exporer’s CSS quirks, it’s the way the ubiquitous browser handles PNGs. The image format, short for “Portable Network Graphics”, has a combination of features that make it an ideal web format for images, yet the handling of the format by IE has made it difficult to deal with for many developers.

Benefits of the Format

PNG has a few different versions which allow it to replace both JPEG and GIF images in nearly all situations on the web. The PNG-8 format uses 8 bits per pixel, and has an indexed color pallet, much like GIF. You can get extremely small PNG images this way if the color pallet is small enough, and for many images in templates around the web, PNG-8 is more than adequate.

For those situations where you need full color, you can instead use the PNG-24 format, which has the same color range as JPEG. Unlike JPEG however, PNG-24 will not artifact your image, and will still keep the image relatively small in size.

The real benefit of PNG however comes in the PNG-32 format, which is full-color like JPEG but offers something call “alpha transparency”. GIFs can be transparent, but the transparency is an all or nothing affair. A pixel is either entirely transparent, or not transparent at all. Alpha transparency however uses 255 “levels of transparency”, allowing every pixel to be partially transparent, letting you create a full-color image which blends seamlessly with any background you place behind it.

Unfortunately, IE has always had trouble implementing alpha transparency in PNG. IE6 just doesn’t support it, and developers have had to rely on Javascript and CSS hacks to modify the browser to support the format. IE 7 introduced support for PNG-32, however as any developer should be used to, it came with quirks.

Supporting Alpha Transparency in IE6

A wealth of solutions exist to help IE6 support PNG-32, and instead of elaborating, we’ll simply be linking to some solutions.

TwinHelix has a fix which tackles both the issue of PNG transparency and the background issues that arise from fixing it. The problem is that in fixing IE’s anomalous behaviour with PNGs, IE no longer allows you the use the background-position: or background-repeat: CSS properties with PNG images. Both the PNG transparency issue, and the resulting positioning issues are tackled in this fix.

DillerDesign offers another fix for your PNG woes, and while this one is slightly better documented, it does not appear to handle the resulting background image issues.

Quirks of IE Support

As with nearly any IE specific problem, PNG support in IE has additional quirks that developers need to be aware of. In all versions of IE, including IE7 and IE8, no matter what PNG fix you have installed on your site, altering the CSS opacity property on an element which uses a PNG with alpha transparency will completely break this transparency. There is no work around for this problem other than to not change or set the opacity of any elements which use PNGs in IE.

We ran into this problem with the tooltips we use for our portfolio and navigation, as our jQuery altered and set the transparency of our items as part of the animations. The solution was to create a separate animation just for IE which doe not change the opacity at all. Curiously, even if you only set the opacity of the element to 1.0 (full opacity) and never change it, IE completely breaks support for alpha transparency.

When dealing with IE, you must be careful what CSS properties you set and where.

While dealing with IE’s quirks is a headache, the browser still represents a large portion of the internet. It’s important for developers to understand the limitations that Microsoft creates for their websites, and to design around them.

Working With jQuery

By Helium Developer

Friday, June 12th, 2009

A lot of developers and website owners alike have been using jQuery for a while now. Yet many people are still confused as to what exactly jQuery is.

Put simply, jQuery is a collection of pre-written Javascript that allows a developer or designer to focus on what they want to do, instead of how to do it.

Javascript: A Standard

Back in the days of Netscape 4.0 or Internet Explorer 5, Javascript was a bit finicky. You might say that it’s implementation was sporadic, haphazard and worst of all: nonstandard.

But today we find ourselves in a very different world. While CSS and to a lesser extent HTML still have some large standardization problems across modern browsers, today’s average web surfer is using a fairly standard Javascript engine, and even better, the browsers are fast enough at implementing it that using it becomes practical.

While Javascript is still a bit of a black-box of mystery to many developers, jQuery is the brainchild of John Resig, a former employee of the Mozilla Foundation (makers of FireFox) and a self-proclaimed “Javascript evangelist”.

Part of the problem with Javascript, Resig discovered, was that few developers had the time or the ability to go through and learn the complex and long-winded syntax of Javascript to do anything useful. But in 2006 he released jQuery to the masses, opening up the interactive web to many.

jQuery: A Focus On The Product

jQuery provided a library of common functions that developers could use.  Want to dynamically add the “ready” class to all forms? Well that’s a simple:

$.('form').addClass('ready');

There’s (almost) nothing to it.

jQuery is used by sites big and small. From this very website to Google and NetFlix.

Using jQuery allows the developer to focus on what they want done instead of how they have to do it, which can be very useful for web UI developers.

But jQuery is really brought to life by the multitude of extensions available, and to give you an idea of just what these extensions do, below you will find links to all the jQuery extensions used on this website.

jQuery Tools
Cycle
Colorbox
Preload

jQuery: A Primer

There are two simple concepts that you need to understand to use jQuery on your site. The first is that you need to include certain files to use the jQuery tools. The second is that you need to initialize the scripts in order for them to turn on.

This is actually a lot simpler than it sounds.

To include jQuery files, whether it’s a core file or a plugin, you simply use code like so in the head tag of your page:

<script language="text/javascript" src="./js/jquery-min.js"></script>

In the above example, you simply need to replace the “src” property with the relative address of the javascript file you want to include.

After you’ve included you files, you can initialize them like so:

$(document).ready(function(){
	$.preload([ 'black_arrow_big', 'black' ], {base:'images/tooltip/',ext:'.png'});
}

This particular line is how this website preloads some of its images using the Preload plugin that was linked earlier in this post.

Generally plugin authors provide many examples of what code you need to initialize their scripts, and in some cases you may even be able to copy and paste.

While jQuery certainly isn’t the perfect solution, for most projects it provides enough functionality without cluttering up the page, and that’s nearly the definition of a good code library.