在点击图像上打开模态(使用单个模态的多个图像)

问题描述 投票:0回答:3

我正在尝试为我的网页创建一个照片页面,单击图像时我想打开一个模式以全屏查看图像(或多或少)。我发现了这个关于使用模态放大图像的教程 - https://www.w3schools.com/howto/howto_css_modal_images.asp

但是,鉴于页面上有多个图像并且只有一个模式,我不确定是否有一种更简单的方法可以在单击时放大图像。

是为每个图像创建单独的ID的唯一方法还是JS中有一个选择器可以一次选择多个图像?

这是 JSfiddle - https://jsfiddle.net/definaly/89roukqd/2/

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

// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = $(".myImg");
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function(){
  modal.style.display = "block";
  modalImg.src = this.src;
  captionText.innerHTML = this.alt;
}

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on <span> (x), close the modal
span.onclick = function() { 
  modal.style.display = "none";
}
/* Style the Image Used to Trigger the Modal */
.myImg {
  border-radius: 5px;
  cursor: pointer;
  transition: 0.3s;
}

.myImg:hover {
  opacity: 0.7;
  }

/* The Modal (background) */
.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  padding-top: 100px; /* 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.9); /* Black w/ opacity */
}

/* Modal Content (Image) */
.modal-content {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
}

/* Caption of Modal Image (Image Text) - Same Width as the Image */
#caption {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
  text-align: center;
  color: #ccc;
  padding: 10px 0;
  height: 150px;
}

/* Add Animation - Zoom in the Modal */
.modal-content, #caption { 
  animation-name: zoom;
  animation-duration: 0.6s;
}

@keyframes zoom {
  from {
    transform: scale(0);
    }
  to {
    transform: scale(1);
    }
}

/* The Close Button */
.close {
  position: absolute;
  top: 15px;
  right: 35px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  transition: 0.3s;
}

.close:hover,
.close:focus {
  color: #bbb;
  text-decoration: none;
  cursor: pointer;
}

/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px){
  .modal-content {
    width: 100%;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!-- Trigger the Modal -->
<img class="myImg" src="https://picsum.photos/300/300?random=1" alt="Snow" style="width:100%;max-width:300px">

<img class="myImg" src="https://picsum.photos/300/300?random=2" alt="Snow" style="width:100%;max-width:300px">

<img class="myImg" src="https://picsum.photos/300/300?random=3" alt="Snow" style="width:100%;max-width:300px">

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- The Close Button -->
  <span class="close">&times;</span>

  <!-- Modal Content (The Image) -->
  <img class="modal-content" id="img01">

  <!-- Modal Caption (Image Text) -->
  <div id="caption"></div>
</div>

提前致谢

javascript jquery html css
3个回答
1
投票

我做了很多改变来利用 jQuery 并抽象出 ID。请随意询问其中任何一个。

演示

// Get the modal
var modal = $("#myModal");
var modalImg = modal.find('.modal-content');

// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = $(".myImg");
var captionBox =$("#caption");

img.click(function() {
    modalImg.attr('src', $(this).attr('src'));
    captionBox.text( $(this).attr('alt') );
    modal.show();
});

// Get the elements that closes the modal
var modalCloser = $(".close");

// When the user clicks on the close element, close the modal
modalCloser.click(function() {
    modal.hide();
});

一些一般性建议:

  • 使用语义变量名称。如果您将某些内容称为“span”并更改标记中的元素类型,那么您现在会出现不匹配。相反,请使用描述其功能的名称。

  • 在打开器或关闭器等内容上设置处理程序时,请使用抓取多个元素的 jQuery 类选择器。这样您就可以从多个地方打开和关闭。

  • 了解 jQuery 的所有常用方法。它们会节省您大量的打字时间,而且我发现它们非常直观,只需很少的努力就能记住它们。

  • JS 使用单引号,HTML 使用双引号。这消除了在将一个嵌套到另一个中时逃脱它们的大部分需要。 jQuery 文档对所有内容都使用 double,但管它呢。

  • 考虑迁移到

    let
    const
    而不是
    var
    。范围界定更加精确,现代 IDE 不会不断建议您这样做。


1
投票

我刚刚修改了你的 HTML 和 JS 代码。

这里是演示链接 - 演示


在 HTML 代码中添加了 Onclick 功能

<img class="myImg" src="https://picsum.photos/300/300?random=1" alt="Snow" onclick="image(event)" style="width:100%;max-width:300px" />

<img class="myImg" src="https://picsum.photos/300/300?random=2" alt="Snow" onclick="image(event)" style="width:100%;max-width:300px" />

<img class="myImg" src="https://picsum.photos/300/300?random=3" alt="Snow" onclick="image(event)" style="width:100%;max-width:300px" />

在 JS 代码中添加了相同的功能。

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

    // Get the image and insert it inside the modal - use its "alt" text as a caption
    // var img = $(".myImg");
    var modalImg = document.getElementById("img01");
    var captionText = document.getElementById("caption");
    function image(event)  {    
        modal.style.display = "block";
        modalImg.src = event.target.src;
        captionText.innerHTML = event.target.alt;
    }

    // Get the <span> element that closes the modal
    var span = document.getElementsByClassName("close")[0];

    // When the user clicks on <span> (x), close the modal
    span.onclick = function () {
        modal.style.display = "none";
    }

0
投票

#极速

很棒的代码!!!! 一件事,如何在模态图像上放置关闭(X)?

希望有人指导

蒂姆

© www.soinside.com 2019 - 2024. All rights reserved.