0

所以我一直在玩这个,但无法让绑定的点击事件从 jqzoomed 元素触发。

HTML:

<div class="product-clicktozoom-image">
    <div class="product-clicktozoom-image-main">
        <a href="http://example.com/image.jpg" class="jqzoom" title="Image" rel="gall"><img itemprop="image" id="mainimage" src="http://exampe.com/image.jpg" alt="Image" title="Image"/></a>   
    </div>
</div>

JS:

jQuery(document).load(function() {
    jQuery('#mainimage').bind('click',function(){
            var fancyImage = jQuery('#mainimage').attr('src');              
            jQuery.fancybox.open(fancyImage); 
    });
});

jqzoom 创建了几个额外的 div,因此最终的页面结构实际上如下所示:

<div class="product-clicktozoom-image-main">
    <a href="example.com/image.jpg" class="jqzoom" title="" rel="gall" style="outline-style: none; text-decoration: none;">
        <div class="zoomPad">
            <img itemprop="image" id="mainimage" src="http://example.com/image.jpg" alt="Image" title="Image" style="opacity: 1;">
                <div class="zoomPup" style="display: none; top: 105px; left: 145px; width: 126px; height: 126px; position: absolute; border-width: 1px;"></div>
                <div class="zoomWindow" style="position: absolute; z-index: 5001; cursor: default; left: 0px; top: 0px; display: none;">
                    <div class="zoomWrapper" style="border-width: 1px; width: 355px; cursor: crosshair;">
                        <div class="zoomWrapperTitle" style="width: 100%; position: absolute; display: none;"></div>
                        <div class="zoomWrapperImage" style="width: 100%; height: 355px;">
                            <img src="http://example.com/image.jpg" style="position: absolute; border: 0px; display: block; left: -411.26760563380276px; top: -298.5915492957746px;">
                        </div>
                    </div>
                </div>
                <div class="zoomPreload" style="visibility: hidden; top: 156px; left: 132.5px; position: absolute;">Loading zoom</div>
        </div>
    </a>
</div>

我尝试将 click 事件与 zoomPad 类以及 zoomWrapperImage 元素中的 img 绑定到 #mainimage,因为当您将鼠标悬停在图像上时,这似乎是顶部元素。

如果他们单击缩放,我想要做的就是在花式框中打开完整图像(当前缩放发生在鼠标悬停时)。我知道fancybox.open(fancyImage); 有效,我可以直接在控制台中成功运行 click 函数中的代码,我只是无法从 click 事件本身触发它。

帮助!

4

1 回答 1

2

这最终变得非常简单,点击事件需要在 .zoomPad 上进行绑定,我使用 window.load 代替 (document).ready 以确保所有元素都已加载。

最终JS:

jQuery(window).load(function() {
    jQuery('.zoomPad').bind('click',function(){
            var fancyImage = jQuery('.zoomWrapperImage img').attr('src');      
            jQuery.fancybox.open([{href : fancyImage}], {closeClick : true}); 
    });
});
于 2013-09-24T10:23:44.077 回答