我有6個圖像(3個全尺寸和3個縮略圖),並將每個圖像url加載到一個javascript變量中。在我的頁面上,我有3個圖像顯示:一個全尺寸和兩個縮略圖。當用戶點擊縮略圖時,我希望全尺寸圖像顯示全尺寸版本的點擊縮略圖,縮略圖顯示以前全尺寸圖像的縮略圖版本。JQuery圖像交換
我可以用下面的腳本得到這個問題。我還可以建立一個巨大的if/then塊來檢查所有情況,但我正在尋找更優雅的東西。有任何想法嗎?
謝謝!
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
}
}
是的,這個作品!雖然對於使用此功能的其他人,請不要忘記在data-main =「 – 2011-12-14 16:49:25