rojey12
rojey12

Reputation: 29

Inserting clicked picture to modal

// Grab main modal overlay
const overlay = document.querySelector('.overlay');

// Inner main overlay where picture will be appended.
const overlayImage = document.querySelector('.overlay-inner');

// Grid images
const gImages = document.querySelector('.project-image');

function open() {
    for (let i = 0; i < gImages; i++) {
        let imageUrl = gImages[i].src;
        overlayImage.src = imageUrl;
    }
    overlay.classList.add('open'); //Open main modal overlay
}

function close() {
    overlay.classList.remove('open');
}

const buttons = document.querySelectorAll('.project');
buttons.forEach(button => button.addEventListener('click', open));

// Close overlay
overlay.addEventListener('click', close);
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

button {
    cursor: pointer;
}

/* Main Section Gallery Styling */
#portfolio {
    width: 100%;
    min-height: 100vh;
    background: white;
    position: relative;
    display: grid;
    grid-template-columns: repeat(4, minmax(200px, 1fr));
    grid-template-rows: 1fr 1fr;
    gap: 2px;
}

.project {
    position: relative;
    background: #f2dad7;
    overflow: hidden;
}

.project img {
    position: absolute;
    opacity: 0.9;
}

.project:hover .grid-overlay {
    transform: translateY(0%);
}

/* Picture Overlay */
.grid-overlay {
    background: rgba(78, 84, 229, 0.91);
    height: 100%;
    grid-column: -1;
    grid-row: -1;
    position: relative;
    display: grid;
    justify-items: center;
    align-items: center;
    transform: translateY(101%);
    transition: all 0.3s ease-in-out;
}

.grid-overlay button {
    background: none;
    outline: none;
    font-weight: 100;
    letter-spacing: 1px;
    border: 1px solid #fff;
    color: white;
    text-transform: uppercase;
    padding: 10px;
}

.grid-overlay button:hover {
    transition: all 0.3s ease-in-out;
    background: white;
    color: black;
    transform: scale(1.03);
}

/* Modal overylay, Position fixed to cover whole screen*/
.overlay {
    position: fixed;
    background: rgba(71, 69, 69, 0.7);
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    display: none;
    z-index: 3;
}

/* Center elements inside modal when open is activated */
.overlay.open {
    display: grid;
    align-items: center;
    justify-items: center;
}

/* Inner Modal Overlay style where the clicked picture belongs */
.overlay-inner {
    background: white;
    width: 700px;
    height: 300px;
    padding: 20px;
    position: relative;
    opacity: 1;
}

.close {
    position: absolute;
    top: 3px;
    right: 10px;
    background: none;
    font-size: 25px;
}

.project-image {
    margin: -50%;
}
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <section id="portfolio">
        <div class="project">
            <img src="oke.png" alt="a tree" class="project-image">
            <div class="grid-overlay">
                <button class="viewbutton">View More</button>
            </div>
        </div>

        <div class="project">
            <img src="second.png" alt="a tree" class="project-image">
            <div class="grid-overlay">
                <button class="viewbutton">View More</button>
            </div>
        </div>

        <div class="project">
            <img src="third.png" alt="a tree" class="project-image">
            <div class="grid-overlay">
                <button class="viewbutton">View More</button>
            </div>
        </div>

        <div class="project">
            <img src="four.png" alt="a tree" class="project-image">
            <div class="grid-overlay">
                <button class="viewbutton">View More</button>
            </div>
        </div>

        <div class="project">
            <img src="five.png" alt="a tree" class="project-image">
            <div class="grid-overlay">
                <button class="viewbutton">View More</button>
            </div>
        </div>

        <div class="project">
            <img src="six.png" alt="a tree" class="project-image">
            <div class="grid-overlay">
                <button class="viewbutton">View More</button>
            </div>
        </div>


        <div class="project">
            <img src="seven.png" alt="a tree" class="project-image">
            <div class="grid-overlay">
                <button class="viewbutton">View More</button>
            </div>
        </div>


        <div class="project">
            <img src="eight.png" alt="a tree" class="project-image">
            <div class="grid-overlay">
                <button class="viewbutton">View More</button>
            </div>
        </div>

        <div class="overlay">
            <div class="close">Close X</div>
            <img class="overlay-inner" src="">
        </div>


    </section>

    <script src="script.js"></script>
</body>

</html>

// Grab main modal overlay
const overlay = document.querySelector('.overlay');

// Inner main overlay where picture will be appended.
const overlayImage = document.querySelector('.overlay-inner');

// Grid images
const gImages = document.querySelectorAll('.project-image');

function open() {
    for (let i = 0; i < gImages; i++) {
        let imageUrl = gImages[i].src;
        overlayImage.src = imageUrl;
    }
    overlay.classList.add('open'); //Open main modal overlay
}

function close() {
    overlay.classList.remove('open');
}

const buttons = document.querySelectorAll('.project');
buttons.forEach(button => button.addEventListener('click', open));

// Close overlay
overlay.addEventListener('click', close);
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

button {
    cursor: pointer;
}

/* Main Section Gallery Styling */
#portfolio {
    width: 100%;
    min-height: 100vh;
    background: white;
    position: relative;
    display: grid;
    grid-template-columns: repeat(4, minmax(200px, 1fr));
    grid-template-rows: 1fr 1fr;
    gap: 2px;
}

.project {
    position: relative;
    background: #f2dad7;
    overflow: hidden;
}

.project img {
    position: absolute;
    opacity: 0.9;
}

.project:hover .grid-overlay {
    transform: translateY(0%);
}

/* Picture Overlay */
.grid-overlay {
    background: rgba(78, 84, 229, 0.91);
    height: 100%;
    grid-column: -1;
    grid-row: -1;
    position: relative;
    display: grid;
    justify-items: center;
    align-items: center;
    transform: translateY(101%);
    transition: all 0.3s ease-in-out;
}

.grid-overlay button {
    background: none;
    outline: none;
    font-weight: 100;
    letter-spacing: 1px;
    border: 1px solid #fff;
    color: white;
    text-transform: uppercase;
    padding: 10px;
}

.grid-overlay button:hover {
    transition: all 0.3s ease-in-out;
    background: white;
    color: black;
    transform: scale(1.03);
}

/* Modal overylay, Position fixed to cover whole screen*/
.overlay {
    position: fixed;
    background: rgba(71, 69, 69, 0.7);
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    display: none;
    z-index: 3;
}

/* Center elements inside modal when open is activated */
.overlay.open {
    display: grid;
    align-items: center;
    justify-items: center;
}

/* Inner Modal Overlay style where the clicked picture belongs */
.overlay-inner {
    background: white;
    width: 700px;
    height: 300px;
    padding: 20px;
    position: relative;
    opacity: 1;
}

.close {
    position: absolute;
    top: 3px;
    right: 10px;
    background: none;
    font-size: 25px;
}
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <section id="portfolio">
        <div class="project">
            <img src="oke.png" alt="a tree" class="project-image">
            <div class="grid-overlay">
                <button class="viewbutton">View More</button>
            </div>
        </div>

        <div class="project">
            <img src="second.png" alt="a tree" class="project-image">
            <div class="grid-overlay">
                <button class="viewbutton">View More</button>
            </div>
        </div>

        <div class="project">
            <img src="third.png" alt="a tree" class="project-image">
            <div class="grid-overlay">
                <button class="viewbutton">View More</button>
            </div>
        </div>

        <div class="project">
            <img src="four.png" alt="a tree" class="project-image">
            <div class="grid-overlay">
                <button class="viewbutton">View More</button>
            </div>
        </div>

        <div class="project">
            <img src="five.png" alt="a tree" class="project-image">
            <div class="grid-overlay">
                <button class="viewbutton">View More</button>
            </div>
        </div>

        <div class="project">
            <img src="six.png" alt="a tree" class="project-image">
            <div class="grid-overlay">
                <button class="viewbutton">View More</button>
            </div>
        </div>


        <div class="project">
            <img src="seven.png" alt="a tree" class="project-image">
            <div class="grid-overlay">
                <button class="viewbutton">View More</button>
            </div>
        </div>


        <div class="project">
            <img src="eight.png" alt="a tree" class="project-image">
            <div class="grid-overlay">
                <button class="viewbutton">View More</button>
            </div>
        </div>

        <div class="overlay">
            <div class="overlay-inner">
                <div class="close">Close X</div>
            </div>
        </div>



    </section>

    <script src="script.js"></script>
</body>

</html>

I am trying to insert the clicked image into a modal. For example, I click the mango image--that is the one I want to appear on the modal. Or if I click the apple picture I would like that to show up on the modal.

I opened my console but I do not see any errors. I am not sure why the image I click is not rendered on the modal. Is there something I am missing in order to insert the desired clicked picture into the modal? I am not able to insert the url grabbed in the javascript and place it into the src attribute inside the img inside-overlay tag.

Thank you for your time!

Upvotes: 0

Views: 54

Answers (2)

xhxe
xhxe

Reputation: 1487

First of all why it wouldn't work, because you didn't have any <img> tag there, where you had to store your image source, what I did, I put there <img> tag and some css to couldn't overflow it, out of overlay-inner then what I do in Jvascript is, when you click button I grab image source on what I am clickng and store in variable and when the modal opens, I put that variable what I mentioned previously as this modal source, in <img src=""> and you get your picture there

/// Grab main modal overlay
const overlay = document.querySelector('.overlay');

