亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

帶有圖像描述的模態圖像

帶有圖像描述的模態圖像

aluckdog 2023-09-18 15:36:34
帶有圖像描述的模態圖像我在這里找到了有關制作多個模態圖像的代碼。然而,當我打開模式時,我似乎找不到添加描述文本的最佳方法。我一直在關注這篇文章的第二個到最后一個答案的代碼: 頁面上的幾個模態圖像 這對我有很大幫助,但我需要描述文本而不是標題文本。如果有人愿意幫助我解決這個問題,我將不勝感激。先感謝您!代碼 :#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>
查看完整描述

2 回答

?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

您需要在單擊后img + text用so 換行,然后按屬性選擇內部內容并更改 modal 。只需對modalImg.src = this.src 進行小修改即可將modalImg.innerHTML = this.innerHTML; 像下面的片段一樣。 我希望下面的片段對您有很大幫助。divwrapped div.innerHTMLimg to div
js code

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>


查看完整回答
反對 回復 2023-09-18
?
慕妹3242003

TA貢獻1824條經驗 獲得超6個贊

將圖像和文本放在同一個容器中會簡單得多。


<div class="image-container">

<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>

</div>

然后使用 JavaScript 聲明一個名為 modalContent 的變量并選擇您的容器。


var modalContent = getElementById('image-container');

它只是簡化了一切并減少了所需選擇器的數量。查看您的代碼,我無法理解每個圖像是單獨還是全部顯示在模式窗口中。我希望這有幫助。


查看完整回答
反對 回復 2023-09-18
  • 2 回答
  • 0 關注
  • 115 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號