Using Rollover Effects

Yesterday I wrote about creating rollover images in Wordpress. This is all very nice, but what’s the point?

1. Engagement

Last term I sat with some students and asked them to show me things that they liked on various websites. There was no agreement whatsoever about preferred colour schemes, design styles or layouts, but almost everyone liked games, videos and interactive content. I am reliably informed that buttons which change when the mouse is rolled over them are ‘cool’.

If a set of buttons with rollover effects will encourage students to access the site I’ve set up for them, then it’s worth doing. (Yes I know, it’ll take much more than that, but it’s still worth it.)

2. Simple interactive content

Once you’veset up the use of rollover effects for the first time, it makes it possible to put some simple interactive content into web pages or posts in a class blog. Here’s a few examples:

Flashcards:

I’m going to ask Y10 to make some of these for the perimeter/area/volume formulae that they need to learn for their exam in January.

Revision prompts:

Can you name the sides in this triangle?
(Roll your mouse over the image to see the answer.)

Giving information or clues:

In a flowerpot in my garden I found 3 slugs, 2 spiders and 4 beetles.
How many legs were there altogether in the flowerpot?

Any more suggestions?

Creating Rollover Images in Wordpress

My student website is long overdue for a major revamp. I decided to rebuild the site using Wordpress, which is going well, but some of the things I want to do are not automatically built in, so I thought I’d try to write some very simple guides as I work out how to do things.

My students all seem to like rollover images, so I wanted to incorporate them. Here’s how I did it:

You will need:

- A self-hosted Wordpress blog. This simply will not work for a blog that is hosted on Wordpress.com, because this process requires Javascript, which is not allowed on WordPress.com blogs. (That’s one of the many reasons I opted to go for the self-hosted option.)
- A folder somewhere on the web to hold your images.
- A text editor, such as Notepad.

Step One – Create some images

You will need two images – the basic image that will appear at first and the image that will replace it when the user hovers their mouse over. Obviously these images should be the same size! I created gifs, but pngs and jpegs will also work. You will need to stick to one format, don’t use a mixture of file types.

Your rollover image needs to have the same name as the basic image, but with an ending such as _over added at the end: e.g. my-image.gif and my-image_over.gif

Once you have finished, upload your images to the web. You must save the two images in the same folder.

Step Two – Create and upload a Javascript file

Actually, no knowledge of Javascript is required here, because Atlanta Jones has created and shared a tutorial and a script that works perfectly. If you have created gif files and used the _over ending on your file names, you can simply download and use this file: rollover.js. Alternatively you can copy and paste this script into a text editor (NOT MS Word – I used notepad) and save it as rollover.js:

jQuery(function($) {
$(document).ready(function() {

		// Preload all rollovers
		$("#hoverlink img").each(function() {
			// Set the original src
			rollsrc = $(this).attr("src");
			rollON = rollsrc.replace(/.gif$/ig,"_over.gif");
			$("<img>").attr("src", rollON);
		});

		// Navigation rollovers
		$("#hoverlink a").mouseover(function(){
			imgsrc = $(this).children("img").attr("src");
			matches = imgsrc.match(/_over/);

			// don't do the rollover if state is already ON
			if (!matches) {
			imgsrcON = imgsrc.replace(/.gif$/ig,"_over.gif"); // strip off extension
			$(this).children("img").attr("src", imgsrcON);
			}

		});
		$("#hoverlink a").mouseout(function(){
			$(this).children("img").attr("src", imgsrc);
		});

	});
});

If you have changed filetypes or endings, you will need to edit the script with your text editor to change anything that says .gif to .png or .jpg, or to change anything that says _over to match the ending you used on your file names.

The script tells Wordpress to pre-load your rollover images and what to do when the mouse is rolled over and off. It is dependent on a tag called hoverlink (I made that name up, you can change it if you want). We’ll use this tag in step four.

Once you have your Javascript file, you need to upload it. It needs to go in the root folder for the theme that you are using in Wordress.

Step Three – tell Wordpress to use the file that you just created

The good news is that Wordpress comes bundled with JQuery, so all you have to do is tell it to use it. Even beter, Brian at frodesigns.com has kindly shared the code that is needed.