// Inner main overlay where picture will be appended.
const overlayImage = document.querySelector('.overlay-inner');

// Grid images
const gImages = document.querySelector('.overlayImage');


function close() {
    overlay.classList.remove('open');
}

const buttons = document.querySelectorAll('.project');

buttons.forEach(button => {
  button.addEventListener("click", () => {
    const imageSource = button.children[0].src
    gImages.src = imageSource
    overlay.classList.add('open');
  })
});

// Close overlay
overlay.addEventListener('click', close);
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

button {
  cursor: pointer;
}


/* Main Section Gallery Styling */

#portfolio {
  width: 100%;
  min-height: 100vh;
  background: white;
  position: relative;
  display: grid;
  grid-template-columns: repeat(4, minmax(200px, 1fr));
  grid-template-rows: 1fr 1fr;
  gap: 2px;
}

.project {
  position: relative;
  background: #f2dad7;
  overflow: hidden;
}

.project img {
  position: absolute;
  opacity: 0.9;
}

.project:hover .grid-overlay {
  transform: translateY(0%);
}


/* Picture Overlay */

.grid-overlay {
  background: rgba(78, 84, 229, 0.91);
  height: 100%;
  grid-column: -1;
  grid-row: -1;
  position: relative;
  display: grid;
  justify-items: center;
  align-items: center;
  transform: translateY(101%);
  transition: all 0.3s ease-in-out;
}

.grid-overlay button {
  background: none;
  outline: none;
  font-weight: 100;
  letter-spacing: 1px;
  border: 1px solid #fff;
  color: white;
  text-transform: uppercase;
  padding: 10px;
}

.grid-overlay button:hover {
  transition: all 0.3s ease-in-out;
  background: white;
  color: black;
  transform: scale(1.03);
}


/* Modal overylay, Position fixed to cover whole screen*/

.overlay {
  position: fixed;
  background: rgba(71, 69, 69, 0.7);
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  display: none;
  z-index: 3;
}


/* Center elements inside modal when open is activated */

.overlay.open {
  display: grid;
  align-items: center;
  justify-items: center;
}


/* Inner Modal Overlay style where the clicked picture belongs */

.overlay-inner {
  background: white;
  width: 700px;
  height: 300px;
  padding: 20px;
  position: relative;
  opacity: 1;
  overflow: hidden;
}

.close {
  position: absolute;
  top: 3px;
  right: 10px;
  background: none;
  font-size: 25px;
}
.overlay-inner src{
  position: absolute;
  width: 100%;
}
<section id="portfolio">
  <div class="project">
    <img src="https://images.unsplash.com/photo-1481349518771-20055b2a7b24?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8cmFuZG9tJTIwb2JqZWN0c3xlbnwwfHwwfHw%3D&ixlib=rb-1.2.1&w=1000&q=80" alt="a tree" class="project-image">
    <div class="grid-overlay">
      <button class="viewbutton">View More</button>
    </div>
  </div>

  <div class="project">
    <img src="https://cdn.pixabay.com/photo/2017/01/08/13/58/cube-1963036__340.jpg" alt="a tree" class="project-image">
    <div class="grid-overlay">
      <button class="viewbutton">View More</button>
    </div>
  </div>

  <div class="project">
    <img src="third.png" alt="a tree" class="project-image">
    <div class="grid-overlay">
      <button class="viewbutton">View More</button>
    </div>
  </div>

  <div class="project">
    <img src="four.png" alt="a tree" class="project-image">
    <div class="grid-overlay">
      <button class="viewbutton">View More</button>
    </div>
  </div>

  <div class="project">
    <img src="five.png" alt="a tree" class="project-image">
    <div class="grid-overlay">
      <button class="viewbutton">View More</button>
    </div>
  </div>

  <div class="project">
    <img src="six.png" alt="a tree" class="project-image">
    <div class="grid-overlay">
      <button class="viewbutton">View More</button>
    </div>
  </div>


  <div class="project">
    <img src="seven.png" alt="a tree" class="project-image">
    <div class="grid-overlay">
      <button class="viewbutton">View More</button>
    </div>
  </div>


  <div class="project">
    <img src="eight.png" alt="a tree" class="project-image">
    <div class="grid-overlay">
      <button class="viewbutton">View More</button>
    </div>
  </div>

  <div class="overlay">
    <div class="overlay-inner">
       <img src="" class="overlayImage">
      <div class="close">Close X</div>
    </div>
  </div>
</section>

Upvotes: 1

Arsalan Khan
Arsalan Khan

Reputation: 146

I think you do not need to use querySelector here

 let imageUrl = gImages[i].querySelector('img').src;

It should be

 let imageUrl = gImages[i].src;

because imageUrl already refers to the image element.

Upvotes: 0

Related Questions