Jump to content

JS / PHP rollovers not quite working


kingnutter

Recommended Posts

Hi there,

 

I'm building rollover navigation by year using js and php. Everything is working fine except for the year 2001.

 

I can link to the js file if you need to see it, but essentially it uses the file xxx_black.png as normal and finds xxx_red.png to use for the rolled over image.

 

All images exist in the correct directory and have been created in the required colour.

 

Any ideas?

 

<?php

$year = 2009;

while ($year >= 2001)
{
?>

<td>
<script>
prepareImageSwap(document.body);
</script>
<a href="search_results.php?&yearsearch=<?php echo $year; ?>">
<img src="/images/<?php echo $year; ?>_black.png" alt="<?php echo $year; ?>"></a></td>
<td><img src="/images/banner_colon.png" alt=":"></td>

<?php $year--;

}
?>

</tr>
</table>

Link to comment
Share on other sites

I Googled CSS rollovers and couldn't find any solutions that didn't involve JS in one way or another.

 

Any pointers to the best angle would be very welcome.

 

You won't need JavaScript if the rollovers are for hyperlinks only.  For generic images that aren't links and other things, IE has had some issues getting :hover to work on anything other than anchors.  I believe they only addressed that problem in IE7, so if you're not only using rollovers for links, and you do care about legacy browsers, then yeah, you'll need JS.

 

Anyhoo, CSS rollovers are incredibly easy to create.  The basic premise is to have both your 'on' and 'off' images in the same image file, either one on top of the other, or one next to the other.  You set this image as the background of the element, positioning it so only the 'off' half is seen by default.  During :hover, you then shift this background image so the 'on' part is visible.  Some links:

 

http://www.alistapart.com/articles/sprites

http://www.oreillynet.com/pub/a/javascript/2001/03/23/rollovers.html

http://www.elated.com/articles/css-rollover-buttons/

Link to comment
Share on other sites

This is really so much better to be done in CSS. If you want to know why its not working in JS for academic reasons, post the HTML output (I.E. no PHP) and the javascript function. But if you are just trying to get the job done properly, stop wasting time and switch to CSS rollovers. Your JS rollovers won't work for many people, where CSS will work for pretty much everyone, and be less resource intensive.

Link to comment
Share on other sites

OK. I've had a good read up about this.

 

The only problem, if you look at my original code, is that I am generating the (year) image links using iteration of a decreasing string.

 

How can I achieve this if CSS requires a definite image URL, or is there a way to incorporate PHP into it?

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.