Posted By

kirie on 06/04/10


Tagged

javascript image gallery fullscreen iphone iPad noflash


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

kirie
DesignByWilliam
wirenaught


Ultimate Full Screen Images Without Flash, iPhone and iPad Friendly


 / Published in: XHTML
 

URL: http://klippoglim.no/blog/2010/06/ultimate-full-screen-images-without-flash-iphone-and-ipad-friendly/

  • Fully compatible with all browsers and iPhone/iPad
  • Letterboxes vertically oriented images
  • Standalone: no library needed, no conflicts needed and speedy
  • VERY light weight (under 3KB for uncompressed css and javascript)
  • Easy to integrate into different gallery types
  • Seamless fallback to pure css solution
  1. <!--
  2. * Scaler: Scaling background images to fill browser window with centering and aspect ratio. iPad and iPhone friendly - v3 - 04/06/2010
  3. * http://klippoglim.no/
  4. * http://kirie.no/
  5. *
  6. * DEMO: http://klippoglim.no/blog/2010/06/ultimate-full-screen-images-without-flash-iphone-and-ipad-friendly/
  7. *
  8. * Copyright (c) 2010 Eirik Backer
  9. * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
  10. * and GPL (http://www.gnu.org/licenses/gpl-2.0.html) licenses.
  11. -->
  12.  
  13.  
  14. <!DOCTYPE html>
  15. <html lang="no">
  16. <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /><title>Scaler by Eirik Backer</title>
  17. <meta name="description" content="" /><meta name="keywords" content="" />
  18. <style type="text/css">
  19. body,html{width:100%;height:100%;overflow:hidden;background:#000 url(load.gif) 50% 50% no-repeat;padding:0;margin:0;border:0}
  20. #bg,#bg div,#bg img{-moz-user-select:-moz-none;-webkit-user-select:none} /*Prevent selection of the background*/
  21. #bg{width:100%;height:100%;overflow:hidden;position:absolute;left:0;bottom:0} /*Container preventing scrolling-bug in opera*/
  22. #bg div{position:absolute;width:200%;height:200%;top:-50%;left:-50%;display:table} /*Create canvas*/
  23. #bg div div{left:0;top:50%;width:100%;display:block;height:auto} /*IE center verticaly*/
  24. #bg div > div{display:table-cell;vertical-align:middle;height:100%;position:static;top:0} /*Others center verticaly*/
  25. #bg img{position:absolute;top:-99999px;left:-99999px;width:50%;display:none} /*Display none AND hide (for later loading with block)*/
  26. #bg img.bgshow{position:static;display:block;margin:0 auto;-ms-interpolation-mode:bicubic} /*Show image, with IE-scaling fix*/
  27. *+html #bg div div{position:absolute;top:50%;height:auto} /*IE7 fix centerer*/
  28. *+html #bg img{position:relative;top:-50%} /*IE7 fix image*/
  29. * html #bg img{height:50%;position:relative;top:-50%} /*IE6 fix image*/
  30. </style>
  31. <!--[if IE]><meta http-equiv="imagetoolbar" content="no" /><meta http-equiv="X-UA-Compatible" content="IE=8" /><![endif]-->
  32. <!--The previous line is for tweeking IE a bit - I do not like the imagetoolbar and IE8s backwards compability mode-->
  33. </head>
  34. <!--Example start-->
  35. <div style="padding:10px;background:#fff;position:absolute;left:10px;bottom:10px;z-index:3;cursor:default">
  36. Just an example showing the floater disappearing, safe to remove when using the code
  37. </div>
  38. <!--Example end-->
  39.  
  40. <div id="bg"> <!--Prevent scrolling on Opera, iPhone and iPad -->
  41. <div> <!--Create canvas for centering -->
  42. <div> <!--Center verticaly -->
  43. <img alt="" src="photos/1.jpg" class="bgshow" />
  44. <img alt="" src="photos/2.jpg" />
  45. <img alt="" src="photos/3.jpg" />
  46. <img alt="" src="photos/4.jpg" />
  47. <img alt="" src="photos/5.jpg" />
  48. </div>
  49. </div>
  50. </div>
  51. <script type="text/javascript">
  52. function Scaler(id){
  53. var imgs=document.getElementById(id).getElementsByTagName('img'),index=0; //Get images and set current index
  54. var resize=function(to){
  55. var to=((to==='+1'?index+1:(to==='-1'?index-1:(isNaN(to)?index:to)))%imgs.length),to=to<0?imgs.length-1:to; //Get current index
  56. if(to!=index)for(var i=0,l=imgs.length;i<l;i++){imgs[i].className=i==to?'bgshow':'';imgs[i].style.display='block'} //Change current and start loading
  57. var i=imgs[to],nw=Math.round(document.body.clientHeight*(i.offsetWidth/i.offsetHeight)); //Get img and calculate new width
  58. i.style.width=(i.offsetWidth>i.offsetHeight&&document.body.clientWidth>nw)?'50%':nw+'px'; //Scale by css if possible
  59. return index=to;
  60. }
  61.  
  62. if(window.attachEvent){window.attachEvent('onload',resize);window.attachEvent('onresize',resize)} //Attact events IE
  63. else{window.addEventListener('load',resize,false);window.addEventListener('resize',resize,false)} //Attact events others
  64. return resize;
  65. };
  66. </script>
  67. <script type="text/javascript">
  68. (function(){
  69. var bg=document.getElementById('bg'),float=document.createElement('span');
  70. var scale=Scaler('bg'), //Create and bind scaler-function
  71. addEvent=function(el,on,fn){el.attachEvent?el.attachEvent('on'+on,fn):el.addEventListener(on,fn,false)}, //Function for cross-borwser addEvent
  72. move=function(e){
  73. float.style.left=(e=e||window.event).clientX+20+'px';
  74. float.style.top=e.clientY+20+'px';
  75. if(float._next!=(e.clientX>(document.body.offsetWidth/2)))float.innerHTML=(float._next=!float._next)?'next':'previous'; //Change html onlyt if needed
  76. },
  77. swap=function(e){
  78. var key=(e=e||window.event).type=='click'?(e.clientX>(document.body.clientWidth/2)?40:37):e.keyCode;
  79. if(key>36&&key<41)scale(key>38?'+1':'-1');
  80. }
  81.  
  82. addEvent(window,'keydown',swap);
  83. addEvent(bg,'mouseout',function(){float.style.display='none'}); //Hide floater when not over image
  84. addEvent(bg,'mouseover',function(){float.style.display='block'});
  85. addEvent(bg,'mousemove',move);
  86. addEvent(bg,'click',swap);
  87.  
  88. float.style.position='absolute';
  89. float.style.zIndex='2';
  90. float.style.color='#fff';
  91. bg.style.cursor='pointer';
  92. bg.appendChild(float);
  93. })();
  94. </script>
  95. </body>
  96. </html>

Report this snippet  

You need to login to post a comment.