简体   繁体   English

将高度和宽度从图像点击发送到fancybox

[英]Send height and width to fancybox from an image click

I have say 10 images and when anyone of them is clicked they open up a Fancybox frame (HTML). 我说有10张图像,当单击其中的任何一个时,它们都会打开一个Fancybox框架(HTML)。 The problem is each images has a different sized Fancybox frame that needs to be opened. 问题是每个图像都有一个需要打开的不同尺寸的Fancybox框架。 How can I send the height and width from the image being clicked. 如何从被单击的图像发送高度和宽度。

$("a.various").fancybox({
    beforeClose: function () {
        $("#gallery_spacer").remove();
    },
    openEffect: 'fade',
    openSpeed: 1500,
    closeEffect: 'fade',
    closeSpeed: 400,
    padding: '0',
    width: 660,
    height: 700,
    maxWidth: 660,
    maxHeight: 700,
    fitToView: false,
    autoSize: false,
    closeClick: false,
    autoScale: 'false',
    autoDimensions: 'false',
    transitionIn: 'true',
    transitionOut: 'true',
    type: 'iframe',
    openEffect: 'fade',
    helpers: {
        overlay: {
            css: {
                'background': 'rgba(255, 255, 255, 0.0)'
            }
        }
    }
});


$("a.various1").fancybox({
    beforeClose: function () {
        $("#gallery_spacer").remove();
    },
    openEffect: 'fade',
    openSpeed: 1500,
    closeEffect: 'fade',
    closeSpeed: 400,
    padding: '0',
    scrolling: 'no',
    width: 660,
    height: 1870,
    maxWidth: 660,
    maxHeight: 1870,
    fitToView: false,
    autoSize: false,
    closeClick: false,
    autoScale: 'false',
    autoDimensions: 'false',
    transitionIn: 'true',
    transitionOut: 'true',
    type: 'iframe',
    openEffect: 'fade',
    helpers: {
        overlay: {
            css: {
                'background': 'rgba(255, 255, 255, 0.0)'
            }
        }
    }
});

The HTML HTML

<li><a class="various1 fade " href="FOOTWEAR_SUB_PAGES/MERCURIAL_SUPERFLY.html"><img src="MAIN_IMAGES/MERCURIAL_SUPERFLY-2.jpg"  border="0" /></a></li>
<li><a class="various2 fade " href="FOOTWEAR_SUB_PAGES/AIRMAX_MOTO.html"><img src="MAIN_IMAGES/AIRMX-22.jpg"  border="0"  /></a></li>

You basically need a single script to bind all your links to fancybox, then use data-* (HTML5) attributes to pass the width and height individually 基本上,您需要一个脚本来将所有链接绑定到fancybox,然后使用data-* (HTML5)属性分别传递widthheight

You could use this html : 您可以使用以下html:

<li><a class="fancybox" data-width="660" data-height="700"  href="FOOTWEAR_SUB_PAGES/MERCURIAL_SUPERFLY.html"><img src="MAIN_IMAGES/MERCURIAL_SUPERFLY-2.jpg"  border="0" /></a></li>
<li><a class="fancybox" data-width="660" data-height="1870" href="FOOTWEAR_SUB_PAGES/AIRMAX_MOTO.html"><img src="MAIN_IMAGES/AIRMX-22.jpg"  border="0"  /></a></li>

... and fetch the size of each link using fancybox beforeShow callback like : ...并使用fancybox beforeShow回调获取每个链接的大小,例如:

beforeShow: function () {
    this.width  = $(this.element).data("width");
    this.height = $(this.element).data("height");
}

... where $(this.element) refers to each clicked element. ... $(this.element)表示每个单击的元素。

See JSFIDDLE 参见JSFIDDLE

Please notice in the fiddle that I commented out some API options that are not valid in v2.x (they are for v1.3.4 and are not compatible) 请我注释掉不在V2.X有效一些API选项小提琴通知 (它们是V1.3.4和不兼容)

you can get the width and height of the image like that: 您可以像这样获得图像的宽度和高度:

var w = $("a.various1 img").width();
var h = $("a.various1 img").heigth();

Since you are loading content into an iframe, you cannot get the required dimensions for fancybox until after the html is rendered in the iframe. 由于您是将内容加载到iframe中,因此在iframe中呈现html之后,才能获得fancybox所需的尺寸。

What you can do is use onComplete to resize the fancybox after the html is rendered as described here . 你可以做的是使用的onComplete所描述的HTML渲染后调整的fancybox 这里

So for your case you do something like this: 因此,对于您的情况,您可以执行以下操作:

$("a.various").fancybox({
            beforeClose: function () { $("#gallery_spacer").remove(); },
            'onComplete' : function() {
                $('#fancybox-frame').load(function() {
                    $('#fancybox-content').height($(this).contents().find('body').height()).width($(this).contents().find('body').width());
            });
            openEffect: 'fade',openSpeed: 1500,closeEffect: 'fade',closeSpeed: 400,'padding': '0','width': 660,'height': 700,
            maxWidth: 660,maxHeight: 700,fitToView: false,autoSize: false,closeClick: false,'autoScale': 'false','autoDimensions': 'false',
            'transitionIn': 'true','transitionOut': 'true','type': 'iframe','openEffect': 'fade',
            helpers: {overlay: { css: {'background': 'rgba(255, 255, 255, 0.0)'}}}});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM