avr39ripe

jsGalleryClass

Apr 29th, 2021 (edited)
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en" id="html">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>GalleryClass</title>
  6.     <style>
  7.         html,
  8.         body {
  9.             display: flex;
  10.             align-items: center;
  11.             justify-content: center;
  12.         }
  13.  
  14.         .container {
  15.             display: flex;
  16.             flex-direction: row;
  17.             justify-content: center;
  18.             align-content: center;
  19.             align-items: center;
  20.             margin-top: 30px;
  21.         }
  22.  
  23.         .gallery {
  24.             width: 50%;
  25.             padding: 50px;
  26.         }
  27.  
  28.         .inactive {
  29.             opacity: 0.4 !important;
  30.         }
  31.  
  32.         .nav {
  33.             opacity: 1;
  34.             width: 20%;
  35.             font-size: 80px;
  36.             padding: 50px;
  37.         }
  38.     </style>
  39. </head>
  40. <body>
  41.     <div id="container" class="container">
  42.     </div>
  43.     <script>
  44.         'use strict'
  45.  
  46.         class Gallery {
  47.             constructor(container, arrImage) {
  48.                 this._containerId = container;
  49.                 this._container = document.getElementById(container);
  50.                 this._arrImage = arrImage;
  51.  
  52.                 this._render();
  53.                 this._container.addEventListener('click', this);
  54.                 this.idx = 0;
  55.             }
  56.  
  57.             _render() {
  58.                 this._container.insertAdjacentHTML('afterbegin', `<div id="${this._containerId}prev" class="nav" tabindex="0">&#x21E6;</div>`);
  59.                 this._prev = document.getElementById(`${this._containerId}prev`);
  60.                 this._container.insertAdjacentHTML('beforeend', `<div class="gallery"><img id="${this._containerId}img" src="html.png"></div>`);
  61.                 this._img = document.getElementById(`${this._containerId}img`);
  62.                 this._container.insertAdjacentHTML('beforeend', `<div id="${this._containerId}next" class="nav" tabindex="0">&#x21E8;</div>`);
  63.                 this._next = document.getElementById(`${this._containerId}next`);
  64.             }
  65.  
  66.             set idx(val) {
  67.                 this._idx = val;
  68.                 if (this._idx == 0) {
  69.                     this._prev.classList.add('inactive');
  70.                     return;
  71.                 }
  72.                 if (this._idx == this._arrImage.length - 1) {
  73.                     this._next.classList.add('inactive');
  74.                     return;
  75.                 }
  76.                 if (this._idx == this._arrImage.length - 2 || this._idx == 1) {
  77.                     this._prev.classList.remove('inactive');
  78.                     this._next.classList.remove('inactive');
  79.                     return;
  80.                 }
  81.             }
  82.  
  83.             get idx() { return this._idx; }
  84.  
  85.             handleEvent(event) {
  86.                 this[`on${event.type[0].toUpperCase()}${event.type.slice(1)}`](event);
  87.             }
  88.  
  89.             onClick(event) {
  90.                 if ((event.target == this._prev && this.idx > 0) || (event.target == this._next && this.idx < this._arrImage.length - 1)) {
  91.                     if (event.target == this._prev) {
  92.                         --this.idx;
  93.                     }
  94.                     else {
  95.                         ++this.idx;
  96.                     }
  97.                     this._img.src = this._arrImage[this.idx];
  98.                 }
  99.             }
  100.         }
  101.         {
  102.             let arrImage = ["html.png", "css.png", "js.png"];
  103.             let gallery = new Gallery("container", arrImage);
  104.         }
  105.     </script>
  106. </body>
  107. </html>
Add Comment
Please, Sign In to add comment