使用ScrollMagic同步Sprite和滚动

时间:2016-12-29 13:45:34

标签: javascript css scroll sprite scrollmagic

溢出社区, 我正在寻找可以通过滚动播放的动画,我找到了

This perfect example

我想通过缺乏关于js和js库的知识来制作类似的东西。 但即使复制和过去所有元素,它仍然无效。

// initialise ScrollMagic controller
var controller = new ScrollMagic.Controller();

// create Tween
var tween = TweenMax.to("#js-animation", 1.0, {
	backgroundPosition: "100% 0",
	ease: SteppedEase.config(480)
})

// build scene
var scene = new ScrollMagic.Scene({duration: 15000})
	.triggerHook("onCenter")
	.setPin("#js-pinned")
	.setTween(tween)
	.addTo(controller);
body {
  padding: 20px;
  text-align: center;
}

.container {
  font-size: 15em;
  min-height: 110vh;
}

.cnc {
  margin: auto;
  width: 50%;
  height: 50%;
  background:  url('http://image.gilawhost.com/16/12/29/9mq5kqgu.png') no-repeat 0 0%;
  background-size: 100%;
}

h1 {
  font-size: 1.2em;
}

p {
  width: 60%;
  margin: auto;
  text-align: left;
}

.p {
  margin-top: 120px;
  font-size: 14px;
  text-align: center;
}
<!DOCTYPE html>
<!--
come from the demo de Tom Bennet
http://www.sitepoint.com/responsive-sprite-animations-imagemagick-greensock
-->
<html >
<head>
  <meta charset="UTF-8">
  <title>Demo 4: Synchronising Playback with the Scrollbar</title>

  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

</head>

<body>
  <h1>Demo 4: Synchronising Playback with the Scrollbar</h1>

<p>A responsive sprite animation that is synchronised with the scrollbar, and that remains in a fixed position for its duration of 1500 pixels. Scroll up and down to control playback.</p>

<div class="container" id="js-pinned">
  <div class="cnc" id="js-animation"></div>
</div>

<p class="p">Demo by Tom Bennet. <a href="http://www.sitepoint.com/responsive-sprite-animations-imagemagick-greensock" target="_blank">See article</a>.</p>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/gsap/1.16.1/TweenMax.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.2/ScrollMagic.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.2/plugins/animation.gsap.min.js'></script>

</body>
</html>

我是否遗漏了某些东西,或者精灵文件太重了?

1 个答案:

答案 0 :(得分:0)

你的精灵PNG很好 - 不太重。您只需要在图像元素上定义一些更明确的大小调整规则。

这里的元素现在是什么样的:

.cnc {
  margin: auto;
  width: 350px;
  height: 200px;
  background:  url('http://image.gilawhost.com/16/12/29/9mq5kqgu.png') no-repeat 0 0%;
  background-size: auto 100%;
}

注意宽度/高度尺寸与精灵动画的一帧成比例,并background-size: auto 100%(这将使图像适合容器的height的100%,但让宽度成比例增长)。

下面的工作示例 - 向外滚动:

&#13;
&#13;
// initialise ScrollMagic controller
var controller = new ScrollMagic.Controller();

// create Tween
var tween = TweenMax.to("#js-animation", 1.0, {
	backgroundPosition: "100% 0",
	ease: SteppedEase.config(479)
})

// build scene
var scene = new ScrollMagic.Scene({duration: 15000})
	.triggerHook("onCenter")
	.setPin("#js-pinned")
	.setTween(tween)
	.addTo(controller);
&#13;
body {
  padding: 20px;
  text-align: center;
}

.container {
  font-size: 15em;
  min-height: 110vh;
}

.cnc {
  margin: auto;
  width: 350px;
  height: 200px;
  background:  url('http://image.gilawhost.com/16/12/29/9mq5kqgu.png') no-repeat 0 0%;
  background-size: auto 100%;
}

h1 {
  font-size: 1.2em;
}

p {
  width: 60%;
  margin: auto;
  text-align: left;
}

.p {
  margin-top: 120px;
  font-size: 14px;
  text-align: center;
}
&#13;
<!DOCTYPE html>
<!--
come from the demo de Tom Bennet
http://www.sitepoint.com/responsive-sprite-animations-imagemagick-greensock
-->
<html >
<head>
  <meta charset="UTF-8">
  <title>Demo 4: Synchronising Playback with the Scrollbar</title>

  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

</head>

<body>
 

<div class="container" id="js-pinned">
  <div class="cnc" id="js-animation"></div>
</div>

<p class="p">Demo by Tom Bennet. <a href="http://www.sitepoint.com/responsive-sprite-animations-imagemagick-greensock" target="_blank">See article</a>.</p>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/gsap/1.16.1/TweenMax.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.2/ScrollMagic.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.2/plugins/animation.gsap.min.js'></script>

</body>
</html>
&#13;
&#13;
&#13;