如何概括生成模态图像的代码来处理多个图像

时间:2016-12-17 02:24:52

标签: javascript html css image

我的最终目标是制作一个可点击的小图片网页。单击时,将显示小图像的模态图像。

我从W3Schools网站复制了以下代码:



// 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 = document.getElementById('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";
}
&#13;
 /* 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 {
    -webkit-animation-name: zoom;
    -webkit-animation-duration: 0.6s;
    animation-name: zoom;
    animation-duration: 0.6s;
}

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

@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%;
    }
}
&#13;
 <!-- Trigger the Modal -->
<img id="myImg" src="img_fjords.jpg" alt="Trolltunga, Norway" width="300" height="200">

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

  <!-- The Close Button -->
  <span class="close" onclick="document.getElementById('myModal').style.display='none'">&times;</span>

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

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

我可以看到代码被硬编码为一个图像,我认为实现目标的最简单方法是将图像的id传递给js脚本。所以我开始修改html:

&#13;
&#13;
<!-- Trigger the Modal -->
<img class="images" id="img_1" src="images/Cairns1-041-3-Water-Safari.gif" alt="Water Travellers" width="300" height="200">
<img class="images" id="img_2" src="images/Cairns1-047-2-Handle-With-Care.gif" alt="Manhandle" width="300" height="200">
<!-- img_3 etc -->
&#13;
&#13;
&#13;

我是否在正确的轨道上,如果是这样,我如何将图像ID传递给脚本?我的目标之一是尽可能地理解代码,而不是仅仅复制一个插件。

1 个答案:

答案 0 :(得分:1)

  

这里有一个JSfiddle,它显示了我的答案如何与问题合并。希望它可以帮助其他任何使用w3schools的人。 https://jsfiddle.net/bdneyq4s/1/

是的,您已走上正轨,但如果您设置了活动观察员,则无需使用ID。

您需要添加在模式中显示图像大版本所需的数据。 (如果你为模态+缩略图使用相同的图像,那么不要担心使用额外的属性)

例如,您可以使用以下内容:

<img class="modal-image" src="path/to/thumbnail.jpg" alt="Caption to show" data-bigImage="path/to/big-image.jpg" />

然后,您可以创建一个事件观察者,该事件观察者将在使用modal-image类的所有元素上收听点击事件。在那里,您可以从点击的元素中获取data-bigImagealt属性,并使用它填充模态的内容。

你说你不想复制和粘贴代码所以我没有写出要执行此操作的Javascript,但是如果你需要我,我会编辑我的回答展示一些如何做的例子。

编辑:我已添加以下代码段,以展示如何完成此操作的基本示例。我使用modal-image类为每个图像创建了一个事件观察者,并用新的图像属性替换模态中的图像。 (您可以点击小缩略图以查看它是否有效)

&#13;
&#13;
// Select all images that have the class modal-image
var image = document.getElementsByClassName("modal-image");

// to set an event observer on each element
for( var i=0; i<image.length; i++ ){
  image[i].onclick = function() {
  	// get the two values we need from the image
    // i'm using the alt as the caption
		var 
      bigImage = this.getAttribute('data-bigImage'),
      caption = this.getAttribute('alt');
    
		replaceModalImage( bigImage, caption );
  }
}


/*
 * Replace the modal image's source in order to display the new image
 *
 * @param src Source of the new image
 * @param caption Caption to show with the modal
*/
function replaceModalImage( src, caption ){
	var 
  	img = document.getElementById('big-image'),
    captionContainer = document.getElementById('caption');
  
  img.setAttribute('src', src);
  img.setAttribute('alt', caption);
  captionContainer.innerHTML = caption;
  
}
&#13;
.modal-image{
  /* this is just to make it small like a thumbnail, use a small image in production */
  width: 100px; 
}

img{
  max-width: 100%;
}

#modal{
  background: #999;
  width: 600px;
  padding: 20px;
}
&#13;
<!-- thumbnail container -->
<div class="thumbnails">
  <!-- src = small image, data-bigImage = big image src -->
  <img class="modal-image" src="http://vignette4.wikia.nocookie.net/geometry-dash/images/4/4b/GearSawblade01.png" alt="Caption to show" data-bigImage="http://simpleicon.com/wp-content/uploads/gear-8.png" />
  <img class="modal-image" src="https://ak1.ostkcdn.com/images/products/6626651/6626651/Cottage-Oak-Dining-Table-P14192779.jpg" alt="Caption to show" data-bigImage="http://www.ikea.com/PIAimages/0106117_PE253936_S5.JPG" />
</div>

<!-- modal html -->
<div id="modal">
  <div id="image"><img id="big-image" src="" alt="" /></div>
  <div id="caption"></div>
</div>
&#13;
&#13;
&#13;