1

I'm building a picture viewer and i've an issue with my code:

$('.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
    });
});

With the script I crops the size of the image in the div #pdfFrame. One problem is for each time I resize my window the image crops for 50%. My question is how to prevent this for cropping over and over again.

It only have to crop by showing the image for the first time.

I hope someone can help me.

Thanks in advance!

7
  • are you wanting the image to crop after the window reaches a certain size because right now its going to resize on any change of the window Feb 28, 2012 at 14:14
  • If it's suppose to resize on load only, not on resize, use jQuery's load() not resize() ?
    – adeneo
    Feb 28, 2012 at 14:15
  • Yes but the pdfFrame need to have a position center of the window, and that depends on the size of the image. Feb 28, 2012 at 14:18
  • @LeonvanderVeen who are you saying yes too???? Feb 28, 2012 at 14:20
  • Then you have to wait for the image to load, not the window!
    – adeneo
    Feb 28, 2012 at 14:21

1 Answer 1

2

As of right now its going to change on any resize of the window. Is what you are probably wanting is that if the window reaches a certain size for the image to crop. So you need to check the window size.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.