如何在过渡之前按顺序制作动画?

huangapple go评论71阅读模式
英文:

How to make an animation in sequence before a transition?

问题

Use fullpage

我的代码 - https://jsfiddle.net/fbyx9kpg/

var delay = 1000;
var timeoutId;
var animationIsFinished = false;

new fullpage('#fullpage', {
  sectionsColor: ['yellow', 'orange', '#C0C0C0', '#ADD8E6'],
  onLeave: function(origin, destination, direction) {

    $('.block').addClass('animate');
    clearTimeout(timeoutId);

    timeoutId = setTimeout(function() {
      animationIsFinished = true;

      fullpage_api.moveTo(destination.index + 1);
    }, delay);

    return animationIsFinished;

  },
});
.section {
    text-align: center;
    font-size: 3em;
    font-family: arial;
}

.block,
#element{
    transition: all 2s ease;
}

.block {
  width: 100px;
  height: 100px;
  background-color: #f00;
}

.block.animate {
  transform: translateX(100px);
}

#element.animate{
    margin-left: 250px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://unpkg.com/fullpage.js@4.0.19/dist/fullpage.min.js"></script>
<link href="https://unpkg.com/fullpage.js@4.0.19/dist/fullpage.min.css" rel="stylesheet"/>
<div id="fullpage">
  <div class="section">
    <div class="block"></div>
    Section 1
  </div>
  <div class="section">
    <div class="block"></div>
    Section 2
  </div>
  <div class="section">
    <div class="block"></div>
    <div id="element">
      Section 3
    </div>
  </div>
  <div class="section">
    <div class="block"></div>
    Section 4
  </div>
</div>

我正在尝试按顺序制作动画,即

section1 - 滚动 - 动画 - 结束动画 - 滚动到section 2

section2 - 滚动 - 动画 - 结束动画 - 滚动到section 3

section3 - 滚动 - 动画 - 结束动画 - 滚动到section 4

section4 - 滚动 - 动画 - 结束动画 - 如果向上滚动到section 3

现在所有部分都同时发生动画。提前感谢您,任何帮助将不胜感激。

英文:

Use fullpage

My code - https://jsfiddle.net/fbyx9kpg/

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var delay = 1000;
var timeoutId;
var animationIsFinished = false;

new fullpage(&#39;#fullpage&#39;, {
  sectionsColor: [&#39;yellow&#39;, &#39;orange&#39;, &#39;#C0C0C0&#39;, &#39;#ADD8E6&#39;],
  onLeave: function(origin, destination, direction) {

    $(&#39;.block&#39;).addClass(&#39;animate&#39;);
    clearTimeout(timeoutId);

    timeoutId = setTimeout(function() {
      animationIsFinished = true;

      fullpage_api.moveTo(destination.index + 1);
    }, delay);

    return animationIsFinished;

  },
});

<!-- language: lang-css -->

.section {
    text-align: center;
    font-size: 3em;
    font-family: arial;
}

.block,
#element{
    transition: all 2s ease;
}

.block {
  width: 100px;
  height: 100px;
  background-color: #f00;
}

.block.animate {
  transform: translateX(100px);
}

#element.animate{
    margin-left: 250px;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://unpkg.com/fullpage.js@4.0.19/dist/fullpage.min.js&quot;&gt;&lt;/script&gt;
&lt;link href=&quot;https://unpkg.com/fullpage.js@4.0.19/dist/fullpage.min.css&quot; rel=&quot;stylesheet&quot;/&gt;
&lt;div id=&quot;fullpage&quot;&gt;
  &lt;div class=&quot;section&quot;&gt;
    &lt;div class=&quot;block&quot;&gt;&lt;/div&gt;
    Section 1
  &lt;/div&gt;
  &lt;div class=&quot;section&quot;&gt;
    &lt;div class=&quot;block&quot;&gt;&lt;/div&gt;
    Section 2
  &lt;/div&gt;
  &lt;div class=&quot;section&quot;&gt;
    &lt;div class=&quot;block&quot;&gt;&lt;/div&gt;
    &lt;div id=&quot;element&quot;&gt;
      Section 3
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;section&quot;&gt;
    &lt;div class=&quot;block&quot;&gt;&lt;/div&gt;
    Section 4
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

I'm trying to make an animation in turn, i.e.

section1 - scroll - animation - end animation - scroll section 2

section2 - scroll - animation - end animation - scroll section 3

section3 - scroll - animation - end animation - scroll section 4

section4 - scroll - animation - end animation - if up to scroll section 3

Now animation happens for all sections at once

Thanks in advance, any help would be appreciated

答案1

得分: 1

过渡只会发生一次,因为它是在将“block”类添加到元素时启动的。

要在每次回调被调用时操纵偏移量,您可以将以下行替换为:

$('.block').css('margin-left', destination.index * 100 + 'px')

在这里,我们使用jQuery的.css()方法来设置margin-left值。该值现在依赖于destination.index的值。

或者,您还可以使用类似以下的方式:

$('.block').css("transform", `translateX(${destination.index * 100}px)`)

.css()方法的文档

英文:

The transition only happens once, because it starts when the "block" class gets added to the element.

To manipulate the offset every time the callback is evoked, you can replace the following line

$(&#39;.block&#39;).addClass(&#39;animate&#39;);

with

$(&#39;.block&#39;).css(&quot;margin-left&quot;, destination.index * 100 + &quot;px&quot;)

Here we use the jQuery .css() method to set the margin-left value. The value now depends on the value of destination.index.

Alternatively, you could also use something like:

$(&#39;.block&#39;).css(&quot;transform&quot;, `translateX(${destination.index * 100}px`)

https://jsfiddle.net/ybwpxz1v/

huangapple
  • 本文由 发表于 2023年5月7日 14:51:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192559.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定