Typescript - 滚动事件的 addEventListener 类型接口

发布于 2025-01-13 16:29:24 字数 302 浏览 2 评论 0原文

监听滚动事件时添加类型接口的正确方法是什么?


    onMounted(() => {
      ...
      // Call when mounted;
    
      document.addEventListener("scroll", (e: ScrollEvent) => {
        console.log(e); // Type interface of ScrollEvent
      });
    });

What is the correct way to add type interface when listening to scroll event.


    onMounted(() => {
      ...
      // Call when mounted;
    
      document.addEventListener("scroll", (e: ScrollEvent) => {
        console.log(e); // Type interface of ScrollEvent
      });
    });

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

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

发布评论

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

评论(1

烟雨扶苏 2025-01-20 16:29:24

下面是一个示例,这是 的 MDN 页面文档:滚动事件

<一href="https://www.typescriptlang.org/play?noUncheckedIndexedAccess=true&target=99&useUnknownInCatchVariables=true&exactOptionalPropertyTypes=true#code/C4TwDgpgBAwg9nAT GEWM5QLxQN4FgBQUUAHgFxQB2ArgLYBGEiA3AUSOdfYy-gL48EAZlQoBjYAEs4FKAHMIwAMqjECADZr4SZBIOBDYB HQAKAJTktKdHkJRECqohk2iRMlADuu5HA8A6VBV1NQANABpWV3ZPb18AoI0ATQjbfgJeAiERcSkZD0QJQ2VVDUsdfU MTCDUIGggKYHgGvV1GcgAJABUAWQAZAFEauoawqFEEKwsJtHMoADc4CWQcSOra+sbpYBaKRj9DYk2GjcwoAClFAHk AOQDgAopZCUEQY3HtVFHqDVGAJlMeBl8FkxJJpFAABZ6CjIGrFYJQYwQOYbcj9FENWYLJYrWwAejxriJxJJUAAehTK VSKZECVBOhDoIESmooMiThJ0AArKioYBQaFQXR86GiaBwQRQYCMqB0PSoaDok6iNTy1CRfKFCDw0rTXQGIzGHyiWgb PzyYCDdYNABCIAAkshjAByd5WZ2mACEowtOs0eoqhtMAPSmXwXhhcT0yGQSoavU5hl2iBdzOCztGUJhcISahD+CAA" rel="nofollow noreferrer">TS Playground

type Coords = {
  x: number;
  y: number;
};

function getScrollCoordinates (): Coords {
  return {
    x: window.scrollX,
    y: window.scrollY,
  };
}

function writeScrollCoordinates (elementContainer: HTMLElement, coords: Coords): void {
  elementContainer.textContent = JSON.stringify(coords, null, 2);
}

function handleScroll (event: Event): void {
  //                   ^^^^^^^^^^^^
  // The scroll event is just an instance of the base Event class
  writeScrollCoordinates(document.getElementById('coords')!, getScrollCoordinates());
}

window.addEventListener('scroll', handleScroll);

示例:

"use strict";
function getScrollCoordinates() {
    return {
        x: window.scrollX,
        y: window.scrollY,
    };
}
function writeScrollCoordinates(elementContainer, coords) {
    elementContainer.textContent = JSON.stringify(coords, null, 2);
}
function handleScroll(event) {
    //                   ^^^^^^^^^^^^
    // The scroll event is just an instance of the base Event class
    writeScrollCoordinates(document.getElementById('coords'), getScrollCoordinates());
}
window.addEventListener('scroll', handleScroll);
body {
  --size: 10000px;
  height: var(--size);
  width: var(--size);
}

#coords {
  font-family: monospace;
  font-size: 2rem;
  position: fixed;
  top: 1rem;
  left: 1rem;
}
<pre><code id="coords">Scroll to see coordinates</code></pre>

Below is an example, and here's the MDN page for Document: scroll event.

TS Playground

type Coords = {
  x: number;
  y: number;
};

function getScrollCoordinates (): Coords {
  return {
    x: window.scrollX,
    y: window.scrollY,
  };
}

function writeScrollCoordinates (elementContainer: HTMLElement, coords: Coords): void {
  elementContainer.textContent = JSON.stringify(coords, null, 2);
}

function handleScroll (event: Event): void {
  //                   ^^^^^^^^^^^^
  // The scroll event is just an instance of the base Event class
  writeScrollCoordinates(document.getElementById('coords')!, getScrollCoordinates());
}

window.addEventListener('scroll', handleScroll);

Example:

"use strict";
function getScrollCoordinates() {
    return {
        x: window.scrollX,
        y: window.scrollY,
    };
}
function writeScrollCoordinates(elementContainer, coords) {
    elementContainer.textContent = JSON.stringify(coords, null, 2);
}
function handleScroll(event) {
    //                   ^^^^^^^^^^^^
    // The scroll event is just an instance of the base Event class
    writeScrollCoordinates(document.getElementById('coords'), getScrollCoordinates());
}
window.addEventListener('scroll', handleScroll);
body {
  --size: 10000px;
  height: var(--size);
  width: var(--size);
}

#coords {
  font-family: monospace;
  font-size: 2rem;
  position: fixed;
  top: 1rem;
  left: 1rem;
}
<pre><code id="coords">Scroll to see coordinates</code></pre>

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