javascript - 将递增运算符更改为递减运算符后窗口不会关闭

标签 javascript html css

你好,我想弄清楚为什么当我在我的 javascript **for(var i = 0; i < img.length;i--)** 中将递增运算符更改为递减运算符时file 当我单击关闭图标时弹出的窗口不再关闭,并且我收到一条错误消息,这只发生在递减运算符上——递增运算符++ 工作正常。

.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 */

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


/* Add Animation */

.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%;
  }
}
<img class="myImg" src="https://cdn.theatlantic.com/assets/media/img/mt/2017/06/GettyImages_675371680/lead_960.jpg?1498239007" alt="Trolltunga, Norway" width="300" height="200">

<img class="myImg" src="https://cdn.theatlantic.com/assets/media/img/mt/2017/06/GettyImages_675371680/lead_960.jpg?1498239007" alt="Trolltunga, Norway" width="300" height="200">

<!-- The Modal -->
<div id="myModal" class="modal">
  <span class="close">&times;</span>
  <img class="modal-content" id="img01">
  <div id="caption"></div>
</div>
<script>
	// 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.getElementsByClassName('myImg');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
for(var i = 0; i < img.length;i--){
  img[i].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";
}
</script>

最佳答案

如@user184994 所说,您的循环将以这种形式永远运行。 i 永远不会大于数组长度。如果您将 for 循环更改为:

for(var i = img.length - 1; i >= 0; i--){
  //etc
}

它应该像以前一样遍历每个元素,除了现在相反。

关于javascript - 将递增运算符更改为递减运算符后窗口不会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47822068/

相关文章:

javascript - 更改php while循环中值的颜色

html - 如何在 css 文件中快速将 6 位颜色代码转换为 3 位颜色代码?

html - 如何透明去除图像阴影

html - 在 div 中间对齐 <ul>

ASP.NET - 如何检查浏览器是否支持 html5?

html - text_field 的类型属性

css - bootstrap,带有左图像和底部按钮的div

javascript - 在 md-button 上应用图标样式

javascript - 允许元素水平 float 离开屏幕

javascript - 是否可以为网站添加定制功能?