简体   繁体   English

单击时如何使图像变大? JavaScript

[英]How to make the images bigger when clicked? JavaScrpit

I've made a gallery, but I'm having trouble getting the image to enlarge when clicked.我已经制作了一个图库,但是在单击时无法放大图像。 I want to be able to click on the largest image and then it will enlarge and appear in the middle of the page.我希望能够点击最大的图像,然后它会放大并出现在页面中间。

在此处输入图像描述

Below is the link to the code:下面是代码的链接:

 function galleryFunction1(smallImg) { let fullImg = document.getElementById('imageBox1'); fullImg.src = smallImg.src; }
 .wrapper { margin: 0 auto; width: 100%; max-width: 1400px; }.gallery { display: flex; flex-direction: row; justify-content: space-around; align-items: center; margin: 100px 10px; }.gallery img { cursor: pointer; }.boxOfimages.big-img img { display: block; margin: 0 auto; width: 290px; margin-bottom: 10px; }.boxOfsmallImgs { display: flex; justify-content: space-between; flex-wrap: wrap; }.boxOfsmallImgs img { width: 70px; opacity: 0.7; transition: opacity 0.4s ease; margin: 2px; }.boxOfsmallImgs img:hover { opacity: 1; } }.boxOftext { display: flex; flex-direction: column; align-items: start; font-size: 0.8rem; }.boxOftext h2 { padding: 20px 10px; }.boxOftext p { padding: 10px 10px; }
 <main class="main wrapper"> <section class="gallery"> <div class="boxOfimages"> <div class="big-img"> <img id="imageBox1" src="https://cdn.pixabay.com/photo/2013/04/02/18/58/sculpture-99484_960_720.jpg" alt=""> </div> <div class="boxOfsmallImgs"> <img src="https://cdn.pixabay.com/photo/2013/04/02/18/58/sculpture-99484_960_720.jpg" onclick="galleryFunction1(this)" alt=""> <img src="https://cdn.pixabay.com/photo/2016/09/07/16/19/pile-1651945_960_720.jpg" onclick="galleryFunction1(this)" alt=""> <img src="https://cdn.pixabay.com/photo/2018/05/11/08/11/pet-3389729_960_720.jpg" onclick="galleryFunction1(this)" alt=""> </div> </div> <div class="boxOftext"> <h2>“Dotyk burzy” / “Touch of Storm”</h2> <p>rzeźba / sculpture gips patynowany, granit / patinated plaster, granite 100 x 28 x 28 cm 2020r. </p> <p>dostępna</p> </div> </section>

https://codepen.io/yerbamatepl/pen/mdBGoed https://codepen.io/yerbamatepl/pen/mdBGoed

Thank you in advance for your help.预先感谢您的帮助。

You need to create a popup window with an image element inside:您需要创建一个弹出窗口 window ,其中包含一个图像元素:

<div class="backdrop">
  <div class="popup">
    <img src="" class="popup-image" />
  </div>
</div>

In CSS you need to make the backdrop element as fixed:在 CSS 中,您需要将backdrop元素设置为固定:

position: fixed;
top: 0;
left: 0;
bottom: 0;
right: 0;

So your element will be pulled to each side所以你的元素将被拉到每一边

The popup element will have absolute positioning: popup元素将具有绝对定位:

position: absolute;
width: 500px;
height: auto;
top: 50%; // to put it in the middle
left: 50%;
transform: translate(-50%, -50%);

And finally img element should have max-width as 100% .最后 img 元素的max-width应为100%

Also you need to make open/close functionality, to do that you need to set display: block/none on the backdrop element accordingly on click您还需要创建打开/关闭功能,为此您需要在点击时相应地在backdrop元素上设置display: block/none

You can use a modal for this.您可以为此使用模式。 Have a image tag inside the modal, and hide the entire modal by default.在模态框内有一个图像标签,默认隐藏整个模态框。

<div id="myModal" class="modal">
  <div class="modal-content">
    <span class="close">&times;</span>
    <img src="" id="modal-image" />
  </div>
</div>
.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  padding-top: 50px; /* Location of the box */
  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 {
  margin: auto;
  text-align: center;
}

.modal-image {
  display: inline-block;
}

When the image is clicked, show the modal and set the src of the modal image to which image triggered the event.单击图像时,显示模态并将模态图像的src设置为触发事件的图像。

// Get the gallery box
var imageBox1 = document.getElementById("imageBox1");

// Get the modal image tag
var modal = document.getElementById("myModal");

var modalImage = document.getElementById("modal-image");

// When the user clicks the big picture, set the image and open the modal
imageBox1.onclick = function (e) {
  var src = e.srcElement.src;
  modal.style.display = "block";
  modalImage.src = src;
};

You can also add a "X" that will close the modal as I added in my example below:您还可以添加一个“X”来关闭模式,正如我在下面的示例中添加的那样:

https://codepen.io/swampen/pen/vYezMGx https://codepen.io/swampen/pen/vYezMGx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试让 JavaScript 在单击时使图像变大,仅适用于第一张图像 - Trying to have JavaScript make images bigger when clicked, only works for first image 单击超过 2 个图像时如何更改图像? - how to make images change when clicked for more than 2 images? 单击后使图像开始出现在画布上 - Make images start to appear on canvas when clicked 我有9个小图像,当单击它们时,它们会在图像的较大版本中消失。 为什么不能在Browser(FF)中工作) - I have 9 small images that when clicked on, will fade in the bigger version of the image. Why wont work in Browser(FF)) JavaScrpit 在 for 循环中增加 2 时崩溃 - JavaScrpit crashes when incrementing by 2 in a for loop 使用我拥有的代码单击时,如何使我的图像消失? - How do I make my images disappear when clicked using the code I have? 如何将图像与网格对齐。 (我想创建一个包含图片的产品列表,单击这些图片可以链接到相应的页面) - How to align images to a grid. (I want to make a product list with pictures that when clicked they link to corresponding pages) 如何使视口变大 - How to make viewport bigger 单击单选按钮时如何调整图像大小 - How to resize images when a radio button is clicked 在 React 中单击轮播图像时如何重定向? - How to redirect when clicked on Carousel images in React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM