英文:
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('#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;
},
});
<!-- 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 -->
<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>
<!-- 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)`)
英文:
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
$('.block').addClass('animate');
with
$('.block').css("margin-left", destination.index * 100 + "px")
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:
$('.block').css("transform", `translateX(${destination.index * 100}px`)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论