I have 6 images (3 full size and 3 thumbnails) and have loaded each image url into a javascript variable. On my page I have 3 images displaying: one full size and two thumbnails. When the user clicks a thumbnail, I’d like for the full size image to display the full size version of the clicked thumbnail, and the thumbnail to display the thumbnail version of the previous full size image.
I can get this to occur once with the script below. I could also build a giant if/then block to check all cases, but I’m looking for something more elegant. Any ideas?
Thanks!
function add_image_header(){
global $post;
$image_header = get_post_meta( $post->ID, 'image_header', true );
$image_one_full = get_post_meta( $post->ID, 'image_one_full', true );
$image_one_cropped = get_post_meta( $post->ID, 'image_one_cropped', true );
$image_two_full = get_post_meta( $post->ID, 'image_two_full', true );
$image_two_cropped = get_post_meta( $post->ID, 'image_two_cropped', true );
$image_three_full = get_post_meta( $post->ID, 'image_three_full', true );
$image_three_cropped = get_post_meta( $post->ID, 'image_three_cropped', true );
$page_meta_desc = get_post_meta( $post->ID, 'thesis_description', true );
if($image_header){
?>
<script type="text/javascript">
$(document).ready(function(){
$("#thumb_one").click(function(){
var imageOneFull = "<?php echo $image_one_full;?>";
var imageOneCropped = "<?php echo $image_one_cropped;?>";
var imageTwoFull = "<?php echo $image_two_full;?>";
var imageTwoCropped = "<?php echo $image_two_cropped;?>";
var imageThreeFull = "<?php echo $image_three_full;?>";
var imageThreeCropped = "<?php echo $image_three_cropped;?>";
$('#thumb_one').click($('#main_image').attr("src",imageTwoFull));
$('#thumb_one').click($('#thumb_one').attr("src",imageOneCropped));
});
});
</script>
<div id="img_header_container">
<img src="<?php echo $image_one_full;?>" id="main_image"/>
<img src="<?php echo $image_two_cropped;?>" id="thumb_one"/>
<img src="<?php echo $image_three_cropped;?>" id="thumb_two"/>
<div id="heading_text"><h2><?php echo get_the_title($ID) ?></h2><?php echo $page_meta_desc;?></div>
</div>
<?php
}
}
I give you two solutions.
1) Use all 3 three thumbnails (which I like more)
HTML:
Jquery:
2) Use 2 thumbnails (as requested)
HTML:
Jquery: