ScrollMagic之前的TweenMax动画

时间:2016-07-07 20:27:34

标签: jquery css animation greensock gsap

在ScrollMagic

之前运行的TweenMax

我正在尝试使用TweenMaxScrollMagic在页面滚动时缩小标题,但动画在不等待滚动的情况下触发。

这是我的代码:

$(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>

我还按以下顺序编写脚本 -

  1. Jquery的
  2. TweenMax
  3. ScrollMagic

1 个答案:

答案 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>