views:

23

answers:

1

Hi,

I'm trying to get some rollovers working using jQuery. The trouble is that the image source has to be got using some php code.

If I do it inline like this with javascript it works:

<a href="<?php the_permalink(); ?>"
   <?php $thumburl = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
   onmouseover="document.roll<?php the_ID(); ?>.src='<?php thumbGen($thumburl,300,0,1,0,0); ?>'"
   onmouseout="document.roll<?php the_ID(); ?>.src='<?php thumbGen($thumburl,300,0,1,0,1); ?>'"
   <img src="<?php thumbGen($thumburl,300,0,1,0,1); ?>" name="roll<?php the_ID(); ?>"/>
</a>

$thumburl variable is the exactly that; url of the particular thumbnail. The thumbgen bit does some size and colour conversions. When together they give me a url of a b&w and colour images that get swapped. I'm using WordPress so I have to use the php rather than just specify the image src because the size and colour conversions are done automatically for the user.

First question, is doing the code inline like this bad? Second. I'm struggling to write a jQuery function for the rollover because of the php. I presume it would be like this:

$(document).ready(function() {
   $('.project img').hover(
    funcation(){
    this.src = '<?php thumbGen($thumburl,300,0,1,0,1); ?>';},
    function (){
    this.src = '<?php thumbGen($thumburl,300,0,1,0,1); ?>';});

But I have no idea where to include this: $thumburl = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );

can you even combine php and jQuery in one?

Thanks

A: 

You can simplify the PHP a bit like this:

<a href="<?php the_permalink(); ?>"
   <?php $thumburl = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
   <img src="<?php thumbGen($thumburl,300,0,1,0,1); ?>" data-hover="<?php thumbGen($thumburl,300,0,1,0,0); ?>" />
</a>

Then do something like this in jQuery, using that data attribute:

$(function() {
   $('.project img').each(function() { 
     $.data(this, 'orig', this.src); 
   }).hover(function() {
     this.src = $(this).attr('data-hover');
   }, function () { 
     this.src = $.data(this, 'orig');
   });
});

What this does is store the original src for each image using $.data(), then on hover we grab whatever new src attribute is, which we store in data-hover (a data attribute we created), and when hovering out we get that original src and restore it.

Nick Craver
Hhhmmmm not working. I can't see any reason why it's not though. Do I need to specify anything for 'orign'? The site is only local, I would try and show you otherwise.
beetrootman
@beetrootman - Can you post what your rendered HTML is coming out like, perhaps in a fiddle like this? http://jsfiddle.net/nick_craver/Vvpum/
Nick Craver
Ok here http://jsfiddle.net/Vvpum/2/
beetrootman
@beetrootman - It's working here: http://jsfiddle.net/nick_craver/Vvpum/3/ (though I don't have the images), do you have jQuery loaded properly in the page, no errors in the console?
Nick Craver
Ah, I think I know what it is. Because I'm using WordPress I always have to replace $ with jQuery so the code becomes this:
beetrootman
jQuery(function() { jQuery('.project a img').each(function() { jQuery.data(this, 'orig', this.src); }).hover(funcation() { this.src = jQuery(this).attr('data-hover'); }, function (){ this.src = jQuery.data(this, 'orig'); });});But neither way is working
beetrootman
@beetrootman - Here's that version: http://jsfiddle.net/nick_craver/Vvpum/4/ :) You can use `jQuery(function($) { });` for your `document.ready` and still use `$` inside.
Nick Craver
Brilliant. Thank you so much.
beetrootman
@beetrootman - welcome! :)
Nick Craver
Ok, I've been trying to implement this. However, I want it to trigger when you hover over '.project' not '.project img'. Which means that somewhere I need to specify that it's this (as in .project) image src. The data thing is throwing me.
beetrootman
@beetrootman - That version would look something like this: http://jsfiddle.net/nick_craver/Vvpum/5/ that help?
Nick Craver
yeah thanks again
beetrootman