属性“querySelector”类型“文档”上不存在

发布于 2025-01-14 02:44:58 字数 3038 浏览 1 评论 0原文

我正在跟随 youtube 上的一些人使用 GSAP 创建带有动画的登陆页面, 但在他使用滚动动画的部分,他使用了 gsap.to() 方法,但诀窍是何时使用

docement.querySelector('string here to class name')

它给了我一个错误 属性“querySelector”在“文档”类型上不存在,

但它与该人完美配合..我做了我的研究,但找不到任何有用的东西,这里是代码


import { Document } from './../../node_modules/yaml/index.d';
import { DOCUMENT } from '@angular/common';
import {
  Component,
  ElementRef,
  Inject,
  Injectable,
  OnInit,
  ViewChild,
} from '@angular/core';
import { gsap } from 'gsap';
import { ScrollTrigger } from 'gsap/ScrollTrigger';

gsap.registerPlugin(ScrollTrigger);
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})

@Injectable()
export class AppComponent implements OnInit {
  @ViewChild('secondSection', { static: true })
  secondSection!: ElementRef<HTMLDivElement>;
  @ViewChild('imageFirst', { static: true })
  imageFirst!: ElementRef<HTMLDivElement>;
  @ViewChild('imageSecond', { static: true })
  imageSecond!: ElementRef<HTMLDivElement>;
  @ViewChild('menu', { static: true })
  menu!: ElementRef<HTMLDivElement>;
  @ViewChild('heading_1', { static: true })
  heading_1!: ElementRef<HTMLHeadingElement>;

  constructor(@Inject(DOCUMENT) private document: Document) {}

  ngOnInit(): void {
    this.initialAnimations();
    this.initScrollAnimations();
  }

  initScrollAnimations(): void {
    gsap.to(this.imageFirst.nativeElement, {
      scrollTrigger: {
        trigger: this.imageFirst.nativeElement,
        scrub: true,
        start: '120% center',
      },
      duration: 1.1,
      scale: 1.2,
      height: 250,
    });

    gsap.to(this.imageSecond.nativeElement, {
      scrollTrigger: {
        trigger: this.imageSecond.nativeElement,
        scrub: true,
        start: '80% center',
      } as ScrollTrigger.Vars,
      duration: 1.1,
      scale: 1.2,
      height: 380,
    });

    gsap.to(this.heading_1.nativeElement, {
      scrollTrigger: {
        trigger: this.heading_1.nativeElement,
        scrub: true,
        start: '150% center',
      } as ScrollTrigger.Vars,
      color: '#fff',
      duration: 1.5,
    });

    gsap.to(this.document.querySelector('.paragraph'), {
      scrollTrigger: {
        trigger: this.document.get('.paragraph'),
        scrub: true,
        start: '150% center',
      } as ScrollTrigger.Vars,

      color: '#fff',
      duration: 1.5,
    });

    gsap.to(this.document.querySelector('.btn'), {
      scrollTrigger: {
        trigger: this.document.get('.btn'),
        scrub: true,
        start: '150% center',
      } as ScrollTrigger.Vars,

      color: '#fff',
      duration: 1.5,
    });

    gsap.to(this.document.querySelector('#story-line'), {
      scrollTrigger: {
        trigger: this.document.get('#story-line'),
        scrub: true,
        toggleClass: 'active',
        start: 'top center',
      } as ScrollTrigger.Vars,

      opacity: 0,
      y: 40,
      duration: 1.5,
    });

i was following along some dude in youtube creating a landing page with animation using GSAP ,
but at the part when he uses scrollanimations he used gsap.to() method but the trick is when is use

docement.querySelector('string here to class name')

it gives me an error
Property 'querySelector' does not exist on type 'Document'

but it works perfectly with the man .. i did my research but can't find no thing useful here is the code


import { Document } from './../../node_modules/yaml/index.d';
import { DOCUMENT } from '@angular/common';
import {
  Component,
  ElementRef,
  Inject,
  Injectable,
  OnInit,
  ViewChild,
} from '@angular/core';
import { gsap } from 'gsap';
import { ScrollTrigger } from 'gsap/ScrollTrigger';

gsap.registerPlugin(ScrollTrigger);
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})

