2017-09-21 51 views
0

我有一些问题。我想创建按钮,将整个页面滚动到顶部。 这是我的代码。Angular2和TweenMax - 无法在'窗口'上执行'scrollTo':参数1('选项')不是对象

app.component.html

<p>lorem...</p> <!-- 100 times --> 
<div (click)="scroll()">Scroll to top</div> 

app.component.ts

import { TabHeaderComponent } from './components/tab/tab-header/tab-header.component'; 
import { Component, OnInit } from '@angular/core'; 
import { TweenLite } from 'gsap'; 
import ScrollToPlugin from "gsap/ScrollToPlugin"; 

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.sass'] 
}) 
export class AppComponent implements OnInit { 

    constructor() {} 
    ngOnInit(): void {} 


    public scroll() { 
    TweenLite.to(window, 1, { 
      scrollTo: 0 
    }); 
    } 
} 

的package.json

{ 


"name": "tabs-app", 
    "version": "0.0.0", 
    "license": "MIT", 
    "scripts": { 
    "ng": "ng", 
    "start": "ng serve", 
    "build": "ng build", 
    "test": "ng test", 
    "lint": "ng lint", 
    "e2e": "ng e2e" 
    }, 
    "private": true, 
    "dependencies": { 
    "@angular/animations": "^4.0.0", 
    "@angular/common": "^4.0.0", 
    "@angular/compiler": "^4.0.0", 
    "@angular/core": "^4.0.0", 
    "@angular/forms": "^4.0.0", 
    "@angular/http": "^4.0.0", 
    "@angular/platform-browser": "^4.0.0", 
    "@angular/platform-browser-dynamic": "^4.0.0", 
    "@angular/router": "^4.0.0", 
    "@types/gsap": "^1.19.1", 
    "core-js": "^2.4.1", 
    "gsap": "^1.20.2", 
    "rxjs": "^5.4.1", 
    "zone.js": "^0.8.14" 
    }, 
    "devDependencies": { 
    "@angular/cli": "1.2.4", 
    "@angular/compiler-cli": "^4.0.0", 
    "@angular/language-service": "^4.0.0", 
    "@types/jasmine": "~2.5.53", 
    "@types/jasminewd2": "~2.0.2", 
    "@types/node": "~6.0.60", 
    "codelyzer": "~3.0.1", 
    "jasmine-core": "~2.6.2", 
    "jasmine-spec-reporter": "~4.1.0", 
    "karma": "~1.7.0", 
    "karma-chrome-launcher": "~2.1.1", 
    "karma-cli": "~1.0.1", 
    "karma-coverage-istanbul-reporter": "^1.2.1", 
    "karma-jasmine": "~1.1.0", 
    "karma-jasmine-html-reporter": "^0.2.2", 
    "protractor": "~5.1.2", 
    "ts-node": "~3.0.4", 
    "tslint": "~5.3.2", 
    "typescript": "~2.3.3" 
    } 
} 

而且我F I点击按钮,我看到的错误实在巨大ammount的

无法执行“scrollTo”上“窗口”:参数1(“选项”)是不是一个对象

你知道, 怎么了?任何想法?

回答

1

哦,我已经找到了解决方案!它非常简单。只需将window更改为document.body即可。这是一个例子。

滚动()函数

public scroll() { 
    // can you tell me why 'document.body.scrollTop' is always equals to 0 
    // in Chrome ??? 
    if(document.body.scrollTop !== 0) { 
     TweenLite.to(document.body, 1, {scrollTop: 0}); 
    } 
    else { 
     TweenLite.to(document.documentElement, 1, {scrollTop: 0}); 
    } 
} 

我希望它能帮助

相关问题