Owyn

JavaScript fit image to screen for Chrome & Firefox by Owyn

Jul 20th, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function fit_to_screen()
  2. {
  3.     img.removeAttribute("style");
  4.     var winX = window.innerWidth + "px";
  5.     var winY = window.innerHeight + "px";
  6.     var vbar = false;
  7.     if (document.body.scrollHeight > document.body.clientHeight) // vertical scrollbar
  8.     {
  9.         img.style.height = winY;
  10.         vbar = true;
  11.     }
  12.     if (document.body.scrollWidth > document.body.clientWidth) // horizontal scrollbar
  13.     {
  14.         if(vbar) // both scrollbars
  15.         {
  16.             if ((document.body.scrollHeight - document.body.clientHeight) > (document.body.scrollWidth - document.body.clientWidth)) // let's see which one is bigger
  17.             {
  18.                 img.removeAttribute("style");
  19.                 img.style.height = winY;
  20.             }
  21.             else
  22.             {
  23.                 img.removeAttribute("style");
  24.                 img.style.width = winX;
  25.             }
  26.         }
  27.         else
  28.         {
  29.             img.removeAttribute("style");
  30.             img.style.width = winX;
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment