1
\$\begingroup\$

I'm in the process of making a website for a friend of mine who wants to display both the tattoos he has made, as well as his paintings. The idea for the layout is to have a gallery for his tattoos, and another gallery for his paintings. I have so far achieved this, but the code is dry and duplicated - so I'm hoping to make it more efficient with less code. It can be seen in action here, but the code for the galleries is also below:

$(document).ready(function($) {
  $(".gallery-item-one").click(function(e) {
    e.preventDefault();
    var image_href = $(this)
      .children("a")
      .attr("href");
    var image = '<img src="' + image_href + '" />';
    $("#lightbox-one").css("display", "flex");
    $("#content").html(image);
    var thisIndex = $(this).index();
    $("#next").click(function() {
      var images = $("#sectionTwo a");
      if (thisIndex >= images.length - 1) {
        thisIndex = 0;
      } else if (thisIndex < images.length) {
        thisIndex = thisIndex + 1;
      }
      var nn = $(images)
        .eq(thisIndex)
        .attr("href");
      var nnImg = '<img src="' + nn + '" />';
      $("#content").html(nnImg);
    });

    $("#prev").click(function() {
      var images = $("#sectionTwo a");
      if (thisIndex <= 0) {
        thisIndex = images.length - 1;
      } else if (thisIndex > 0) {
        thisIndex = thisIndex - 1;
      }
      var pr = $(images)
        .eq(thisIndex)
        .attr("href");
      var prImg = '<img src="' + pr + '" />';
      $("#content").html(prImg);
    });
  });

  $("#close").on("click", function() {
    $("#lightbox-one").css("display", "none");
  });
});

$(document).ready(function($) {
  $(".gallery-item-two").click(function(e) {
    e.preventDefault();
    var image_href = $(this)
      .children("a")
      .attr("href");
    var image = '<img src="' + image_href + '" />';
    $("#lightbox-one").css("display", "flex");
    $("#content").html(image);
    var thisIndex = $(this).index();
    $("#next").click(function() {
      var images = $("#sectionThree a");
      if (thisIndex >= images.length - 1) {
        thisIndex = 0;
      } else if (thisIndex < images.length) {
        thisIndex = thisIndex + 1;
      }
      var nn = $(images)
        .eq(thisIndex)
        .attr("href");
      var nnImg = '<img src="' + nn + '" />';
      $("#content").html(nnImg);
    });

    $("#prev").click(function() {
      var images = $("#sectionThree a");
      if (thisIndex <= 0) {
        thisIndex = images.length - 1;
      } else if (thisIndex > 0) {
        thisIndex = thisIndex - 1;
      }
      var pr = $(images)
        .eq(thisIndex)
        .attr("href");
      var prImg = '<img src="' + pr + '" />';
      $("#content").html(prImg);
    });
  });

  $("#close").on("click", function() {
    $("#lightbox-one").css("display", "none");
  });
});
.container {
  height: 100vh;
  width: 95%;
  margin: auto;
  position: relative;
}
.images {
  width: 95%;
  display: flex;
  justify-content: space-between;
  position: relative;
}

#tattoos {
  height: 2500px;
}

.sectionOne {
  display: grid;
  height: 100vh;
  grid-template-rows: repeat(1, 1fr);
  grid-template-columns: repeat(1, 1fr);
  grid-gap: 15px;
  width: 60%;
  position: sticky;
  top: 0;
}

.overlay {
  box-shadow: inset 0 0 0 2000px rgba(0, 0, 0, 0.3);
}

#image-one-mobile,
#image-one-mobile-paint {
  display: none;
}

.image-one,
.image-one-paint {
  box-shadow: inset 0 0 0 2000px rgba(0, 0, 0, 0.8);
  margin: 4% 0;
}

#sectionTwo {
  width: 37%;
  display: grid;
  height: 2440px;
  grid-template-rows: repeat(10, 1fr);
  grid-template-columns: repeat(1, 1fr);
  grid-gap: 30px;
  margin: 2.5% 0;
}

.image-one {
  background-image: url("https://i.ibb.co/HXfG735/tattoo.jpg");
  background-size: cover;
  background-position: 0% 80%;
}

#image-two {
  background-image: url("https://i.ibb.co/kBJrryt/7ab34470-9318-4fc1-a6da-656ca31399c5.jpg");
  background-size: cover;
  background-position: 0% 80%;
}

#image-three {
  background-image: url("https://i.ibb.co/SKCnT4d/b19aaf2a-84cf-42b4-9ccd-6205ca1be395.jpg");
  background-size: cover;
  background-position: 0% 50%;
}

#image-four {
  background-image: url("https://i.ibb.co/xF4Qv50/b5d6695b-2142-42cc-8eef-2963311edfd6.jpg");
  background-size: cover;
  background-position: 0% 60%;
}

#image-eight {
  background-image: url("https://i.ibb.co/0nY3VDG/d1ecf205-6b1b-4a1a-b94b-7d75aa177464.jpg");
  background-size: cover;
  background-position: 0% 60%;
}

#image-nine {
  background-image: url("https://i.ibb.co/QYf6vDY/48a63fe2-066b-42d1-9211-040c6977ceff.jpg");
  background-size: cover;
  background-position: 0% 40%;
}

#image-ten {
  background-image: url("https://i.ibb.co/7RdJqgP/ce30fff2-1679-403f-8431-c6be4f8b1466.jpg");
  background-size: cover;
  background-position: 0% 80%;
}

#image-twelve {
  background-image: url("https://i.ibb.co/NTZgKcg/new-image-four.jpg");
  background-size: cover;
  background-position: 0% 50%;
}

#image-thirteen {
  background-image: url("https://i.ibb.co/CbpJGD4/new-three.jpg");
  background-size: cover;
  background-position: 0% 40%;
}

#image-fourteen {
  background-image: url("https://i.ibb.co/QQLR1y7/new-one.jpg");
  background-size: cover;
  background-position: 0% 80%;
}

#image-fifteen {
  background-image: url("https://i.ibb.co/gJ6ZrLq/new-two.jpg");
  background-size: cover;
  background-position: 0% 50%;
}

#painting {
  height: 1320px;
}

#sectionThree {
  width: 37%;
  display: grid;
  height: 1260px;
  grid-template-rows: repeat(5, 1fr);
  grid-template-columns: repeat(1, 1fr);
  grid-gap: 30px;
  margin: 2.5% 0;
}

.image-one-paint {
  background-image: url("https://i.ibb.co/FYT7TTy/one.jpg");
  background-size: cover;
  background-position: 0% 50%;
}

#image-two-paint {
  background-image: url("https://i.ibb.co/Qf3VST6/two.jpg");
  background-size: cover;
  background-position: 0% 50%;
}

#image-three-paint {
  background-image: url("https://i.ibb.co/4Vn1zwF/three.jpg");
  background-size: cover;
  background-position: 0% 50%;
}

#image-four-paint {
  background-image: url("https://i.ibb.co/1ZdCPjQ/four.jpg");
  background-size: cover;
  background-position: 0% 50%;
}

#image-five-paint {
  background-image: url("https://i.ibb.co/XygcPPm/five.jpg");
  background-size: cover;
  background-position: 0% 50%;
}

#image-six-paint {
  background-image: url("https://i.ibb.co/sVgSQZD/six.jpg");
  background-size: cover;
  background-position: 0% 50%;
}

.gallery-item,
.gallery-item-one,
.gallery-item-two {
  display: flex;
  justify-content: center;
  align-items: center;
  opacity: 1;
  text-transform: uppercase;
  font-family: "Lora";
  font-weight: 700;
  color: white;
  font-size: 9vw;
}

.gallery-item-one,
.gallery-item-two:hover {
  cursor: pointer;
}

.container-two {
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  height: 100%;
}

#lightbox-one {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgb(0, 0, 0, 0.9);
  text-align: center;
  z-index: 2;
  display: none;
  justify-content: center;
  align-items: center;
  text-align: center;
}

#lightbox-one img {
  box-shadow: 0 0 25px #111;
  -webkit-box-shadow: 0 0 25px #111;
  -moz-box-shadow: 0 0 25px #111;
  max-width: 80%;
  max-height: 100%;
}

#close {
  position: absolute;
  color: #ffffff;
  top: 5%;
  right: 5%;
}

#close:hover {
  cursor: pointer;
}

#arrowcontainer {
  width: 100%;
  height: 100vh;
  position: absolute;
  top: 50%;
  display: flex;
  justify-content: space-between;
}

#arrowcontainer div {
  color: #ffffff;
  margin: 0 5%;
}

#arrowcontainer div i:hover {
  cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="tattoos" class="container images">
        <section class="sectionOne">
            <div class="image-one gallery-item">
                tatuajes
            </div>
        </section>
        <section id="sectionTwo">
            <div id="image-one-mobile" class="image-one gallery-item">
                tatujes
            </div>   
            <div id="image-two" class="gallery-item-one overlay fade">
                <a href="https://i.ibb.co/kBJrryt/7ab34470-9318-4fc1-a6da-656ca31399c5.jpg"></a>
            </div>
            <div id="image-three" class="gallery-item-one overlay slide">
                <a href="https://i.ibb.co/SKCnT4d/b19aaf2a-84cf-42b4-9ccd-6205ca1be395.jpg"></a>
            </div>
            <div id="image-four" class="gallery-item-one overlay slide">
                <a href="https://i.ibb.co/xF4Qv50/b5d6695b-2142-42cc-8eef-2963311edfd6.jpg"></a>
            </div>
            <div id="image-eight" class="gallery-item-one overlay slide">
                <a href="https://i.ibb.co/0nY3VDG/d1ecf205-6b1b-4a1a-b94b-7d75aa177464.jpg"></a>
            </div>
            <div id="image-nine" class="gallery-item-one overlay slide">
                <a href="https://i.ibb.co/QYf6vDY/48a63fe2-066b-42d1-9211-040c6977ceff.jpg"></a>
            </div>
            <div id="image-ten" class="gallery-item-one overlay slide">
                <a href="https://i.ibb.co/7RdJqgP/ce30fff2-1679-403f-8431-c6be4f8b1466.jpg"></a>
            </div>
            <div id="image-twelve" class="gallery-item-one overlay slide">
                <a href="https://i.ibb.co/NTZgKcg/new-image-four.jpg"></a>
            </div>
            <div id="image-thirteen" class="gallery-item-one overlay slide">
                <a href="https://i.ibb.co/CbpJGD4/new-three.jpg"></a>
            </div>
            <div id="image-fourteen" class="gallery-item-one overlay slide">
                <a href="https://i.ibb.co/QQLR1y7/new-one.jpg"></a>
            </div>
            <div id="image-fifteen" class="gallery-item-one overlay slide">
                <a href="https://i.ibb.co/gJ6ZrLq/new-two.jpg"></a>
            </div>
        </section>
    </div>

    <div id="lightbox-one">
        <div id="content" class="container-two">
            <img src="" />
        </div>
        <div id="close"><i class="fas fa-times"></i></div>
        <div id="arrowcontainer">
            <div id="prev"><i class="fas fa-chevron-left"></i></div>
            <div id="next"><i class="fas fa-chevron-right"></i></div>
        </div>
    </div>

    <div id="painting" class="container images">
        <section class="sectionOne">
            <div class="image-one-paint gallery-item">
                pinturas
            </div>
        </section>
        <section id="sectionThree">
            <div id="image-one-mobile-paint" class="image-one gallery-item">
                    pinturas
            </div>
            <div id="image-two-paint" class="gallery-item-two overlay slide">
                <a href="https://i.ibb.co/Qf3VST6/two.jpg"></a>
            </div>
            <div id="image-three-paint" class="gallery-item-two overlay slide">
                <a href="https://i.ibb.co/4Vn1zwF/three.jpg"></a>
            </div>
            <div id="image-four-paint" class="gallery-item-two overlay slide">
                <a href="https://i.ibb.co/1ZdCPjQ/four.jpg"></a>
            </div>
            <div id="image-five-paint" class="gallery-item-two overlay slide">
                <a href="https://i.ibb.co/XygcPPm/five.jpg"></a>
            </div>
            <div id="image-six-paint" class="gallery-item-two overlay slide">
                <a href="https://i.ibb.co/sVgSQZD/six.jpg"></a>
            </div>
        </section>
    </div>

As I said, it works, I just to know of a way where the JS code that clicks through the images in the lightbox can be reduced.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.