调整窗口大小调整图像大小

时间:2012-02-28 14:10:43

标签: javascript jquery resize

我正在构建一个图片查看器,我的代码存在问题:

$('.table_content_results table').click(function()
{
    $('#overlay').show();
    $('#pdfFrame').show();  
    $(window).resize(); 
});

$(window).resize(function()
{

    var contentWidth = $('#pdfFrame').children('img').width();
    var contentHeight = $('#pdfFrame').children('img').height();

    var imageWidth = contentWidth/2;
    var imageHeight = contentHeight/2;

    $('#pdfFrame').children('img').width(imageWidth).height(imageHeight);

    $('#pdfFrame').css(
    {
        position:'absolute',
        left: ($(window).width() - imageWidth)/2,
        top: ($(window).height() - imageHeight)/2
    });
});

使用脚本我在div #pdfFrame中裁剪图像的大小。一个问题是每次我调整窗口大小时图像裁剪50%。我的问题是如何防止这种情况一次又一次地裁剪。

只需首次显示图像即可裁剪。

我希望有人可以帮助我。

提前致谢!

1 个答案:

答案 0 :(得分:2)

到目前为止,它将在窗口的任何大小调整上进行更改。您可能想要的是,如果窗口达到一定大小以便裁剪图像。所以你需要检查窗口大小。

$(window).resize(function()
{
   if($(window).width() < 400 || $(window).height() < 400)
   {
      //execute crop
   }
}