Robin Whittleton

Easy image rollovers

Recently themeforest.net ran a quick tutorial on how to achieve an image slide effect similar to our homepage. I thought I’d go into some more detail about the design decisions we made.

Here’s the exact jQuery code used:

$('#body_home #our_work li').hover(function(){
	$(this).find('img').animate({top:'139px'},{queue:false,duration:500});
}, function(){
	$(this).find('img').animate({top:'0px'},{queue:false,duration:500});
});

There’s really not that much! First, we select all the <li> elements inside our ‘our_work’ block on the homepage. Each of these is a picture and a block of text wrapped in a link. By using an id to define the block we let jQuery find it very quickly: the browser maintains a list of all elements with an id attribute that jQuery can hook directly into.

Once we’ve got our blocks we can apply hover functionality to them. The first method passed into the hover handler is our slide down animation and the second is our slide up. We find the <img> element inside each block and animate it away from the top. In our case we’ve chosen half a second (500ms) for a complete slide down and up, and by passing in queue:false we make sure the slide up cancels the slide down if the user mouses out of the block while it’s animating. I’ve chosen to animate the image down by 139px because that’s the exact height of the image: just enough to take it off screen.

From a CSS point of view it’s still kept pretty simple:

#our_work li{
	float:left;
	margin:0 30px 95px 0;
	padding: 5px 7px 8px 5px;
	width:258px;
}

#our_work li a {
	display: block;
	position: relative;
	overflow: hidden;
	height: 107px;
	width: 226px;
	padding: 16px;
}

#our_work li a:focus, #our_work li a:hover { text-decoration: none; }

#our_work li img {
	position: absolute;
	top: 0;
	left: 0;
	height: 139px;
	width: 258px;
}

The <li>s are floated left to make them line up and given some margin and padding, and a set width. Inside each <li> we have an <a> element which wraps around the <img> and associated text. We give this display:block and a fixed height and width so that it acts as complete clickable area. Because the <img> is inside this it’s also clickable. By making the <a> position relative it acts as a positioning parent for the <img>: when we set the <img> to position absolute it’ll reference its co-ordinates from the top left of the <a>. Finally, the overflow:hidden hides the <img> when it slides outside the <a>. Simple!

Final touches we’ve added are to style the text with a nice font and size and add a little background folded-corner image, but this is obviously easily customisable for whatever style you want.

One thing that’s very important to take care of whenever you’re writing Javascript is to cope when JS isn’t available. For our blocks we decided to just show the images. Because they are still inside the link they can be clicked, so the user can go through to the info page for that project. Screenreader users should also be read the description text from behind the image as we’re not hiding it.

Drop me a comment if you’re interested in hearing about other jQuery effects we’ve done on this site and others from our portfolio and I’ll write some more about it.

Tags: image, unobtrusive, javascript, jquery

Comments: 10

Chris
commented on

Your website is an amazing example of accessibility. The site degrades perfectly without Javascript / CSS and is looks pixel perfect in any browser on any platform. You use clear semantic markup and have coupled this with an amazing design. I would *love* to work alongside people with the same passion for standards as you do!

I've been looking into jQuery to enhance UX and i think you've got it perfect. The feature you describe here is great a great addition for JS users.

And youre UK based... Niceone!

Ive subscribed to the RSS, will be keeping up with what you guys write..

A Hassan
commented on

eyo, nice work man.

its a good thing that you wrote this blog because u get more traffic + ur sharing ur work.

jers!

webbo
commented on

Thank you very much for sharing, I find this type of quick tutorial so helpful.

Medinlú
commented on

Nice trick! I´m trying to make the thumbnails appearing with a fadein transition once the page is loaded, but I have a problem with the background image on the

  • You know if that would be possible?

    Cheers!

  • Robin
    commented on

    Hi Medinlú, I’m not sure I understand your question? Feel free to email us and I’ll see if I can help in more detail.

    eric
    commented on

    Robin thanks for the tutorial. Kyan uses some of the coolest jQuery effects that i've seen. Kyan is definitely one of my favorite design agencies.

    I love the latest work done for the heartfelt agency. I think the design is awesome seasoned with some topnotch jQuery.

    I was wondering if you could do a tutorial to demonstrate how you used jQuery to make the sign in form magically appear on the same page. I looked at the source code but I couldn't get it to work.

    thanks!!

    Wilfried
    commented on

    You know if that would be possible?

    Indeed it is!

    Peter Moran
    commented on

    I find this type of quick tutorial so helpful too.

    JKLstar
    commented on

    I'm a trying to build a website and I like this effect very much.

    I looked at both tutorials and still can't figure out how to link the image text (the yellow one with corners) to another website???

    Also, how can I center the image? I'm using only one image in the middle of a white background (no header/no menu/only one image) Front image size = 500px width 400px height. Text image size = 500px width 400px height (like your yellow one with text) linking to another website.

    Huh... hope I explained myself well. Thank you.

    Regards,

    JKLstar

    Lori
    commented on

    Love your site, and love this rollover effect. I'm with JKLstar. I can get ThemeForest's script to work, but I can't find a way to link the images. It looks like you guys have it figured out (since you were the first to do it), but I can't make it work with your code either. Anyway you guys would be willing provide some downloadble, simple source files?

    Add a comment

    Note: comments are moderated before publication.

    Most Popular

    The Entifyr

    Steven Wake

    The Entifyr is a small web application that started life as an personal experiment to automate the removal of problem characters from client supplied content (usually Microsoft Word documents). The offending characters are replaced with corresponding entities (hence the name) al…

    Now residing at 171 High Street, Guildford

    Peter Roome

    NEW ADDRESS: Kyanmedia, Guildford, 171 High Street, Guildford, Surrey, GU1 3AJ Yes thats right, we have made the big move, a week earlier than the scheduled 24th July. The impromptu decision was made mid morning Friday (17th July, 2009) after discovering, Smithbrook was…

    Number one in Google

    Paul Sturgess

    Good listings across multiple search engines can make or break a website, at Kyan we believe there are no real secrets to search engine optimisation (SEO). Transparency with our clients is key, we don’t keep our techniques behind lock and key as we believe SEO is not just the re…