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 theme.inc) 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);
if(empty($imglink))
$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[
$(document).ready(function()
{
$("#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:

Update:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s