javascript - 带有图像描述的模态图像

标签 javascript html css

带有图像描述的模态图像

我在这里找到了有关制作多个模态图像的代码。 然而,当我打开模式时,我似乎找不到添加描述文本的最佳方法。我一直在关注这篇文章的第二个到最后一个答案的代码: Several Modal Images on page 这对我帮助很大,但我需要描述文本而不是标题文本。 如果有人愿意帮助我解决这个问题,我将不胜感激。 预先感谢您!

代码:

#myImg {
  border-radius: 5px;
  cursor: pointer;
  transition: 0.3s;
}

#myImg:hover {
  opacity: 0.7;
}

.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 {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
}

#text {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
  text-align: center;
  color: #ccc;
  padding: 10px 0;
  height: 150px;
}

.modal-content,
#text {
  animation-name: zoom;
  animation-duration: 0.6s;
}

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

.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;
}

@media only screen and (max-width: 700px) {
  .modal-content {
    width: 100%;
  }
}

</style>
</head>
<body>
  <img class="myImg" src="http://onebigphoto.com/uploads/2012/10/midnight-sun-in-lofoten-norway.jpg" alt="Midnight sun in Lofoten, Norway" width="300" height="200">
  <div class="text">The Beautiful mountain over the Norway River was a spectacular view to see..</div>

  <img class="myImg" src="http://cdn-image.travelandleisure.com/sites/default/files/styles/1600x1000/public/1490029386/fisherman-cabin-hamnoy-lofoten-islands-norway-NORWAY0320.jpg?itok=cpPuUjh1" alt="Fishermen's cabins in Lofoten, Norway" width="300" height="200">
  <div class="text">The lovely cabins here in Norway was an amazing stay with beautiful scenery...</div>

  <img class="myImg" src="http://fjordtours.blob.core.windows.net/fjordtours-umbraco/1199/gerirangerfjord-per-ottar-walderhaug-fjordnorway.jpg" alt="Gerirangerfjord, Norway" width="300" height="200">
  <div class="text">An afternoon on top of the Norway mountains you get an even more breath taking view..</div>

  <div id="myModal" class="modal">
    <span class="close">&times;</span>
    <img class="modal-content" id="img01">
    <div class="text"></div>
  </div>
< script >
  var modal = document.getElementById('myModal');
// to all images -- note I'm using a class!
var images = document.getElementsByClassName('myImg');
// the image in the modal
var modalImg = document.getElementById("img01");
// and the caption in the modal
var captionText = document.getElementsByClassName("text");

// Go through all of the images with our custom class
for (var i = 0; i < images.length; i++) {
  var img = images[i];
  // and attach our click listener for this image.
  img.onclick = function(evt) {
    modal.style.display = "block";
    modalImg.src = this.src;
    captionText.innerHTML = this.alt;
  }
}

var span = document.getElementsByClassName("close")[0];

span.onclick = function() {
    modal.style.display = "none";
  } </script>

  </body> 
  </html>

最佳答案

您需要用 div 包裹 img + text,因此点击 wrapped div 后,然后通过 .innerHTML< 选择内部内容 属性并将模态 img 更改为 div
只需对 js 代码 modalImg.src = this.src 进行小修改即可 modalImg.innerHTML = this.innerHTML;
类似下面的片段。

我希望下面的代码片段对您有很大帮助。

var modal = document.getElementById('myModal');
// to all images -- note I'm using a class!
var images = document.getElementsByClassName('myImg');
// the image in the modal
var modalImg = document.getElementById("img01");
// and the caption in the modal
var captionText = document.getElementsByClassName("text");
// Go through all of the images with our custom class
for (var i = 0; i < images.length; i++) {
  var img = images[i];
  // and attach our click listener for this image.
  img.onclick = function(evt) {
    modal.style.display = "block";
    // modalImg.src = this.src;
    modalImg.innerHTML = this.innerHTML;
    captionText.innerHTML = this.alt;
  }
}
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
  modal.style.display = "none";
}
*,*:before, *:after{box-sizing: border-box;}
body{
  margin: 0;
  padding: 0;
  font-family: Arial;
}
.myImg{
  border-radius: 5px;
  cursor: pointer;
  transition: 0.3s;
  display: inline-block;
  vertical-align: top;
  width: 300px;
  min-height: 200px;
}
.myImg:hover {
  opacity: 0.7;
}
.myImg img{
  max-width: 100%;
}
.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: hidden;
  overflow-y: 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 {
  margin: 0 auto 20px auto;
  display: block;
  width: 80%;
  max-width: 700px;
}
.modal-content img{
  width: 100%;
  display: block;
}
.modal-content .text{
  font-size: 16px;
  color: #f1f1f1;
  padding: 10px;
}
.modal-content{
  animation-name: zoom;
  animation-duration: 0.6s;
}

@keyframes zoom {
  from {
    transform: scale(0)
  }
  to {
    transform: scale(1)
  }
}
.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;
}
@media only screen and (max-width: 700px) {
  .modal-content {
    width: 100%;
  }
}
<div class="myImg">
  <img src="http://onebigphoto.com/uploads/2012/10/midnight-sun-in-lofoten-norway.jpg" alt="Midnight sun in Lofoten, Norway">
  <div class="text">The Beautiful mountain over the Norway River was a spectacular view to see..</div>
</div>
<div class="myImg">
  <img src="http://cdn-image.travelandleisure.com/sites/default/files/styles/1600x1000/public/1490029386/fisherman-cabin-hamnoy-lofoten-islands-norway-NORWAY0320.jpg?itok=cpPuUjh1" alt="Fishermen's cabins in Lofoten, Norway">
  <div class="text">The lovely cabins here in Norway was an amazing stay with beautiful scenery...</div>
</div>
<div class="myImg">
  <img src="http://fjordtours.blob.core.windows.net/fjordtours-umbraco/1199/gerirangerfjord-per-ottar-walderhaug-fjordnorway.jpg" alt="Gerirangerfjord, Norway">
  <div class="text">An afternoon on top of the Norway mountains you get an even more breath taking view..</div>
</div>

<!-- Modal Elements -->
<div id="myModal" class="modal">
  <span class="close">&times;</span>
  <div class="modal-content" id="img01"></div>
</div>

关于javascript - 带有图像描述的模态图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60715560/

相关文章:

JavaScript 影响悬停时的 div

javascript - 图像在父行中垂直居中

html - PSD 到 HTML - 如何执行我的背景图形?

css - ASP.net 背景图片不显示

javascript - 将内容类型设置为 application/json 时出现意外 token 错误

javascript - 有什么理由在 JavaScript 中使用 Object.create() 或 new 吗?

javascript - 在 GWT 中使用 jQuery 工具——有没有办法允许交换?

javascript - 使用键值遍历javascript多维数组

javascript - CSS/JS 转换 :translate3d and scrolling - smooth on Android, 在 iPhone 上没有动量

html - 无法使从左到右的边框被覆盖