简体   繁体   English

Scrollmagic-通过多个补间更改正文和div背景颜色

[英]Scrollmagic - Changing body and div background color with multiple tweens

The site I'm working on uses scrollmagic to fade the color of the active div (each set to size of the screen) from back to white as it scrolls down. 我正在工作的网站在向下滚动时,使用scrollmagic将活动div的颜色(每个div设置为屏幕大小)从后退为白色。 I have it changing the div color as well as the body color for a more seamless transition. 我可以更改div颜色以及主体颜色,以实现更无缝的过渡。 For some reason, the body background color is not changing on the first 2 scenes that fire, but it is on the last 2. I've included a codepen illustrating the issue. 由于某些原因,在触发的前两个场景中,主体背景颜色没有变化,但在最后两个场景中,背景颜色没有变化。我包括一个说明问题的代码笔。 When you scroll down and fire the last 2 scenes, triggered by #blog and #contact, the body color gets set properly. 向下滚动并触发由#blog和#contact触发的最后两个场景时,将正确设置主体颜色。 But not with the first two triggers - #about and #portfolio. 但不是前两个触发器-#about和#portfolio。

Any suggestions? 有什么建议么?

HTML: HTML:

</div>
<div id ="about" class="container frame">


</div>
<div id ="portfolio" class="container frame">


</div>
<div id ="blog" class="container frame">


</div>
<div id ="contact" class="container frame">


</div>

JavaScript: JavaScript:

$(function() {

  var blockTween1 = TweenMax.to('#about', 1.5, {
    backgroundColor: '#000'
});
  var blockTween2 = TweenMax.to('#portfolio', 1.5, {
    backgroundColor: '#fff'
});
  var blockTween3 = new TweenMax.to('#blog', 1.5, {
    backgroundColor: '#000'
});
  var blockTween4 = new TweenMax.to('#contact', 1.5, {
    backgroundColor: '#fff'
});
  var blockTween5 = new TweenMax.to('body', 1.5, {
    backgroundColor: '#000'
});
  var blockTween6 = new TweenMax.to('body', 1.5, {
    backgroundColor: '#fff'
});


var controller = new ScrollMagic.Controller();


var containerScene1 = new ScrollMagic.Scene({
    triggerElement: '#about'
})
.setTween(blockTween1);

var containerScene2 = new ScrollMagic.Scene({
    triggerElement: '#about'
})
.setTween(blockTween5);

  var containerScene3 = new ScrollMagic.Scene({
    triggerElement: '#portfolio'
})
.setTween(blockTween2);

var containerScene4 = new ScrollMagic.Scene({
    triggerElement: '#portfolio'
})
.setTween(blockTween6);

var containerScene5 = new ScrollMagic.Scene({
    triggerElement: '#blog'
})
.setTween(blockTween3);

var containerScene6 = new ScrollMagic.Scene({
    triggerElement: '#blog'
})
.setTween(blockTween5);

  var containerScene7 = new ScrollMagic.Scene({
    triggerElement: '#contact'
})
.setTween(blockTween4);

var containerScene8 = new ScrollMagic.Scene({
    triggerElement: '#contact'
})
.setTween(blockTween6);

controller.addScene([
  containerScene1,
  containerScene2,
  containerScene3,
  containerScene4,
  containerScene5,
  containerScene6,
  containerScene7,
  containerScene8,

]);


});

http://codepen.io/anon/pen/OyMzQm http://codepen.io/anon/pen/OyMzQm

Well I figured it out. 好吧,我想通了。 For whatever reason, I can't reuse the variables I define the Tween properties in. For 4 body transitions, I had to specify 4 unique variables, even if they are applying the same effect. 无论出于何种原因,我都无法重用定义Tween属性的变量。对于4个主体转换,我必须指定4个唯一变量,即使它们施加相同的效果。 So instead of just: 因此,不仅仅是:

  var blockTween5 = new TweenMax.to('body', 1.5, {
    backgroundColor: '#000'
    });
  var blockTween6 = new TweenMax.to('body', 1.5, {
    backgroundColor: '#fff'
    });

It became: 它变成了:

  var blockTween5 = new TweenMax.to('body', 1.5, {
    backgroundColor: '#000'
});
  var blockTween6 = new TweenMax.to('body', 1.5, {
    backgroundColor: '#fff'
});
  var blockTween7 = new TweenMax.to('body', 1.5, {
    backgroundColor: '#000'
});
  var blockTween8 = new TweenMax.to('body', 1.5, {
    backgroundColor: '#fff'
});

With appropriate extra scenes. 有适当的额外场景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM