4

我是 jQuery 新手,所以这个问题的答案可能很简单:

我有一个图像,我想用它做几件事。当用户单击“缩放”图标时,我正在运行“图像工具”插件 ( http://code.google.com/p/jquery-imagetool/ ) 以加载更大版本的图像。该插件在图像周围创建一个新的 div 并允许用户平移。

当用户单击替代图像时,我将删除旧图像并加载新图像。

当用户单击替代图像,然后单击缩放按钮时,问题就出现了 - imagetool 插件创建了新的 div,但图像出现在它之后......

代码如下:

// Product Zoom (jQuery)
$(document).ready(function(){
$("#productZoom").click(function() {

    // Set new image src
    var imageSrc = $("#productZoom").attr("href");
    $("#productImage").attr('src', imageSrc);   

    // Run the imagetool plugin on the image
    $(function() {
        $("#productImage").imagetool({
            viewportWidth: 300,
            viewportHeight: 300,
            topX: 150,
            topY: 150,
            bottomX: 450,
            bottomY: 450
        });
    });
    return false;
});
});


// Alternative product photos (jQuery)
$(document).ready(function(){
$(".altPhoto").click(function() {

    $('#productImageDiv div.viewport').remove();
    $('#productImage').remove();

    // Set new image src
    var altImageSrc = $(this).attr("href");

    $("#productZoom").attr('href', altImageSrc);

    var img = new Image();
    $(img).load(function () {
        $(this).hide();
        $('#productImageDiv').append(this);
        $(this).fadeIn();
    }).error(function () {
        // notify the user that the image could not be loaded
    }).attr({
        src: altImageSrc,
        id: "productImage"
        });

    return false;       
});
});

在我看来,imagetool 插件一旦被替换为新图像就无法再看到#productImage 图像......所以我认为这与绑定有关?如因为页面加载后新图像添加到dom中,iamgetool插件无法再正确使用它......是这样吗?如果是这样,任何想法如何处理它?

4

3 回答 3

7

嘿!我自己整理的...

事实证明,如果我完全删除包含的 div,然后用 .html 重写它,imagetool 插件会再次识别它。

有兴趣的人的修改代码:

$(document).ready(function(){

  // Product Zoom (jQuery)
  $("#productZoom").click(function() {

    $('#productImage').remove();
    $('#productImageDiv').html('<img src="" id="productImage">');

    // Set new image src
    var imageSrc = $("#productZoom").attr("href");
    $("#productImage").attr('src', imageSrc);   

    // Run the imagetool plugin on the image
    $(function() {
        $("#productImage").imagetool({
            viewportWidth: 300,
            viewportHeight: 300,
            topX: 150,
            topY: 150,
            bottomX: 450,
            bottomY: 450
        });
    });

    return false;
  });


  // Alternative product photos (jQuery)
  $(".altPhoto").click(function() {

    $('#productImageDiv div.viewport').remove();
    $('#productImage').remove();

    // Set new image src
    var altImageSrc = $(this).attr("href");

    // Set new image Zoom link (from the ID... is that messy?)
    var altZoomLink = $(this).attr("id");

    $("#productZoom").attr('href', altZoomLink);

    var img = new Image();
    $(img).load(function () {
        $(this).hide();
        $('#productImageDiv').append(this);
        $(this).fadeIn();
    }).error(function () {
        // notify the user that the image could not be loaded
    }).attr({
        src: altImageSrc,
        id: "productImage"
        });

    return false;       
  });
});
于 2008-09-25T14:14:07.137 回答
1

您可以尝试将productZoom.click()函数抽象为命名函数,然后在更改为备用图像后重新绑定它。就像是:

// Product Zoom (jQuery)
$(document).ready(function(){
$("#productZoom").click(bindZoom);

// Alternative product photos (jQuery)
$(".altPhoto").click(function() {

        $('#productImageDiv div.viewport').remove();
        $('#productImage').remove();

        // Set new image src
        var altImageSrc = $(this).attr("href");

        $("#productZoom").attr('href', altImageSrc);

        var img = new Image();
    $(img).load(function () {
                $(this).hide();
        $('#productImageDiv').append(this);
        $(this).fadeIn();
    }).error(function () {
        // notify the user that the image could not be loaded
    }).attr({
                src: altImageSrc,
                id: "productImage"
                });
        $("#productZoom").click(bindZoom);
        return false;

});  
});

function bindZoom() {
        // Set new image src
        var imageSrc = $("#productZoom").attr("href");
        $("#productImage").attr('src', imageSrc);       

        // Run the imagetool plugin on the image
        $(function() {
                $("#productImage").imagetool({
                        viewportWidth: 300,
                        viewportHeight: 300,
                        topX: 150,
                        topY: 150,
                        bottomX: 450,
                        bottomY: 450
                });
        });
        return false;
}

此外,将您的两个 ready() 块滚动到同一个块中。

于 2008-09-25T11:48:48.063 回答
0

首先,我有一个问题,是 .altPhoto 链接还是图片?因为如果它的图像那么这条线是错误的

var altImageSrc = $(this).attr("href");

它应该是

var altImageSrc = $(this).attr("src");

这是我一眼就能找到的唯一东西

于 2008-09-25T12:10:52.317 回答