简体   繁体   English

Javascript 在页面上有第二个 div 时停止工作 class

[英]Javascript stops working when there is a second div with that class on the page

I have some code I am using to autoplay a Summary Block in Squarespace.我有一些代码用于在 Squarespace 中自动播放摘要块。 It is a workaround done by automatically clicking the next button every three seconds.这是通过每三秒自动单击下一个按钮来完成的解决方法。 The code also has a shut off in case a viewer clicks the previous button.如果查看者单击上一个按钮,该代码也会关闭。

This is working great except if there is more than one Summary Block on the page.这很好用,除非页面上有多个摘要块。 As soon as there is more than one, the code doesn't run.只要有多个,代码就不会运行。 I thought I was getting around more than one by having it loop through all of the Summary Blocks but it doesn't seem to be working (maybe I am misunderstanding the loop?).我以为我通过循环遍历所有摘要块来绕过一个以上,但它似乎没有工作(也许我误解了循环?)。

  var summaryCarousel = document.querySelectorAll(".sqs-block-summary-v2");
  for (var i = 0; i < summaryCarousel.length; i++) {
    var carouselNextArrow = summaryCarousel[i].querySelector(".summary-carousel-pager-next");
    var carouselPrevArrow = summaryCarousel[i].querySelector(".summary-carousel-pager-prev");
    carouselPrevArrow.addEventListener("click", carouselStopAutoplay, false);
  }

  function carouselClickNext() {
    carouselNextArrow.click();
  }

  function carouselStopAutoplay() {
    clearInterval(carouselAutoplay);
  }

  let carouselAutoplay = setInterval(carouselClickNext, 3000);

Can anyone see what I am doing wrong?谁能看到我做错了什么?

UPDATE更新

Thank you to charlietfl below for helping me out.感谢下面的 charlietfl 帮助我。 The code is now working except it is making the Squarespace Editor open after every click.代码现在可以正常工作,只是每次单击后都会打开 Squarespace 编辑器。 I came up with this work around to have it only apply the code on the live version of the site.我想出了这个解决方法,让它只在网站的实时版本上应用代码。 However, if anyone has any ideas as to why the code is doing this, it would be greatly appreciated!!但是,如果有人对代码为什么这样做有任何想法,将不胜感激!

if(window.location.href.indexOf("squarespace") < 0 ) {  
    var summaryCarousel = document.querySelectorAll(".sqs-block-summary-v2");
    for (var i = 0; i < summaryCarousel.length; i++) {
      initAutoClick(summaryCarousel[i]);
    }

    function initAutoClick(parent){

      var carouselNextArrow = parent.querySelector(".summary-carousel-pager-next");
      var carouselPrevArrow = parent.querySelector(".summary-carousel-pager-prev"); 

      carouselPrevArrow.addEventListener("click", carouselStopAutoplay, false);


      function carouselClickNext() {
        carouselNextArrow.click();
      }

      function carouselStopAutoplay() {
        clearInterval(carouselAutoplay);
      }

      let carouselAutoplay = setInterval(carouselClickNext, 3000);

    }
  }

The basic problem is after the loop completes the value of carouselNextArrow will be the one in the last summary block only.基本问题是循环完成后carouselNextArrow的值将仅是最后一个汇总块中的值。 Also the setInterval would not be instance specific either setInterval 也不是特定于实例的

You could wrap this in a function to call for each summary block instance.您可以将其包装在 function 中以调用每个汇总块实例。

var summaryCarousel = document.querySelectorAll(".sqs-block-summary-v2");
for (var i = 0; i < summaryCarousel.length; i++) {
  // call function for each instance
  initAutoClick(summaryCarousel[i])
}

function initAutoClick(parent){

  var carouselNextArrow = parent.querySelector(".summary-carousel-pager-next");
  var carouselPrevArrow = parent.querySelector(".summary-carousel-pager-prev"); 

  carouselPrevArrow.addEventListener("click", carouselStopAutoplay, false);


  function carouselClickNext() {
    carouselNextArrow.click();
  }

  function carouselStopAutoplay() {
    clearInterval(carouselAutoplay);
  }

  let carouselAutoplay = setInterval(carouselClickNext, 3000);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM