簡體   English   中英

如何在窗口調整大小和方向時使腳本調整圖像和div的大小

[英]How to make script resize images and divs on window resize and orientation change

我目前正在使用以下javascript將圖像調整為其父圖像的大小,同時保持寬高比並保持父div正方形。 所以我有一個矩形的方框,根據方向拉伸到最大寬度或最大高度。 這在第一次加載時效果很好,但是我無法獲取圖像和div以在頁面方向更改時調整大小或重新調整大小以正常工作。 有任何想法嗎。 我嘗試使用window.resizewindow.orientation偵聽器。

原始代碼來自: 縮放,裁剪和居中圖像...

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);
};

在$(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(); 

我弄清楚了我所缺少的。 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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM