Image rollover on primary links Drupal howto

Tested on:

Do you remember my Customize links menu in Drupal mini-howto? You can use it to change text links in image links renaming and customizing theme_links function (from and adding it to your template.php.

On your template.php file, you have something like this:
$imglink_path = path_to_theme() . “/my_zen_subtheme/menu/” . str_replace(” “,”_”,strtolower($link[‘title’]) . $imgsuffix . “.gif”);
$imglink = theme_image($imglink_path, $alt = $link[‘title’], $title = $link[‘title’], $attributes = NULL, $getsize = FALSE);
$imglink = $link[‘title’];

  1. Search for images in a defined folder by link title: if link title is “My Nice Link”, a “my_nice_link.gif” image is used.
  2. Generate HTML code for “my_nice_link.gif”. Note: getsize is set to FALSE to avoid errors on rollover
  3. If Drupal cannot generate HTML code for image (e.g. file not found) link is rendered as plain text

If you want to add a rollover effect on primary links images, you have to add this JQuery javascript code on page.tpl.php:

<script type="text/javascript">// <![CDATA[
$("#primary a").mouseover(function ()
$rollsrc = $(this).children("img").attr("src");
$matches = $rollsrc.match(/_active.gif$/);
if (!$matches) {
$rollON = $rollsrc.replace(/.gif$/,"_active.gif");
$(this).children("img").attr("src", $rollON);

$(“#primary a”).mouseout(function ()
$(this).children(“img”).attr(“src”, $rollsrc);
// ]]></script>

Where “_active” is the rollover image.

See also:


%d bloggers like this: