Advertisement
B1KMusic

Web Image Viewer script

May 3rd, 2014
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Web image viewer script by Braden Best
  3.  * To use: inject this script into any page, and click on the image you want to view full-size
  4.  */
  5.  
  6. (function(){
  7.   function push(url){
  8.     var img = document.createElement('img'),
  9.         img_helper = document.createElement('div');
  10.     // Image
  11.     img.src = url;
  12.     img.style.position = 'absolute';
  13.     img.style.left = '0px';
  14.     img.style.top  = '40px';
  15.     img.style.zIndex = '10000'; // this is to push it above everything else, so the NG navbar doesn't float over it.
  16.     // Image helper
  17.     img_helper.innerHTML = "Click here to close image";
  18.     img_helper.style.position = 'absolute';
  19.     img_helper.style.left = '0px';
  20.     img_helper.style.top  = '0px';
  21.     img_helper.style.margin = '0';
  22.     img_helper.style.padding = '5px 0';
  23.     img_helper.style.width = '100%';
  24.     img_helper.style.height='30px';
  25.     img_helper.style.background = '#fff';
  26.     img_helper.style.color = '#000';
  27.     img_helper.style.fontSize = '24px';
  28.     img_helper.style.textAlign = 'center';
  29.     img_helper.style.zIndex = '10000';
  30.     // append to body
  31.     document.body.appendChild(img);
  32.     document.body.appendChild(img_helper);
  33.     document.body.scrollTop = 0;
  34.     // helper on-click
  35.     img_helper.onclick = function(){
  36.       document.body.removeChild(img);
  37.       document.body.removeChild(img_helper);
  38.       img_helper.onclick = null;
  39.     }
  40.   }
  41.   var imgs = document.querySelectorAll('img[src]'), i;
  42.   if(imgs[0]){
  43.     for(i = 0; i < imgs.length; i++){
  44.       if(imgs[i].src){
  45.         imgs[i].onclick = function(){
  46.           push(this.src);
  47.           return false;
  48.         }
  49.       }
  50.     }
  51.   } else {
  52.     console.log("Something has gone wrong!");
  53.   }
  54. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement