我正在尝试使用TweenMax
和ScrollMagic
在页面滚动时缩小标题,但动画在不等待滚动的情况下触发。
这是我的代码:
$(document).ready(function(){
var header = $('header')
//init the controller
var controller = new ScrollMagic.Controller();
var tween = TweenMax.to(header ,1 ,{height: 100});
var scene = new ScrollMagic.Scene({
triggerElement: header
})
.setTween(tween)
.addTo(controller);
});
body {
margin: 0;
padding: 0;
background-color: #e95849;
font-family: sans-serif;
}
ul {
margin: 0;
padding: 0;
list-style-type: none;
}
ul:after {
content: "";
display: table;
clear: both;
}
li {
float: right;
color: #000;
padding: 20px 10px;
}
header {
background-color: #fff;
height: 150px;
position: fixed;
width: 100vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.18.5/TweenMax.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.5/ScrollMagic.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.5/plugins/animation.gsap.min.js"></script>
<header>
<nav>
<ul id="animate">
<li>ABOUT</li>
<li>WORK</li>
<li>DESIGN</li>
<li>CONTACT</li>
</ul>
</nav>
</header>
<div style="height: 2000px"></div>
我还按以下顺序编写脚本 -
答案 0 :(得分:1)
脚本被触发,因为header
元素已经位于触发位置(=视口顶部)。将offset: 1
添加到场景中 - 这将导致它等到向下滚动至少一个像素。 (将该值更改为您喜欢的任何值)
$(document).ready(function(){
var header = $('header')
//init the controller
var controller = new ScrollMagic.Controller();
var tween = TweenMax.to(header ,1 ,{height: 100});
var scene = new ScrollMagic.Scene({
triggerElement: header,
offset: 1
})
.setTween(tween)
.addTo(controller);
});
body {
margin: 0;
padding: 0;
background-color: #e95849;
font-family: sans-serif;
}
ul {
margin: 0;
padding: 0;
list-style-type: none;
}
ul:after {
content: "";
display: table;
clear: both;
}
li {
float: right;
color: #000;
padding: 20px 10px;
}
header {
background-color: #fff;
height: 150px;
position: fixed;
width: 100vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.18.5/TweenMax.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.5/ScrollMagic.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.5/plugins/animation.gsap.min.js"></script>
<header>
<nav>
<ul id="animate">
<li>ABOUT</li>
<li>WORK</li>
<li>DESIGN</li>
<li>CONTACT</li>
</ul>
</nav>
</header>
<div style="height: 2000px"></div>