简体   繁体   English

通过保留比例和最大尺寸使图像宽度适合屏幕

[英]Fitting image width to screen by preserving ratio and max size

I would like to be able show an image with the width fitting to the browser screen and height resizing according to the aspect ratio with the image not getting bigger then his original size. 我希望能够显示一个图像,该图像的宽度适合浏览器屏幕,并且高度根据宽高比调整大小,并且图像不会比其原始尺寸大。 I found this post and the link provided in the first answer is kinda what I want but not exactly. 我找到了这篇文章 ,第一个答案中提供的链接有点像我想要的,但不完全是。 It resizes the image without any restraints on the maximum size. 它将调整图像大小,而对最大大小没有任何限制。 How can I do that? 我怎样才能做到这一点?


The code from the answer: 来自答案的代码:

(function ($) {

$.fn.photoResize = function (options) {

    var element = $(this), 
        defaults = {
            bottomSpacing: 10
        };

    $(element).load(function () {
        updatePhotoHeight();

        $(window).bind('resize', function () {
            updatePhotoHeight();
        });
    });

    options = $.extend(defaults, options);

    function updatePhotoHeight() {
        var o = options, 
            photoHeight = $(window).height();

        $(element).attr('height', photoHeight - o.bottomSpacing);
    }
};

}(jQuery));

You don't need JS. 您不需要JS。 This behaviour, can easily be achieved with simple CSS 通过简单的CSS可以轻松实现此行为

DEMO 演示

CSS : CSS:

img{
    max-width:100%;
    height:auto;
}

EXPLANATION : 说明:

The max-width:100%; max-width:100%; property states that the element shouldn't be wider that 100% of containers width. 属性指出,元素的宽度不应超过容器宽度的100%。 So if the image is wider than the container, it is shrunk to fit inside the container and use the whole availabe width. 因此,如果图像比容器宽,则会缩小以适合容器内部并使用整个可用宽度。 If the container is wider than the image, the image keeps its natural size. 如果容器比图像宽,则图像将保持其自然尺寸。

The height:auto; height:auto; property : 财产:

The browser will calculate and select a height for the specified element. 浏览器将计算并选择指定元素的高度。 ( MDN ) MDN

So the aspect ratio of the image is preserved according to the width of the image. 因此,将根据图像的宽度保留图像的高宽比。

I have fixed the code you mention in the original post. 我已经修复了您在原始帖子中提到的代码。

Github repo with rewrite of original plugin. Github repo带有原始插件的重写。

$('img.test').photoResize({
    upscaleImageWidth:false,
    upscaleImageHeight:false
});

This fixes the problem because it also takes into account the viewport height, which a CSS only version does not. 这解决了问题,因为它也考虑了视口高度,而仅CSS版本没有考虑。 It is also an improved version of what the OP first indicated to have found. 这也是OP最初发现的功能的改进版本。

Full plugin code: 完整的插件代码:

(function ($) {

$.fn.photoResize = function (options) {

var element = $(this),
defaults = {
bottomSpacing: 10,
rightSpacing: 20,
// remember initial picture size (used as maximum size)
             unscaledHeight: $(element).height(),
             unscaledWidth: $(element).width(),
             upscaleImageWidth: true,
             upscaleImageHeight: true
};

options = $.extend(defaults, options);

        $(element).load(function () {
            changeDimensions();
        });
        // the load event is a bit tricky -- it seems to not fire if
        // the element has been loaded very fast... i.e. from the browser's cache.
        // Therefore we force dimension change even before any load event has
        // been received:
        changeDimensions();
        $(window).bind('resize', function () {
            changeDimensions();
        });

        function changeDimensions() {
            // again, we might have to load the picture, yet...
            if ( options.unscaledHeight == 0 ) {
                options.unscaledHeight = $(element).height();
                options.unscaledWidth = $(element).width();
            }
            if ( options.unscaledHeight == 0 ) return;

            var maxDisplayHeight = $(window).height() - $(element).offset().top - options.bottomSpacing;
            var maxDisplayWidth = $(window).width() - $(element).offset().left - options.rightSpacing;
            var desiredHeight = maxDisplayHeight < options.unscaledHeight || options.upscaleImageHeight ? maxDisplayHeight : options.unscaledHeight;
            var desiredWidth = maxDisplayWidth < options.unscaledWidth || options.upscaleImageWidth ? maxDisplayWidth : options.unscaledWidth;
            var currHeight = $(element).height();
            var currWidth = $(element).width();

            if ( currHeight != desiredHeight || currWidth != desiredWidth ) {
                // keep aspect ratio
                if ( desiredHeight / options.unscaledHeight <= desiredWidth / options.unscaledWidth ) {
                    $(element).height(desiredHeight);
                    $(element).width(options.unscaledWidth * desiredHeight / options.unscaledHeight);
                } else {
                    $(element).height(options.unscaledHeight * desiredWidth / options.unscaledWidth);
                    $(element).width(desiredWidth);
                }
            }
        }
};

}(jQuery));

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

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