Jonathan Miles, Freelance Web Designer

Resizing images with jQuery

September 7th, 2009

jQuery

The other week I came across a post about resizing images with jQuery. The idea is simple: use JavaScript to detect the size of the browser window (or whatever metric works best for you) and then replace any images with smaller versions if needed.

My immediate thought was that this technique would work great on the splash page of the Insect Dynasty website that I worked on recently, since the splash image was slightly larger than I would’ve liked when viewed at 1024 x 768:

What a visitor would have seen at 1024 x 768

Before: what a visitor would have seen at 1024 x 768

Even though you really need to be using a higher resolution to view the artwork on the website easily I wasn’t happy to leave the splash page like this. Besides, I wanted to try this technique out. So I wrote an inline script for the splash page using jQuery:

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
jQuery(document).ready(function() {

        // Switches between the small and large splash image as appropriate.
        function resizeSplash() {
       
            var $window = jQuery(window);
            var $body = jQuery("body");
            var $enter = jQuery("#enter");
           
            if ($window.height() < 750) {
                $body.css({
                    backgroundPosition: "center 0",
                    backgroundImage: "url('<?php bloginfo('template_url'); ?>/images/splash-background-small.jpg')",
                    minHeight: "0"
                });
                $enter.css("marginTop", "375px");
            } else {
                $body.removeAttr("style");
                $enter.removeAttr("style");
            }
        }

        // Initial call to image resizing.
        resizeSplash();

        // Resizes the splash image, if needed, when the visitor resizes their browser.
        jQuery(window).bind("resize", function() {
            resizeSplash();  
        }
       
    );

How it works

The key part of the script is where it checks if the browser viewport is less than 750 pixels tall – if so the background image on the body element is swapped. I also make some adjustments to the background-position and min-height properties, as these are also set in the stylesheet and need to be overridden as well when changing to a smaller splash image.

When the viewport height is 750 pixels or greater I simply remove the inline style attributes that jQuery would have set via calls to the css() function. This is the best way to revert back to using the larger splash image because it’s simple code, which is always a good thing – it makes the browser go back to using the rules from the stylesheet. That’s much better than making another call to css(), which would require duplicating bits of style definitions unnecessarily.

The technique relies on JavaScript being enabled so there may be some visitors who don’t enjoy its benefit. But it’s still an improvement for the people who do use JavaScript, as you can see:

With a smaller image the splash page fits nicely

After: with a smaller image the splash page fits nicely

The evil of splash pages

As an aside I’m aware that splash pages aren’t all that popular – they’re often just an unwanted obstacle for visitors, so there aren’t that many cases where I’d recommend using one on a website (not without really thinking it over carefully). In this case the splash page is partly setting the tone for the website and, being a website with a strong artistic focus, I consider the splash image to be part of the website’s content.

As splash pages go it’s also rather lightweight, unlike the frankly obnoxious ones you sometimes see which have heavyweight flash animations. That’s very difficult to pull off.

Fingerjig 1.3: jQuery on-screen keyboard

August 20th, 2009

Fingerjig

Fingerjig 1.3 is now available to play and it comes with a new feature that I think will particularly help players who are learning to type. I’ve had to restrict the new feature to Firefox only, so I’ll explain why I had to do that in a moment. For those of you who are using Firefox you’ll now see an on-screen keyboard directly beneath the game:

Fingerjig 1.3's shiny jQuery based on-screen keyboard

Fingerjig 1.3's shiny jQuery based on-screen keyboard

(Just to go off on a quick tangent: if you’re like me you might be wondering what “exsanguinating” means. The answer is “to be drained of blood” – how delightful. Now where was I…)

As you type the corresponding keys on the on-screen keyboard will light up, giving you an idea of where your fingers are. It’s a great aid when you’re starting out and trying to get your fingers onto the correct keys, as you shouldn’t have to look down so much. If you’re already a fast typer then you might just enjoy seeing the keys light up as your fingers whiz around the keyboard. I’m sure some players will find it distracting, so simply click the “close” link and it won’t bother you again. Simple.

Just for Firefox

And now to explain why the on-screen keyboard is only available for Firefox users. The reason is that I implemented it using jQuery, which is a great Javascript framework. Taking this approach allowed me to knock the keyboard together relatively quickly and I also found it fun to do. Unfortunately not all web browsers are equal and the performance of Javascript varies too much for this feature to work satisfactorily in other browsers, such as the all important Internet Explorer.

So that’s why I’ve reluctantly had to restrict the new feature to just Firefox. About 40% of visitors to this website use Firefox, which is quite respectable. But obviously I’d like to roll this feature out to the other 60% of visitors, so I’ll have to try and find some time to write a pure Flash version.

Play Fingerjig