When the page is loaded,how to make six functions executed by each otherr?

空扰寡人 提交于 2019-12-02 06:58:49

You can use .queue() to queue a function to be called for each element in an array or jQuery object. Set .className at element, attach animationend event to element using .one(), at animation end handler called when css animations complete for the element, remove .className, call next function in queue.

Chain .promise(), .then() to .dequeue() to call function when all functions in queue have been called and jQuery returns promise object.

At .then() function set width of #linearN element to "0%", call original function again, repeatedly, at .then() chained to .promise() when queue is empty of functions, to satisfy "loop" requirement of scheduling same function to be called following asynchronous function calls.

The Question describes six #linearN elements

... addClass "line6" to element "#linear6", after 1000ms, removeClass ".line6" from element "#linear6" then back to "#linear1"..."linear6"...loop

though there are three .lineN declarations at css, not six. Only first three #linearN elements are passed to function at stacksnippets. When six .lineN declarations are defined at css, remove .slice(0, 3) chained to $("[id^=linear]", middlecolumn).

$(function() {

  var middlecolumn = $(".middlecolumn");
  var linearLines = $("[id^=linear]", middlecolumn).slice(0, 3);

  function animateLines(column, lines) {
    return column.queue("lines", $.map(lines, function(el, index) {
        return function(next) {
          $(el).addClass("line" + (index + 1))
            .one("animationend", function() {
              $(this).removeClass("line" + (index + 1));
              setTimeout(next, 1000);
            })
        }
      })).dequeue("lines").promise("lines")
      .then(function() {
        console.log("lines queue complete"
        , "\n`animateLines` call scheduled at next line");
        return animateLines(column, lines.css("width", "0%"));
      })
  }

  animateLines(middlecolumn, linearLines);

})
.line1 {
  float: right;
  width: 0%;
  height: 3px;
  background-color: #2e9edd;
  background: -webkit-linear-gradient(0 0, 0 100%, from(#2e9edd), to(#2e9edd));
  -webkit-animation: aaa 1s linear 1;
  -webkit-animation-fill-mode: both;
}

.line2 {
  float: right;
  position: relative;
  top: 30px;
  width: 0%;
  height: 3px;
  background-color: #2e9edd;
  background: -webkit-linear-gradient(0 0, 0 100%, from(#2e9edd), to(#2e9edd));
  -webkit-animation: aaa 1s linear 1;
  -webkit-animation-fill-mode: both;
}

.line3 {
  float: right;
  position: relative;
  top: 50px;
  width: 0%;
  height: 3px;
  background-color: #2e9edd;
  background: -webkit-linear-gradient(0 0, 0 100%, from(#2e9edd), to(#2e9edd));
  -webkit-animation: aaa 1s linear 1;
  -webkit-animation-fill-mode: both;
}

@keyframes aaa {
  0% {
    width: 0%;
  }
  30% {
    width: 30%;
  }
  60% {
    width: 60%;
  }
  100% {
    width: 95%;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<div class="middlecolumn">
  <div class="left1">
    <div id="linear1"></div>
  </div>
  <div class="left2">
    <div id="linear2"></div>
  </div>
  <div class="left3">
    <div id="linear3"></div>
  </div>
  <div class="right1">
    <div id="linear4"></div>
  </div>
  <div class="right2">
    <div id="linear5"></div>
    <!-- <div class="point"></div> -->
  </div>
  <div class="right3">
    <div id="linear6"></div>
  </div>
</div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!