[英]Fit Lightbox container in window if image is larger
I'm just looking for a simple way to set the max width and height of the Lightbox container and image based on the window size if the image is larger than the current window size. 我只是在寻找一种简单的方法来设置Lightbox容器和图像的最大宽度和高度( 如果图像大于当前窗口大小),则基于窗口大小进行设置。
So say the image is 2000x1200 and the window is 1280x1024, then the max-height
and max-width
of div.lb-outerContainer
and img.lb-image
should be set to 假设图片为2000x1200,窗口为1280x1024,那么
div.lb-outerContainer
和img.lb-image
的max-height
和max-width
应设置为
$(window).height() - 286, $(window).width() - 60
and 和
$(window).height() - 306, $(window).width() - 80
respectively. 分别。
I'm just having a bit of trouble determining where to go about implementing these rules. 我在确定执行这些规则的位置时遇到了一些麻烦。 Do I do it in the
lightbox.js
file? 是否在
lightbox.js
文件中执行此操作? If so, where? 如果是这样,在哪里? Would it be acceptable to just throw in some script on the page it's used on?
仅仅在其使用的页面上放入一些脚本是可以接受的吗?
This answer isn't for lightbox only this is just general scaling of .image
vs window
这个答案不是只针对灯箱的,这只是
.image
与window
一般缩放比例
var $window = $(window),
$image = $(".image");
$image.load(function() {
var windowHeight = $window.height(),
windowWidth = $window.width(),
imageHeight = $image.height(),
imageWidth = $image.width(),
radioHeight = windowHeight/imageHeight,
radioWidth;
if ( radioHeight < 1 ) {
imageHeight *= radioHeight;
imageWidth *= radioHeight;
}
radioWidth = windowWidth/imageWidth;
if ( radioWidth < 1 ) {
imageHeight *= radioWidth;
imageWidth *= radioWidth;
}
$image.height( imageHeight );
$image.width( imageWidth );
});
And demo: http://jsbin.com/upisen/1/edit 和演示: http : //jsbin.com/upisen/1/edit
This will not keep image ratio. 这将无法保持图像比例。 You have to use something like this :
您必须使用这样的东西:
$(function(){
$('SELECTOR FOR YOUR IMAGE').load(function() {
this.removeAttribute( "height" );
this.removeAttribute( "width" );
this.style.height = this.style.width = "";
$(this).data('w',this.width);
$(this).data('h',this.height);
ResizeGallery();
});
// on resize window
$(window).resize(function() {
ResizeGallery();
});
});
function ResizeGallery() {
var img=$('SELECTOR FOR YOUR IMAGE');
// remove attribute to get true size
img.removeAttr( "height" );
img.removeAttr( "width" );
var w = img.data('w');
var h = img.data('h');
// compute maximum dimention
var maxW = $(window).width()-300; //300= margin
var maxH = $(window).height()-200;
// compute zoom ratio and keep only the smallest
// also ratio must not exceed 1
var ratio=Math.max(1,Math.min(maxW/w, maxH/h));
// compute new dimentions
var newW = Math.floor(ratio*w);
var newH = Math.floor(ratio*h);
// apply to image
img.css('height',newW ).css('width',newH );
}
assuming you're using jquery 假设您正在使用jQuery
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.