css 如何让图像在单击时以其原始大小显示?

vmjh9lq9  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(144)

我想在容器中显示图像,但图像分辨率会受到影响。我的图像高度和宽度可以是随机的,但出于对齐的目的,我对每张图像使用相同的高度和宽度。那么,如果我单击某个特定的图像,它是某种屏幕模态/Imageview,我如何显示原始图像呢?

.custom-class {
    overflow-y: auto;
    height: 350px;
    -webkit-overflow-scrolling: touch;
}
.Custcontent {
    position: relative;
    float: left;
}
<div class="row">
<div class="col-sm-5">
<div class="custom-class">
<span class="Custcontent">
   <img src="https://picsum.photos/200/300" style="width: 2500px; height: 250px; border: thin solid black;">
   <img src="https://picsum.photos/200"style="width: 2500px; height: 250px; border: thin solid black;">
   <img src="https://picsum.photos/id/237/200/300"style="width: 2500px; height: 250px; border: thin solid black;">
   <img src="https://picsum.photos/seed/picsum/200/300"style="width: 2500px; height: 250px; border: thin solid black;">
</span>
</div>
</div>
</div>

图片是动态加载的,所以我不能用id来加载,如果你觉得很难理解我的查询,请告诉我。
https://jsfiddle.net/tusharmalap82/ykLpm2e6/8/

hmmo2u0o

hmmo2u0o1#

我做了一些假设,你需要考虑如果你想显示图像的高度或宽度。如果你想原始大小,你将需要调整容器的大小,以适应

// hmtl and css from https://sabe.io/tutorials/how-to-create-modal-popup-box
const modal = document.querySelector(".modal");
const trigger = document.querySelector(".trigger");
const closeButton = document.querySelector(".close-button");
const modalImgContainer = document.getElementById("imgContainer");
const toggleModal = () => modal.classList.toggle("show-modal");
const windowOnClick = e => e.target === modal && toggleModal();
closeButton.addEventListener("click", toggleModal);
window.addEventListener("click", windowOnClick);

document.querySelector(".custom-class .Custcontent").addEventListener("click", e => {
  const tgt = e.target;
  if (tgt.matches("img")) {
    imgContainer.innerHTML = "";
    const img = new Image();
    img.src = tgt.src;
    img.style.height = "200px"
    imgContainer.append(img);
    toggleModal();
  }  
});
.custom-class {
  overflow-y: auto;
  height: 350px;
  -webkit-overflow-scrolling: touch;
}

.Custcontent {
  position: relative;
  float: left;
}

.modal {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    opacity: 0;
    visibility: hidden;
    transform: scale(1.1);
    transition: visibility 0s linear 0.25s, opacity 0.25s 0s, transform 0.25s;
}

.modal-content {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: white;
    padding: 1rem 1.5rem;
    width: 24rem;
    border-radius: 0.5rem;
    text-align: center;
}

.close-button {
    float: right;
    width: 1.5rem;
    line-height: 1.5rem;
    text-align: center;
    cursor: pointer;
    border-radius: 0.25rem;
    background-color: lightgray;
}

.close-button:hover {
    background-color: darkgray;
}

.show-modal {
    opacity: 1;
    visibility: visible;
    transform: scale(1.0);
    transition: visibility 0s linear 0s, opacity 0.25s 0s, transform 0.25s;
}
<div class="row">
  <div class="custom-class">
    <span class="Custcontent">
   <img src="https://picsum.photos/200/300" style="width: 2500px; height: 250px; border: thin solid black;">
   <img src="https://picsum.photos/200"style="width: 2500px; height: 250px; border: thin solid black;">
   <img src="https://picsum.photos/id/237/200/300"style="width: 2500px; height: 250px; border: thin solid black;">
   <img src="https://picsum.photos/seed/picsum/200/300"style="width: 2500px; height: 250px; border: thin solid black;">
</span>
  </div>
</div>
<div class="modal">
    <div class="modal-content">
        <span class="close-button">×</span>
        <div id="imgContainer"></div>
    </div>
</div>

相关问题