我正在尝试将scrollmagic实现到我的Angular 2应用程序中,但到目前为止我还没有成功。我看了很多教程,但他们无法帮助我。我正在寻找能够向我明确告诉我如何让Angular 2与scrollmagic合作的人。我试过这个:
我在我的组件中添加了这个:
import { TweenLite, TimelineMax, TweenMax } from "gsap";
import { Component, NgZone, AfterViewInit} from '@angular/core';
declare let ScrollMagic: any;
@Component({
selector: "info-strip",
templateUrl: "./info-strip.component.html",
styleUrls: ["./info-strip.component.scss"],
animations: []
})
export class InfoStripComponent {
ctrl = new ScrollMagic.Controller();
constructor(private zone: NgZone) {}
ngAfterViewInit() {
new ScrollMagic.Scene({
offset: 200,
duration: 50
})
.setPin("#info_strip")
.addTo(this.ctrl);
}}
`
如果我将控制器和场景写入日志,我可以看到它们是有效的ScrollMagic项目,但是我的代码似乎没有做任何事情并且它没有显示任何错误。我想简单地创建一个粘性标题。请任何人帮助使用angular 2来使用scrollmagic
我用Angular CLI + ScrollMagic构建了一个测试系统,发现了一个问题。
Angular CLI:1.5.2 节点:6.10.3 操作系统:win32 x64 Angular:5.1.2 ...动画,常见,编译器,编译器 - cli,核心,表单 ... http,语言服务,平台浏览器 ...平台浏览器动态,路由器
@ angular / cli:1.5.2 @ angular-devkit / build-optimizer:0.0.36 @ angular-devkit / core:0.0.22 @ angular-devkit / schematics:0.0.42 @ ngtools / json-schema:1.1.0 @ ngtools / webpack:1.8.2 @ schematics / angular:0.1.11 @ schematics / schematics:0.0.11 打字稿:2.4.2 webpack:3.8.1
请注意,我的ScrollMagic代码可能不是最佳的。
app.component.ts
import { Component, AfterViewInit } from '@angular/core';
import { TweenMax, TimelineMax } from 'gsap';
import * as ScrollMagic from 'ScrollMagic';
import 'ScrollMagic/scrollmagic/minified/plugins/debug.addIndicators.min.js';
// import 'ScrollMagic/scrollmagic/minified/plugins/animation.gsap.min.js';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements AfterViewInit {
ctrl = new ScrollMagic.Controller();
ngAfterViewInit() {
new ScrollMagic.Scene({
triggerElement: '.menu-trigger',
duration: window.screen.height,
triggerHook: 0
})
.setPin('#menu', { pushFollowers: false })
.addTo(this.ctrl);
}
}
app.component.html
<nav class="nav">
<h1 class="header">Header</h1>
<div class="menu-trigger">
<ul id="menu">
<li><a href="">ScrollMagic</a></li>
<li><a href="">Will</a></li>
<li><a href="">Pin</a></li>
<li><a href="">This</a></li>
<li><a href="">Menu</a></li>
<li><a href="">Until</a></li>
<li><a href="">Second</a></li>
<li><a href="">Slide</a></li>
</ul>
</div>
</nav>
<section class="slide slide01">
<h1>Content</h1>
</section>
<section class="slide slide02">
<h1>More Content</h1>
</section>
app.component.css
body { font-family: Helvetica, Arial, sans-serif; color: rgb(70, 67, 67); }
html, body { margin: 0; height: 100%; }
.nav {
color: rgb(255, 255, 255);
}
.nav .header {
font-size: xx-large;
padding: 20px 10px 20px 10px;
color: rgb(70, 67, 67);
background-color: rgb(212, 215, 248);
}
#menu {
color: rgb(255, 255, 255);
background-color: rgb(0, 0, 0);
padding-left: 0;
list-style: none;
display: flex;
width: 100%;
justify-content: flex-start;
margin-bottom: 0;
margin-top: 0;
border-bottom: 1px solid rgb(203, 189, 189);
}
#menu li {
padding: 10px;
}
#menu li a {
text-decoration: none;
color: rgb(255, 255, 255);
font-weight: bold;
}
h1, p {
margin: 0; padding: 0;
}
.slide {
display: flex;
justify-content: center;
align-items: center;
color: #efefef;
height: 1200px;
}
.slide01 { background-color: rgb(168, 190, 246); }
.slide02 { background-color: rgb(4, 63, 164); }
注意注释掉的行
// import 'ScrollMagic/scrollmagic/minified/plugins/animation.gsap.min.js';
如果我们尝试包含此插件,webpack在将其连接到gsap时会遇到问题。
在ng build
之后,
./node_modules/ScrollMagic/scrollmagic/minified/plugins/animation.gsap.min.js中的错误找不到模块:错误:无法在...中解析'TimelineMax'
这篇文章2017年11月4日的tsconfig paths not respected给出了一个解决方案,但它涉及修补node_modules/@ngtools/webpack/src/paths-plugin.js
。
从帖子,
仅供参考,对于其他需要1.2.6之前行为的人来说,我登陆的解决方案是使用最新的Angular / CLI /等。并用https://github.com/buu700/ngtools-webpack-tmp/blob/master/src/paths-plugin.js覆盖node_modules/@ngtools/webpack/src/paths-plugin.js。它不是很优雅,可能会被未来版本打破,但现在可以完成工作而不会阻止更新到Angular 5。
如果你不喜欢黑客攻击npm安装的@ ngtools / webpack / src / paths-plugin.js,这里有一个替代方案。
部分构建错误消息是
path \ to \ myProject \ src \ TweenMax.js不存在
所以我们可以(粗略地)通过将gasp源文件复制到./src
文件夹来解决这个问题。
参见How to hook 3rd party scripts before serving / building an app with angular-cli,Cory Rylan展示了如何在package.json中使用npm脚本来挂钩构建过程。
package.json
,保留build
脚本,以便它可以正常运行。npm run startbuild
或其他可能更改gasp文件的命令时,运行命令npm install
。的package.json
{
"name": "myProject",
"version": "0.0.0",
"license": "MIT",
"scripts": {
...
"startbuild": "npm run prebuild && ng build",
"prebuild": "copyfiles -f node_modules/gsap/src/uncompressed/*.js src",
"build": "ng build",
...
},