简体   繁体   English

如何循环 jquery 代码或重新启动它?

[英]How can i loop jquery code or restart it?

How can I loop this code so that it runs after the script finishes running.如何循环此代码以便它在脚本完成运行后运行。 Then he repeats it all over again.然后他又重复一遍。

i wanna make repat this code infinity times.我想让这个代码无限次重复。

This is jquery 3.5.1这是 jquery 3.5.1

// Title
    var title1 = document.getElementById("slide-text-title-id");
    var title2 = document.getElementById("slide-text-title-id-2");
    var title3 = document.getElementById("slide-text-title-id-3");

        $(document).ready(function () {
            setTimeout(function () {
                $(title1).css("opacity", "1");
                $(title1).css("transition", "0.5s");
            }, 500);
            setTimeout(function () {
                $(title1).css("opacity", "0");
            }, 6000);
            setTimeout(function () {
                $(title1).css("opacity", "1");
            }, 20000);
        });

        $(document).ready(function () {
            setTimeout(function () {
                $(title2).css("opacity", "1");
                $(title2).css("transition", "0.5s");
            }, 9000);
            setTimeout(function () {
                $(title2).css("opacity", "0");
            }, 13500);
            setTimeout(function () {
                $(title2).css("opacity", "1");
            }, 20000);
        });

        $(document).ready(function () {
            setTimeout(function () {
                $(title3).css("opacity", "1");
                $(title3).css("transition", "0.5s");
            }, 16000);
            setTimeout(function () {
                $(title3).css("opacity", "0");
            }, 21000);
            setTimeout(function () {
                $(title3).css("opacity", "1");
            }, 26500);
        });

The principle:原则:

// the collection:
const elements = ['a', 'b', 'c'];

// the stepper function (also called walker or iterator)
function stepper(index) {
  // the current item is elements[index];
  // do something (anything) with current item:

  console.log({
    index,
    [`currentItem (elements[${index}])`]: elements[index]
  });

  // after a while (3000ms), call this function again, with next index:
  // when current item is last, we call with `0`, so it loops:

  setTimeout(function() {
    stepper(index + 1 < elements.length ? index + 1 : 0);
    /* the above line is shorthand for:
       if (index + 1 < elements.length) {
         stepper(index + 1);
       } else {
         stepper(0);
       }
     */
  } , 3000);
}

// start the loop: 
stepper(0);

Now, let's try it with some elements.现在,让我们尝试一些元素。 Arguably, the easiest way to handle this is to toggle the class active on a collection of slides.可以说,处理此问题的最简单方法是在一组幻灯片上切换 class active Using CSS, we apply styles to the active element.使用 CSS,我们将 styles 应用于有源元件。 In order to be able to control the direction of the animation, I'm also adding a leaving class to the currently active slide, which I'm removing in the next iteration.为了能够控制 animation 的方向,我还在当前活动的幻灯片中添加了一个leaving class,我将在下一次迭代中将其删除。

