如何替换elevatezoom图像

时间:2015-06-25 05:54:16

标签: javascript jquery html5

目前我正在使用elevatezoom来缩放我的产品图片。它是一个基本的缩放功能。我有一个大图像,其他人很小。我为此编写了一个代码。当你点击小图片时它会替换为大图像。它改变正确,但是当我在那个图像上悬停时,缩放功能不起作用,请在这里写下我的代码,请你帮帮我。

html代码是

<div class="col-xs-12 col-lg-12"> 
    <img id="main_img" class="img-responsive" src="<?=BASE?>/uploaded_content/product/products_speedboot.jpg" data-zoom-image="<?=BASE?>/uploaded_content/product/products_speedboot.jpg"/>
</div>
<div class="col-lg-12 margin-top-10class other-pic-gallary">
    <div class="col-lg-4 padding-remove-left"><img class="img-thumbnail" src="<?=BASE?>/uploaded_content/product/products_speedboot.jpg" /></div>
    <div class="col-lg-4 padding-remove-left"><img class="img-thumbnail" src="<?=BASE?>/uploaded_content/product/Sports_Equipment_Fittings.jpg" /></div>
    <div class="col-lg-4 padding-remove-left"><img class="img-thumbnail" src="<?=BASE?>/uploaded_content/product/products_speedboot.jpg" /></div>
</div>

脚本代码

$(document).ready(function(){
    $('#main_img').elevateZoom({
        zoomType: "inner",
        cursor: "crosshair",
        zoomWindowFadeIn: 500,
        zoomWindowFadeOut: 750
    });

    $('body').on('click','.other-pic-gallary .img-thumbnail',function(){
        //console.log($(this).attr('src'));
        var img_val = $(this).attr('src');
        $('#main_img').attr('src',img_val);
        $('#main_img').attr('data-zoom-image',img_val);
        $('.zoomWindowContainer div').stop().css("background-image","url("+ img_val +")");
        $('#main_img').elevateZoom({
            zoomType: "inner",
            cursor: "crosshair",
            zoomWindowFadeIn: 500,
            zoomWindowFadeOut: 750
        });

    });
});

1 个答案:

答案 0 :(得分:9)

我已在FIDDLE

为您制作了一个演示
image.on('click', function(){
    // Remove old instance od EZ
    $('.zoomContainer').remove();
    zoomImage.removeData('elevateZoom');
    // Update source for images
    zoomImage.attr('src', $(this).data('image'));
    zoomImage.data('zoom-image', $(this).data('zoom-image'));
    // Reinitialize EZ
    zoomImage.elevateZoom(zoomConfig);
});