Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. const options = {
  2. // background color
  3. bgColor: 'rgba(0,0,0,0.9)',
  4.  
  5. // fade-in and fade-out duration
  6. fadeDurationMs: 400,
  7.  
  8. // hide scroll when showing fullscreen image
  9. hideScroll: true,
  10.  
  11. // fade overlay z index
  12. zIndex: 999
  13. };
  14.  
  15. /**
  16. * Show given image on full screen
  17. *
  18. * @param self image url or img tag
  19. */
  20. export function img_box(self) {
  21. const container = initContainer();
  22. const imgUrl = typeof self === 'string' ? self : self.src;
  23. const bodyOverflow = document.body.style.overflow;
  24.  
  25. const image = new Image();
  26. image.src = imgUrl;
  27. image.style.maxWidth = '90%';
  28. image.style.maxHeight = '90%';
  29. image.style.margin = 'auto';
  30. container.appendChild(image);
  31. if (options.hideScroll) {
  32. document.body.style.overflow = 'hidden'
  33. }
  34.  
  35. container.style.display = 'flex';
  36.  
  37. window.setTimeout(() => container.style.opacity = 1, 0);
  38.  
  39. function onClick() {
  40. container.removeEventListener('click', onClick);
  41. container.style.opacity = 0;
  42. window.setTimeout(function() {
  43. container.style.display = 'none';
  44. container.innerHTML = '';
  45. document.body.style.overflow = bodyOverflow;
  46. }, options.fadeDurationMs * 0.8);
  47. }
  48.  
  49. container.addEventListener('click', onClick)
  50. }
  51.  
  52. let imgboxContainer;
  53. function initContainer() {
  54. if (imgboxContainer) return imgboxContainer;
  55.  
  56. const o = document.createElement('div');
  57. o.innerHTML =
  58. '<div id="img_box" style="top:0px;left:0px;opacity:0;width:100%;height:100%;display:none;position:fixed;' +
  59. 'cursor:pointer;z-index:' + options.zIndex +';background-color:' + options.bgColor +
  60. ';transition:opacity ' + options.fadeDurationMs + 'ms"/>';
  61.  
  62. imgboxContainer = o.firstChild;
  63. document.body.appendChild(imgboxContainer);
  64.  
  65. return imgboxContainer;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement