javascriptarraysimage-gallery

How to fix my modal gallery on how to better improve the succeeding images


I'm still learning JS and wanted to make a small modal gallery with the inside having its own gallery for a brochure for a wedding salon. Below is the link to my Codepen:

Codepen

I managed to open the modal gallery but the 2nd gallery gets messed up and wouldn't play nice. I'm no JavaScript expert yet but I managed this far.

I want the 2nd one to not be all over the place. I tried to code it in earlier but my skills are still lacking.

window.addEventListener("click", init, false);

// Get modal element
const modal = document.getElementById("myModal");
const modal1 = document.getElementById("myModal1");

// Get the button that opens the modal
const mdl = document.getElementById("openModal");
const mdl1 = document.getElementById("openModal1");

// Get the <span> element that closes the modal
const span = document.getElementsByClassName("close")[0];

// When the user clicks the image, open the modal
mdl.onclick = function () {
  modal.style.display = "block";
};

mdl1.onclick = function () {
  modal1.style.display = "block";
};

// When the user clicks anywhere outside of the modal, close it
window.onclick = function (event) {
  if (event.target === modal) {
    modal.style.display = "none";

  }
  if (event.target === modal1) {
    modal1.style.display = "none";

  }
};

///////////////

function init(e) {
  if (!e.target.closest(".item")) return;
  let hero = document.querySelector('div[data-pos="0"]');
  let target = e.target.parentElement;
  [target.dataset.pos, hero.dataset.pos] = [
    hero.dataset.pos,
    target.dataset.pos,
  ];
}
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  height: 100vh;
  height: 100svh;
  display: grid;
  place-items: center;
}


.gallery {
  position: relative;
  width: 990px;
  height: 605px;

  &:hover :not(div[data-pos="0"], img) {
    cursor: pointer;
  }

  & .item {
    position: absolute;
    width: 175px;
    height: 150px;
    overflow: hidden;
    transition: transform 0.9s, width 0.9s, height 0.9s;

    & img {
      width: 100%;
      height: 100%;
    }
  }

  & div[data-pos="0"] {
    width: 600px;
    height: inherit;
    z-index: 10;
  }
}

div[data-pos="0"] {
  transform: translate(0, 0);
}
div[data-pos="1"] {
  transform: translate(620px, 0);
}
div[data-pos="2"] {
  transform: translate(815px, 0);
}
div[data-pos="3"] {
  transform: translate(620px, 170px);
}
div[data-pos="4"] {
  transform: translate(815px, 170px);
}

@media (max-width: 800px) {
  .gallery {
    width: 405px;
    height: 660px;

    & .item {
      width: 200px;
      height: 150px;
    }

    & div[data-pos="0"] {
      width: inherit;
      height: 350px;
      // z-index: 1;
    }
  }

  div[data-pos="0"] {
    transform: translate(0, 0);
  }
  div[data-pos="1"] {
    transform: translate(0, 355px);
  }
  div[data-pos="2"] {
    transform: translate(0, 510px);
  }
  div[data-pos="3"] {
    transform: translate(205px, 355px);
  }
  div[data-pos="4"] {
    transform: translate(205px, 510px);
  }
}

.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0, 0, 0); /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4); /* Black w/ opacity */
}

.modal1 {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0, 0, 0); /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4); /* Black w/ opacity */
}

.modal-content {
  background-color: #fefefe;
  margin: 15% auto; /* 15% from the top and centered */
  padding: 20px;
  border: 1px solid #888;
  width: fit-content; /* Could be more or less, depending on screen size */
}

.close {
  color: #aaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: black;
  text-decoration: none;
  cursor: pointer;
}

.gallery img {
  width: 100%; /* Adjust as needed */
  margin-bottom: 20px;
  width: 100px; /* Thumbnail size */
  // margin: 10px;
  transition: transform 0.2s; /* Animation */
}

//////////////
<div class="wrapper">   
<img
      id="openModal"
      src="https://images.unsplash.com/photo-1578054330576-3f6646e38b1b?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
      alt="Open Modal"
      style="width: 100%; max-width: 300px"
    />

    <!-- Modal Structure -->
    <div id="myModal" class="modal">
      <div class="modal-content">
        <span class="close">&times;</span>
        <div class="gallery">
          <!-- Gallery Images Here -->
          <main class="gallery">
            <div class="item" data-pos="0">
              <img
                src="https://plus.unsplash.com/premium_photo-1661456395657-049a92e01522?q=80&w=1976&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
              />
            </div>
            <div class="item" data-pos="1">
              <img
                src="https://images.unsplash.com/photo-1580151134699-e0cfbeb763d3?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
              />
            </div>
            <div class="item" data-pos="2">
              <img
                src="https://images.unsplash.com/photo-1549488497-94b52bddac5d?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
              />
            </div>
            <div class="item" data-pos="3">
              <img
                src="https://images.unsplash.com/photo-1523264114838-feca761983c4?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
              />
            </div>
            <div class="item" data-pos="4">
              <img
                src="https://images.unsplash.com/photo-1542764824-4cdbd6c92dbe?q=80&w=2069&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
              />
            </div>
          </main>

          <!-- Add other images similarly -->
        </div>
      </div>
    </div>

    <!-- Trigger Button 2nd -->
    <img
      id="openModal1"
      src="https://plus.unsplash.com/premium_photo-1661340932540-c285e730728a?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
      alt="Open Modal"
      style="width: 100%; max-width: 300px"
    />

    <!-- Modal Structure -->
    <div id="myModal1" class="modal modal1">
      <div class="modal-content">
        <span class="close">&times;</span>
        <div class="gallery">
          <!-- Gallery Images Here -->
          <main class="gallery">
            <div class="item" data-pos="0">
              <img
                src="https://images.unsplash.com/photo-1525258946800-98cfd641d0de?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
              />
            </div>
            <div class="item" data-pos="1">
              <img
                src="https://images.unsplash.com/photo-1628046276142-a614ec8c5504?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
              />
            </div>
            <div class="item" data-pos="2">
              <img
                src="https://plus.unsplash.com/premium_photo-1664530452440-e0cbce3869c0?q=80&w=1972&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
              />
            </div>
            <div class="item" data-pos="3">
              <img
                src="https://images.unsplash.com/photo-1593575620619-602b4ddf6e96?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
              />
            </div>
            <div class="item" data-pos="4">
              <img
                src="https://images.unsplash.com/photo-1615164631286-c9a3fd24e3ad?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
              />
            </div>
          </main>

        </div>
      </div>
    </div>
</div>


Solution

  • you can do this that way ( with real <dialog> element en JS .showModal() method.

    const
      myModalDialog = document.querySelector('#modal-dial')
      ;
    document.querySelector('div.wrapper').addEventListener('click', ({target})=>
      {
      if (!target.matches('img[data-modal]')) return
        ;
      myModalDialog.className = target.dataset.modal;
      myModalDialog.showModal();
      })
    
    myModalDialog.addEventListener('click', ({target}) =>
      {
      if (target.matches('.close'))
        {
        myModalDialog.close();
        return; 
        }
      if ( !target.matches('img')) return
        ;
      const 
        small = target.closest('.item')
      , pos   = small.dataset.pos
      , bigg  = small.closest('.gallery').querySelector('.item[data-pos="0"]')
        ;
      if (small===bigg) return
        ;
      small.dataset.pos = '0';
      bigg.dataset.pos  = pos;
      })
    * {
      margin      : 0;
      padding     : 0;
      box-sizing  : border-box;
      }
    body {
      height      : 100svh;
      display     : grid;
      place-items : center;
      }
    .wrapper img {
      width  : 300px;
      cursor : pointer;
      }
    dialog {
      position : relative;
      margin   : auto;
      border   : none;
      padding  : 20px;
      outline  : none;
      }
    dialog::backdrop {
      background-color: #7fffd49f;
      }
    .gallery {
      position : relative;
      width    : 990px;
      height   : 605px;
      }
    .gallery .item:not([data-pos="0"]) {
      cursor: pointer;
      }
    .gallery .item {
      position   : absolute;
      width      : 175px;
      height     : 150px;
      overflow   : hidden;
      transition : transform 0.9s, width 0.9s, height 0.9s;
      }
    .gallery .item img {
      width  : 100%;
      height : 100%;
      }
    .gallery .item[data-pos="0"] {
      width     : 600px;
      height    : inherit;
      transform : translate(0, 0);
      z-index   : 10;
      }
    .gallery .item[data-pos="1"] { transform: translate(620px,   0);   }
    .gallery .item[data-pos="2"] { transform: translate(815px,   0);   }
    .gallery .item[data-pos="3"] { transform: translate(620px, 170px); }
    .gallery .item[data-pos="4"] { transform: translate(815px, 170px); }
    
    @media (max-width: 800px) {
      .gallery {
        width  : 405px;
        height : 660px;
        }
      .gallery .item {
        width  : 200px;
        height : 150px;
        }
      .gallery .item[data-pos="0"] {
        width  : inherit;
        height : 350px;
        }
      .gallery .item[data-pos="0"] { transform: translate(  0,     0  ); }
      .gallery .item[data-pos="1"] { transform: translate(  0,   355px); }
      .gallery .item[data-pos="2"] { transform: translate(  0,   510px); }
      .gallery .item[data-pos="3"] { transform: translate(205px, 355px); }
      .gallery .item[data-pos="4"] { transform: translate(205px, 510px); }
      }
    
    dialog.modal_0 > article:not(.modal_0) ,
    dialog.modal_1 > article:not(.modal_1) {
      display: none;
      }
      
    .close {
      color       : #aaa;
      float       : right;
      font-size   : 28px;
      font-weight : bold;
      cursor      : pointer;
      transform   : translate(18px, -24px)
      }
    .close:hover { 
      color: black; 
      }
    <div class="wrapper">
      <img alt="" data-modal="modal_0"
        src="https://images.unsplash.com/photo-1578054330576-3f6646e38b1b?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
      >
      <img  alt="" data-modal="modal_1"
        src="https://plus.unsplash.com/premium_photo-1661340932540-c285e730728a?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
      >
    </div>
    
    <dialog id="modal-dial" class="modal_0">
      <span class="close">&times;</span>
      <article class="gallery modal_0">
        <div class="item" data-pos="0">
          <img
            src="https://plus.unsplash.com/premium_photo-1661456395657-049a92e01522?q=80&w=1976&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
          />
        </div>
        <div class="item" data-pos="1">
          <img
            src="https://images.unsplash.com/photo-1580151134699-e0cfbeb763d3?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
          />
        </div>
        <div class="item" data-pos="2">
          <img
            src="https://images.unsplash.com/photo-1549488497-94b52bddac5d?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
          />
        </div>
        <div class="item" data-pos="3">
          <img
            src="https://images.unsplash.com/photo-1523264114838-feca761983c4?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
          />
        </div>
        <div class="item" data-pos="4">
          <img
            src="https://images.unsplash.com/photo-1542764824-4cdbd6c92dbe?q=80&w=2069&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
          />
        </div>
      </article>
    
      <article class="gallery modal_1">
        <div class="item" data-pos="0">
          <img
            src="https://images.unsplash.com/photo-1525258946800-98cfd641d0de?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
          />
        </div>
        <div class="item" data-pos="1">
          <img
            src="https://images.unsplash.com/photo-1628046276142-a614ec8c5504?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
          />
        </div>
        <div class="item" data-pos="2">
          <img
            src="https://plus.unsplash.com/premium_photo-1664530452440-e0cbce3869c0?q=80&w=1972&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
          />
        </div>
        <div class="item" data-pos="3">
          <img
            src="https://images.unsplash.com/photo-1593575620619-602b4ddf6e96?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
          />
        </div>
        <div class="item" data-pos="4">
          <img
            src="https://images.unsplash.com/photo-1615164631286-c9a3fd24e3ad?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
          />
        </div>
      </article>
    
    </dialog>