You will need to edit your header.php file, find this by clicking on Appearance, then Editor. First find the <head> tag. NB in some themes the the <head> tag may not look exactly like that, so you may need to look for something like this: <head with-some-extra-code-here>.
Once you’ve found the <head> tag, copy and paste this code under it:

    <?php
       wp_enqueue_script('jquery');
       wp_enqueue_script('jquery-core-ui');
       wp_enqueue_script('jquery-tabs-ui');
       wp_enqueue_script('hoverlink', get_bloginfo('template_directory').'/rollover.js');
       wp_head();
    ?>

You can see the penultimate directs Wordpress to the jQuery code for the image rollover effect that we are creating. It also refers to the hoverlink tag that we’ll use in the next step.

Step Four – putting your images into a post or page

Now you can actually put your images into a post or page. The last bit of technical trickery is to put your images inside a section tagged "hoverlink". To do this, use the html editor to create your page or post, rather than the visual editor.

This is the code that I used to create the image at the end of this post:


<div id="hoverlink">
<a href="http://www.morethanmaths.com/teacher"><img src="http://www.morethanmaths.com/teacher/sample-files/2009/12/my-avatar.gif" border="0" alt="My Avatar" width="220" height="220" /></a></div>

You can see that the code relating to the image is contained inside two tags: <div id="hoverlink"> and </div>. Obviously you will need to change the highlighted sections to match your own choice of hyperlink, image location and size.

Want a rollover image that isn’t hyperlinked? No problem:


<div id="hoverlink">
<a><img src="
http://www.morethanmaths.com/teacher/sample-files/2009/12/my-avatar.gif" border="0" alt="My Avatar" width="220" height="220" /></a></div>

Want your rollover image in a sidebar? Add a text widget and put the code into that. Wordpress will recognise the code and display the image with the rollover effect enabled.

Here’s the finished product:

There’s also an example of a rollover image in the sidebar of this blog, try rolling your mouse over the juggler to see it action – it’s a fairly subtle effect this time.

I’m not a Javascript expert – in fact I know next to nothing – but I got this working with little more than copy and paste. I’ve tried this with Wordpress 2.9 and 2.8.6, using the Atahualpa and the Default themes. I tested the finished product in both Firefox and Internet Explorer. As far as I can tell, it’s working perfectly.

Merry Christ-maths!

Whatever form your festivities may take, I hope you have a Merry Christmas!

I don’t know why I associate puzzles with Christmas. Maybe it’s childhood memories of tiny plastic tangrams from the insides of crackers, maybe it’s because I’ve been lucky enough to receive lots of puzzle books as gifts over the years. Anyway, here’s one of my favourites, hope you like it.

#movemeon – the book

Move Me On - Buy or DownloadWhat happened when Doug Belshaw asked educators to share hints and tips using Twitter and the #movemeon hashtag?  Lots of people joined in to share their ideas and create a really interesting crowd-sourced resource.

Doug created an archive of the tweets using Twapperkeeper, which is great,  but it’s not likely to reach teachers that aren’t already using Twitter. So he made a book as well. Brilliant.

This isn’t just for new or trainee teachers, we’re all learners, I’m sure everyone can find something in here to make them think  ‘I’ve got to try that’, or ‘I used to do that – why did I stop?’

I have to say that Doug and Stuart Ridout have done a fantastic job with the book. Best of all, the e-book is free, so what are you waiting for? Go and download one.

I’ll definitely be sharing copies at school next term.

PDF/ebook (free download): http://bit.ly/movemeonpdf

Book (at cost price): http://bit.ly/movemeonbook

Badges to promote the book are at: http://bit.ly/movemeonbadges
(no need for CC attribution, etc.)

On the move

If you are reading this – then welcome to my blog’s new home!

I enjoyed using Wordpress.com – and I fully intend to continue to do so – but I always intended to use self-hosted Wordpress for this blog. It just opens up so many more options. Since my old web host couldn’t seem to provide the necessary support for me to run an up to date release of Wordpress, that presented a bit of a challenge. So I’ve moved: firstly to a new web hosting provider, then to a self-hosted blog.

Unfortunately I couldn’t automatically redirect any readers from my old place at Wordpress.com. So if you’ve followed the trail of breadcrumbs and made it here – thanks for making the effort!

Get Adobe Flash playerPlugin by wpburn.com wordpress themes