一个JS函数不起作用,但jsfiddle的其余部分都很好吗?

时间:2017-10-27 02:28:21

标签: javascript html css jsfiddle

我读到一个问题可能是我的功能不是全局的,所以我改变了但只有我的showSlides功能没有被应用/不能正常工作?我该如何解决?对不起,如果有一些明显的答案,我对webdev很新。

注意:我不知道currentSlide和plusSlides是否有效,因为showSlides不是,但我假设问题已经连接。

https://jsfiddle.net/8ynLwfcf/



window.openModal = function openModal() {
  document.getElementById('myModal').style.display = "block";
}
window.closeModal = function closeModal() {
  document.getElementById('myModal').style.display = "none";
}

var slideIndex = 1;
showSlides(slideIndex);

window.plusSlides = function plusSlides(n) {
  showSlides(slideIndex += n);
}

window.currentSlide = function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("demo");
  var captionText = document.getElementById("caption");
  if (n > slides.length) {
    slideIndex = 1
  }
  if (n < 1) {
    slideIndex = slides.length
  }
  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";
  }
  for (i = 0; i < dots.length; i++) {
    dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex - 1].style.display = "block";
  dots[slideIndex - 1].className += " active";
  captionText.innerHTML = dots[slideIndex - 1].alt;
}
&#13;
 body {
   font-family: Verdana, sans-serif;
   margin: 0;
 }
 
 * {
   box-sizing: border-box;
 }
 
 .row > .column {
   padding: 0 8px;
 }
 
 .row:after {
   content: "";
   display: table;
   clear: both;
 }
 
 .column {
   float: left;
   width: 25%;
 }
 /* The Modal (background) */
 
 .modal {
   display: none;
   position: fixed;
   z-index: 1;
   padding-top: 100px;
   left: 0;
   top: 0;
   width: 100%;
   height: 100%;
   overflow: auto;
   background: rgba(0, 0, 0, 0.9);
 }
 /* Modal Content */
 
 .modal-content {
   position: relative;
   background-color: rgba(0, 0, 0, 0.9);
   margin: auto;
   padding: 0;
   width: 90%;
   max-width: 1200px;
 }
 /* The Close Button */
 
 .close {
   color: white;
   position: absolute;
   top: 10px;
   right: 25px;
   font-size: 35px;
   font-weight: bold;
 }
 
 .close:hover,
 .close:focus {
   color: #999;
   text-decoration: none;
   cursor: pointer;
 }
 
 .mySlides {
   display: none;
 }
 
 .cursor {
   cursor: pointer
 }
 /* Next & previous buttons */
 
 .prev,
 .next {
   cursor: pointer;
   position: absolute;
   top: 50%;
   width: auto;
   padding: 16px;
   margin-top: -50px;
   color: white;
   font-weight: bold;
   font-size: 20px;
   transition: 0.6s ease;
   border-radius: 0 3px 3px 0;
   user-select: none;
   -webkit-user-select: none;
 }
 /* Position the "next button" to the right */
 
 .next {
   right: 0;
   border-radius: 3px 0 0 3px;
 }
 /* On hover, add a black background color with a little bit see-through */
 
 .prev:hover,
 .next:hover {
   background-color: rgba(0, 0, 0, 0.8);
   text-decoration: none;
 }
 /* Number text (1/3 etc) */
 
 .numbertext {
   color: #f2f2f2;
   font-size: 12px;
   padding: 8px 12px;
   position: absolute;
   top: 0;
 }
 
 img {
   margin-bottom: -4px;
 }
 
 t img.hover-shadow {
   transition: all .2s ease-in-out;
 }
 
 .hover-shadow:hover {
   transform: scale(1.1);
 }
 
 .modal-content {
   -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)
   }
 }
&#13;
<body>

  <h2 style="text-align:center">Modal Albums</h2>

  <div class="row">
    <div class="column">
      <img src="https://www.yosemitehikes.com/images/wallpaper/yosemitehikes.com-bridalveil-winter-1200x800.jpg" style="width:100%" onclick="openModal();currentSlide(1)" class="hover-shadow cursor">
    </div>
  </div>

  <div id="myModal" class="modal">
    <span class="close cursor" onclick="closeModal()">&times;</span>
    <div class="modal-content">

      <div class="mySlides">
        <div class="numbertext">1 / 4</div>
        <img src="https://www.yosemitehikes.com/images/wallpaper/yosemitehikes.com-bridalveil-winter-1200x800.jpg" style="width:100%">
      </div>

      <div class="mySlides">
        <div class="numbertext">2 / 4</div>
        <img src="http://www.hardwallpapers.com/uploads/3527_Smudge-hdr.jpg" style="width:100%">
      </div>

      <div class="mySlides">
        <div class="numbertext">3 / 4</div>
        <img src="https://assets.rockefellerfoundation.org/app/uploads/20151124121133/15659655525_e6cfc22b56_o-1200x800.jpg" style="width:100%">
      </div>

      <div class="mySlides">
        <div class="numbertext">4 / 4</div>
        <img src="https://support.kickofflabs.com/wp-content/uploads/2016/06/800x1200.png" style="width:100%">
      </div>

      <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
      <a class="next" onclick="plusSlides(1)">&#10095;</a>

    </div>
  </div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

变量点是空的,因为没有任何带有classname'demo'的元素。即使captionText为空,也没有任何带有id'caption'的元素。

答案 1 :(得分:0)

您不需要在寡妇对象上定义函数。 更改所有功能定义,如

window.openModal = function openModal() {
  document.getElementById('myModal').style.display = "block";
}

function openModal() {
  document.getElementById('myModal').style.display = "block";
}

您可以通过更改设置来更改jsFiddle运行脚本的方式。

链接到类似的答案:Simple onload doesn't work in JSFIddle

enter image description here

这是固定的jsFiddle链接https://jsfiddle.net/8ynLwfcf/9/

您需要修复缺少的变量dot,因为没有名为demo的className