如何在窗口调整大小和方向更改时使脚本调整图像和div的大小

时间:2016-02-13 06:22:06

标签: javascript jquery css resize

我目前正在使用以下javascript将图片大小调整为其父级的大小,同时保持宽高比并保持父级div。所以我有一个方框,矩形根据方向拉伸到最大宽度或最大高度。这在第一次加载时效果很好但是我不能让图像和div在页面方向更改时调整大小或调整大小以便工作。有任何想法吗。我尝试过使用window.resizewindow.orientation听众。

原始代码来自: Scale, crop, and center an image...

var aspHt = $('.aspectcorrect').outerWidth();
$('.aspectcorrect').css('height', aspHt + 'px').css('width', aspHt + 'px');  

function ScaleImage(srcwidth, srcheight, targetwidth, targetheight, fLetterBox) {
    var result = {
        width : 0,
        height : 0,
        fScaleToTargetWidth : true
    };

    if ((srcwidth <= 0) || (srcheight <= 0) || (targetwidth <= 0) || (targetheight <= 0)) {
        return result;
    }

    // scale to the target width
    var scaleX1 = targetwidth;
    var scaleY1 = (srcheight * targetwidth) / srcwidth;

    // scale to the target height
    var scaleX2 = (srcwidth * targetheight) / srcheight;
    var scaleY2 = targetheight;

    // now figure out which one we should use
    var fScaleOnWidth = (scaleX2 > targetwidth);
    if (fScaleOnWidth) {
        fScaleOnWidth = fLetterBox;
    } else {
        fScaleOnWidth = !fLetterBox;
    }

    if (fScaleOnWidth) {
        result.width = Math.floor(scaleX1);
        result.height = Math.floor(scaleY1);
        result.fScaleToTargetWidth = true;
    } else {
        result.width = Math.floor(scaleX2);
        result.height = Math.floor(scaleY2);
        result.fScaleToTargetWidth = false;
    }
    result.targetleft = Math.floor((targetwidth - result.width) / 2);
    result.targettop = Math.floor((targetheight - result.height) / 2);

    return result;
}

function RememberOriginalSize(img) {
    if (!img.originalsize) {
        img.originalsize = {
            width : img.width,
            height : img.height
        };
    }
}

function FixImage(fLetterBox, div, img) {

    RememberOriginalSize(img);

    var targetwidth = $(div).width();
    var targetheight = $(div).height();
    var srcwidth = img.originalsize.width;
    var srcheight = img.originalsize.height;
    var result = ScaleImage(srcwidth, srcheight, targetwidth, targetheight, fLetterBox);

    img.width = result.width;
    img.height = result.height;
    $(img).css("left", result.targetleft);
    $(img).css("top", result.targettop);
}

function FixImages(fLetterBox) {
    $("div.aspectcorrect").each(function(index, div) {
        var img = $(this).find("img").get(0);
        FixImage(fLetterBox, this, img);
    });
}

window.onload = function() {
    FixImages(true);
};

2 个答案:

答案 0 :(得分:0)

在$(window).resize()之后调用.resize():

$(window).resize( function(){

  var height = $(window).height();
  var width = $(window).width(); 

  if(width>height) {
    // Landscape
    $("#landscape").css('display','none');
  } else {
    // Portrait
    $("#landscape").css('display','block');
    $("#landscape").click(function(){
        $(this).hide();
    });
  }
}).resize();

答案 1 :(得分:0)

我弄明白我错过了什么。 javascript的第一点是设置高度和宽度的样式。在调用.outerHeight时,它仍然使用内联样式来计算宽度,因此不会调整div的大小。我只是先使用.removeAttr('style')删除该属性,然后调整大小。现在工作。我只是使用$(window).on("resize", resizeDiv)并将调整大小包装到名为resizeDiv

的函数中
function resizeDiv() {
    var asp = $('.aspectcorrect');
    asp.removeAttr("style");
    var aspHt = asp.outerWidth();
    asp.css('height', aspHt + 'px').css('width', aspHt + 'px'); 
    FixImages(true);
}