@Injectable()
export class AppComponent implements OnInit {
  @ViewChild('secondSection', { static: true })
  secondSection!: ElementRef<HTMLDivElement>;
  @ViewChild('imageFirst', { static: true })
  imageFirst!: ElementRef<HTMLDivElement>;
  @ViewChild('imageSecond', { static: true })
  imageSecond!: ElementRef<HTMLDivElement>;
  @ViewChild('menu', { static: true })
  menu!: ElementRef<HTMLDivElement>;
  @ViewChild('heading_1', { static: true })
  heading_1!: ElementRef<HTMLHeadingElement>;

  constructor(@Inject(DOCUMENT) private document: Document) {}

  ngOnInit(): void {
    this.initialAnimations();
    this.initScrollAnimations();
  }

  initScrollAnimations(): void {
    gsap.to(this.imageFirst.nativeElement, {
      scrollTrigger: {
        trigger: this.imageFirst.nativeElement,
        scrub: true,
        start: '120% center',
      },
      duration: 1.1,
      scale: 1.2,
      height: 250,
    });

    gsap.to(this.imageSecond.nativeElement, {
      scrollTrigger: {
        trigger: this.imageSecond.nativeElement,
        scrub: true,
        start: '80% center',
      } as ScrollTrigger.Vars,
      duration: 1.1,
      scale: 1.2,
      height: 380,
    });

    gsap.to(this.heading_1.nativeElement, {
      scrollTrigger: {
        trigger: this.heading_1.nativeElement,
        scrub: true,
        start: '150% center',
      } as ScrollTrigger.Vars,
      color: '#fff',
      duration: 1.5,
    });

    gsap.to(this.document.querySelector('.paragraph'), {
      scrollTrigger: {
        trigger: this.document.get('.paragraph'),
        scrub: true,
        start: '150% center',
      } as ScrollTrigger.Vars,

      color: '#fff',
      duration: 1.5,
    });

    gsap.to(this.document.querySelector('.btn'), {
      scrollTrigger: {
        trigger: this.document.get('.btn'),
        scrub: true,
        start: '150% center',
      } as ScrollTrigger.Vars,

      color: '#fff',
      duration: 1.5,
    });

    gsap.to(this.document.querySelector('#story-line'), {
      scrollTrigger: {
        trigger: this.document.get('#story-line'),
        scrub: true,
        toggleClass: 'active',
        start: 'top center',
      } as ScrollTrigger.Vars,

      opacity: 0,
      y: 40,
      duration: 1.5,
    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红玫瑰 2025-01-21 02:44:58

使用 gsap,您不需要传递实际的 DOM 元素,您只需作为字符串传递,gsap 将为您查询选择器。

所以而不是
gsap.to(this.document.querySelector('.btn') ...

执行
gsap.to('.btn') ...

应该可以正常工作

然后对 trigger 对象属性执行相同的操作

With gsap you don't need to pass the actual DOM element, you can just pass as a string and gsap will querySelector for you.

so instead of
gsap.to(this.document.querySelector('.btn') ...

Do
gsap.to('.btn') ...

Should work just fine

Then do the same to the trigger object property

盛装女皇 2025-01-21 02:44:58

我不知道为什么这段代码存在:

constructor(@Inject(DOCUMENT) private document: Document) {}

我会删除它。

然后我会转到您的 tsconfig.json 并确保 'dom' 位于 "lib": ["es2018", "dom"] 这将允许您直接从 *.ts 文件引用 document,从而启用 document.querySelector('.paragraph')

请注意,document.querySelector('.paragraph') 中不再有 this. 了。

https://stackblitz.com/edit/角度-ivy-h6kzqj?file=src/app/app.component.ts

I don't know why this code exists:

constructor(@Inject(DOCUMENT) private document: Document) {}

I would remove it.

Then I would instead go to your tsconfig.json and ensure that 'dom' is in "lib": ["es2018", "dom"] This will allow you to just directly reference document from your *.ts files, enabling document.querySelector('.paragraph').

Note that there is no this. in document.querySelector('.paragraph') anymore.

https://stackblitz.com/edit/angular-ivy-h6kzqj?file=src/app/app.component.ts

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文