diff options
| author | ivarlovlie <ivar.lovlie@gmail.com> | 2020-06-15 19:13:57 +0200 |
|---|---|---|
| committer | ivarlovlie <ivar.lovlie@gmail.com> | 2020-06-15 19:13:57 +0200 |
| commit | 55483eee0f655ea9e5bdf88165ec1f2a107f39dc (patch) | |
| tree | 5f655c697c17e54f0d2e662b82b242c33676b422 /lightbox | |
| download | web-components-55483eee0f655ea9e5bdf88165ec1f2a107f39dc.tar.xz web-components-55483eee0f655ea9e5bdf88165ec1f2a107f39dc.zip | |
Initial commit
Diffstat (limited to 'lightbox')
| -rw-r--r-- | lightbox/lightbox.css | 21 | ||||
| -rw-r--r-- | lightbox/lightbox.js | 34 | ||||
| -rw-r--r-- | lightbox/sample.html | 28 |
3 files changed, 83 insertions, 0 deletions
diff --git a/lightbox/lightbox.css b/lightbox/lightbox.css new file mode 100644 index 0000000..953af83 --- /dev/null +++ b/lightbox/lightbox.css @@ -0,0 +1,21 @@ +img.lightbox[data-light-box-id] { + cursor: pointer; +} + +[data-light-box-wrapper-id] { + background-color: #0b0704; + position: fixed; + top: 0; + left: 0; + height: 100%; + width: 100%; + display: flex; + justify-content: center; + align-items: center; +} + +[data-light-box-wrapper-id] img { + padding: 1rem; + max-height: calc(100% - 50px); + max-width: calc(100% - 50px); +}
\ No newline at end of file diff --git a/lightbox/lightbox.js b/lightbox/lightbox.js new file mode 100644 index 0000000..d6b974f --- /dev/null +++ b/lightbox/lightbox.js @@ -0,0 +1,34 @@ +class Lightbox { + constructor(selector) { + this.initalizeTags(selector); + const css = "img.lightbox[data-light-box-id]{cursor:pointer}[data-light-box-wrapper-id]{background-color:#0b0704;position:fixed;top:0;left:0;height:100%;width:100%;display:flex;justify-content:center;align-items:center}[data-light-box-wrapper-id] img{padding:1rem;max-height:calc(100% - 50px);max-width:calc(100% - 50px)}"; + const styleSheet = document.createElement("style"); + styleSheet.innerHTML = css; + styleSheet.dataset.lightboxStyles = "true"; + document.head.appendChild(styleSheet); + } + + openLightbox(id) { + const source = document.querySelector("[data-light-box-id='" + id + "']"); + const wrapper = document.createElement("div"); + wrapper.dataset.lightBoxWrapperId = id; + wrapper.onclick = () => this.closeLightbox(id); + const img = document.createElement("img"); + img.src = source.src; + wrapper.appendChild(img); + document.body.style = "overflow: hidden !important;"; + document.body.appendChild(wrapper); + } + + closeLightbox(id) { + document.querySelector("[data-light-box-wrapper-id='" + id + "']").remove(); + document.body.style.overflow = ""; + } + + initalizeTags(selector) { + document.querySelectorAll(selector).forEach(image => { + image.dataset.lightBoxId = Math.random().toString(36).substring(2) + Date.now().toString(36); + image.onclick = () => this.openLightbox(image.dataset.lightBoxId); + }); + } +}
\ No newline at end of file diff --git a/lightbox/sample.html b/lightbox/sample.html new file mode 100644 index 0000000..48964e5 --- /dev/null +++ b/lightbox/sample.html @@ -0,0 +1,28 @@ +<!DOCTYPE html> +<html lang="en"> + +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + + <title>Document</title> +</head> + +<body> + <img src="https://source.unsplash.com/random" height="250" alt="Random image from unsplash" class="lightbox"> + <img src="https://source.unsplash.com/random" height="250" alt="Random image from unsplash" class="lightbox"> + <img src="https://source.unsplash.com/random" height="250" alt="Random image from unsplash" class="lightbox"> + <img src="https://source.unsplash.com/random" height="250" alt="Random image from unsplash" class="lightbox"> + <img src="https://source.unsplash.com/random" height="250" alt="Random image from unsplash" class="lightbox"> + <img src="https://source.unsplash.com/random" height="250" alt="Random image from unsplash" class="lightbox"> + <img src="https://source.unsplash.com/random" height="250" alt="Random image from unsplash" class="lightbox"> + <img src="https://source.unsplash.com/random" height="250" alt="Random image from unsplash" class="lightbox"> + <img src="https://source.unsplash.com/random" height="250" alt="Random image from unsplash" class="lightbox"> + <img src="https://source.unsplash.com/random" height="250" alt="Random image from unsplash" class="lightbox"> + <script src="lightbox.js"></script> + <script> + const lightbox = new Lightbox(".lightbox"); + </script> +</body> + +</html>
\ No newline at end of file |
