[英]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">×</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.