Fancybox为某些图片设置了高度和宽度属性

时间:2016-06-13 10:21:35

标签: jquery fancybox

我正在使用fancybox,我想为某些图像设置宽度和高度。 有可能这样做吗?

所有建议都很棒,并提前致谢

1 个答案:

答案 0 :(得分:0)

尝试在fancybox中设置“图像”类型的宽度和高度时,这对我有用:

jQuery(".fancybox").fancybox({
arrows : true,
autoSize: false,
fitToView: false,
afterLoad: function(){
    this.width = 960;
    this.height = 540;
    }  
}
});

如果您为所需的任何值更改960和540,或者您也可以在函数中进行javascript计算。如果要为特定图像运行代码,可以检查当前元素的ID或其他属性。例如,如果这是你的html:

<div class="photo-block fancybox" id="image1" data-fancybox-group="group" href="/image.jpg" title="Image description to show">
<div class="photo-block-img"><img src="/image.jpg"/></div>
<div class="photo-block-text">Some text that shows in here</div></div><div class="photo-block fancybox" id="image2" data-fancybox-group="group" href="/image.jpg" title="Image description to show">
<div class="photo-block-img"><img src="/image.jpg"/></div>
<div class="photo-block-text">Some text that shows in here</div>

然后您可以执行以下操作,仅将其应用于afterLoad函数内的ID“image2”:

if($(this.element).attr('id') == 'image2'){
    this.width=960;
    this.height=540;
}

我必须使用“afterLoad”而不是“beforeLoad”,就像我在“iframe”类型的答案中看到的那样。 beforeLoad对图像类型没有任何影响,但AfterLoad工作。

相关问题