javascript - 如何循环多个 jQuery 函数

标签 javascript jquery html css

我试图将这些函数循环在一起,以便在最后一个函数完成时重播,但我无法弄清楚如何做。

$(document).ready(function() {
  setTimeout(function() {
    $('#main').animate({
      height: 70,
      width: 300
    }, 200);
  }, 500);
  setTimeout(function() {
    $('#main').animate({
      height: 350,
      width: 350
    }, 200)
    $(".title").css("display", "none")
    $(".1").css("display", "block");
  }, 1000);
  setTimeout(function() {
    $('#main').animate({
      height: 150,
      width: 350
    }, 200)
    $(".title").css("display", "none")
    $(".1").css("display", "none")
    $(".2").css("display", "block");
  }, 1500);
  setTimeout(function() {
    $('#main').animate({
      height: 70,
      width: 300
    }, 200)
    $(".title").css("display", "none")
    $(".1").css("display", "none")
    $(".2").css("display", "none")
    $(".3").css("display", "block");
  }, 2000);
});
#main {
  width: 0px;
  height: 70px;
  background-color: #000000;
  position: absolute;
  left: 70px;
  top: 50px;
}

.styled {
  color: #ffffff;
  font-size: 24px;
  font-family: arial;
  position: absolute;
  left: 20px;
  top: 20px;
}

.social {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="main" class="banner">
  <div class="styled title">name.</div>
  <div class="styled social 1">/1</div>
  <div class="styled social 2">/2</div>
  <div class="styled social 3">/3</div>
</div>

最佳答案

将所有代码都放在一个函数中,并使最后一个函数的回调成为根函数的函数调用。

function myFunction(){
  setTimeout(function() {
    $('#main').animate({
      height: 70,
      width: 300
    }, 200);
  }, 500);
  setTimeout(function() {
    $('#main').animate({
      height: 350,
      width: 350
    }, 200)
    $(".title").css("display", "none")
    $(".1").css("display", "block");
  }, 1000);
  setTimeout(function() {
    $('#main').animate({
      height: 150,
      width: 350
    }, 200)
    $(".title").css("display", "none")
    $(".1").css("display", "none")
    $(".2").css("display", "block");
  }, 1500);
  setTimeout(function() {
    $('#main').animate({
      height: 70,
      width: 300
    }, 200)
    $(".title").css("display", "none")
    $(".1").css("display", "none")
    $(".2").css("display", "none")
    $(".3").css("display", "block");

    myFunction();
  }, 2000);
}

最后,您需要在第一次调用它时使用它。

$(document).ready(function(){
    myFunction();
});

关于javascript - 如何循环多个 jQuery 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48508754/

相关文章:

javascript - 如何检查变量是否包含中文/日文字符?

javascript - 这个 JavaScript 错误是什么意思?

javascript - 如何使用 Javascript 使文本框只读

javascript - 因为在 Javascript 中没有加载到 jquery mobile 中的 dom

javascript - 在后退/前进按钮上显示下一个/上一个图像的文件名

php - 有没有办法在 Moodle 中生成 block 或创建类似嵌套block的样式?

javascript - 修复一个看起来不稳定的倒数计时器

JavaScript 函数未运行

Javascript:将json数据保存为服务器/用户硬盘上的文件

javascript - 在 Javascript 中使用 X、Y 坐标裁剪图像