简体   繁体   English

调整窗口大小时保持div的长宽比

[英]Keep aspect ratio of div when resizing window

I have an issue which is a little hard to explain. 我有一个很难解释的问题。 I have a div which has a 2:1 aspect ratio. 我有一个高宽比为2:1的div。 And I want it to have a 100% width or height based on the browsers window size. 我希望它具有基于浏览器窗口大小的100%宽度或高度。

If the window height is greater than the divs height the the size should be based on 100% width. 如果窗口高度大于divs高度,则尺寸应基于100%宽度。

And if window height is less than the divs height the size should be based on 100% height. 并且如果窗口高度小于divs高度,则尺寸应基于100%的高度。

With my curren approach it is flashing when I size larger. 用我的方法,当我放大时,它会闪烁。

Look at this link http://acozmo.com/me/ for a demo. 请查看此链接http://acozmo.com/me/以获取演示。

HTML HTML

<section id="container">
</section>

JQuery JQuery的

function update() {

        var $documentHeight = $(document).height();
        var $windowHeight = $(window).height();
        var $documentWidth = $(document).width();
        var $windowWidth = $(window).width();

        var $container = $('#container');

        if ( $windowHeight >= $documentHeight ) {
            $container.width($windowWidth);
            $container.height($windowWidth / 2);
        }
        if ( $windowHeight < $documentHeight ) {
            $container.width($windowHeight * 2);
            $container.height($windowHeight);
        }
    }

    $(document).ready(function() {
        update()
    });

    $(window).resize(function() {
        update();
    })

To resize to fit inside the window while maintaining aspect ratio, set up your container with an initial size and then divide the area width by the container's width to get the scale. 要在保持宽高比的同时调整大小以适合窗口,请设置容器的初始尺寸,然后将区域宽度除以容器的宽度以得到比例。 You then need a check to make sure that scale doesn't make the height too great. 然后,您需要检查以确保比例尺不会使高度过大。 See code below and fiddle - I've added the case where the height is greater than the width too for completeness. 请参见下面的代码和小提琴 -为了完整性,我添加了高度也大于宽度的情况。

To stop the flashing, throttle your resize function using setTimeout. 要停止闪烁,请使用setTimeout限制调整大小功能。 This will ensure that update is only called once every n milliseconds. 这样可以确保每n毫秒仅调用一次更新。 You can play with the number until you find the right balance. 您可以玩数字游戏,直到找到合适的余额为止。

var container = $('#container'),
    updateTimeout,
    threshold = 100;

function update() {
    var windowWidth = $(window).width(),
        windowHeight = $(window).height(),
        width = container.width(),
        height = container.height(),
        scale;

    if ( windowWidth > windowHeight ) {
        scale = windowWidth / width;
        if ( height * scale > windowHeight ) {
            scale = windowHeight / height;
        }
    }
    else {
        scale = windowHeight / height;
        if ( width * scale > windowWidth ) {
            scale = windowWidth / width;
        }
    }
    container.width(Math.round(width * scale));
    container.height(Math.round(height * scale));
}

$(document).ready(function() {
    update();
});

$(window).resize(function() {
    clearTimeout(updateTimeout);
    updateTimeout = setTimeout(update, threshold);
});

It's probably happening because the fact that you are using a second if , instead of an else . 这可能是因为您使用的是第二个if而不是else . . it hits the first if , shrinks the height, and then hits the second if and increases it. 它击中第一个if ,缩小高度,然后击中第二个if并增加它。

Try changing this (the second if ): 尝试更改此设置( if是第二个):

if ( $windowHeight < $documentHeight ) {

. . . to this: 对此:

else {

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

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