The rest are details. rest 是详细信息。 As a rule of thumb, details are important in web development.根据经验,细节在 web 开发中很重要。

 const elements = [1, 2, 3]; function stepper(index) { // remove class leaving from all slides $('.slider.leaving').removeClass('leaving'); // add class leaving to the previously active item and remove class active $('.slider.active').addClass('leaving').removeClass('active'); // add class active to current item $('.slider > div').eq(index).addClass('active'); setTimeout(() => stepper(index < elements.length - 1? index + 1: 0), 2100); } // start the infinite loop: stepper(0);
 body { margin: 0; }.slider { position: relative; overflow: hidden; }.slider > div { font-size: 7rem; min-height: 100vh; display: flex; align-items: center; justify-content: center; position: absolute; top: 0; height: 100%; width: 100%; transform: translateX(100%); background-color: #ccc; color: white; }.slider.leaving { transform: translateX(-100%); transition: transform.42s cubic-bezier(.4,0,.2,1); }.slider.active { position: relative; opacity: 1; transform: translateX(0); transition: transform.42s cubic-bezier(.4,0,.2,1) }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="slider"> <div>Slide 1</div> <div>Slide 2</div> <div>Slide 3</div> </div>


Note: I tried to present the principle and then a minimal implementation.注意:我尝试介绍原理,然后介绍最小实现。 In the wild, code gets more complex and more verbose.在野外,代码变得更加复杂和冗长。 Looking at your current attempt, I'd advise you to go through the basics first.查看您当前的尝试,我建议您首先通过基础知识来 go。

Probably the fastest way to learn is by watching tutorials accompanied by playing with the code presented there.最快的学习方法可能是观看教程并玩弄那里提供的代码。 Don't be afraid to change code.不要害怕更改代码。 Change everything, little by little and see where it breaks.一点一点地改变一切,看看哪里坏了。

At every single line you can use console.log({ stuff }) to see what's in stuff at that exact line.在每一行,您都可以使用 console.log({ stuff console.log({ stuff })查看该行的内容。
Break code.破解代码。 The sooner you understand why it broke, the sooner you'll know that bit, too!您越早了解它为什么会损坏,您也会越早知道这一点!

with setIntervalsetInterval

var interval = setInterval(function ()
{
  // Title
  var title1 = document.getElementById("slide-text-title-id");
  var title2 = document.getElementById("slide-text-title-id-2");
  var title3 = document.getElementById("slide-text-title-id-3");

  $(document).ready(function () {
      setTimeout(function () {
          $(title1).css("opacity", "1");
          $(title1).css("transition", "0.5s");
      }, 500);
      setTimeout(function () {
          $(title1).css("opacity", "0");
      }, 6000);
      setTimeout(function () {
          $(title1).css("opacity", "1");
      }, 20000);
  });

  $(document).ready(function () {
      setTimeout(function () {
          $(title2).css("opacity", "1");
          $(title2).css("transition", "0.5s");
      }, 9000);
      setTimeout(function () {
          $(title2).css("opacity", "0");
      }, 13500);
      setTimeout(function () {
          $(title2).css("opacity", "1");
      }, 20000);
  });

  $(document).ready(function () {
      setTimeout(function () {
          $(title3).css("opacity", "1");
          $(title3).css("transition", "0.5s");
      }, 16000);
      setTimeout(function () {
          $(title3).css("opacity", "0");
      }, 21000);
      setTimeout(function () {
          $(title3).css("opacity", "1");
      }, 26500);
  });

}, 0);

or setTimeoutsetTimeout

var timer;
!function loop()
{
  // Title
  var title1 = document.getElementById("slide-text-title-id");
  var title2 = document.getElementById("slide-text-title-id-2");
  var title3 = document.getElementById("slide-text-title-id-3");

  $(document).ready(function () {
      setTimeout(function () {
          $(title1).css("opacity", "1");
          $(title1).css("transition", "0.5s");
      }, 500);
      setTimeout(function () {
          $(title1).css("opacity", "0");
      }, 6000);
      setTimeout(function () {
          $(title1).css("opacity", "1");
      }, 20000);
  });

  $(document).ready(function () {
      setTimeout(function () {
          $(title2).css("opacity", "1");
          $(title2).css("transition", "0.5s");
      }, 9000);
      setTimeout(function () {
          $(title2).css("opacity", "0");
      }, 13500);
      setTimeout(function () {
          $(title2).css("opacity", "1");
      }, 20000);
  });

  $(document).ready(function () {
      setTimeout(function () {
          $(title3).css("opacity", "1");
          $(title3).css("transition", "0.5s");
      }, 16000);
      setTimeout(function () {
          $(title3).css("opacity", "0");
      }, 21000);
      setTimeout(function () {
          $(title3).css("opacity", "1");
      }, 26500);
  });
  timer = setTimeout(loop, 0);
}();

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

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