简体   繁体   English

如何将 ResizeObserver 用于多种行为

[英]How to utilize ResizeObserver for multiple behaviors

It seems thatResizeObserver is best used when you have one instance managing the behaviors of many elements (as pointed out in one of the sources from "ResizeObserver one vs multiple performance" ):当您有一个实例管理许多元素的行为时,似乎最好使用ResizeObserver (正如“ResizeObserver one vs multiple performance” 的来源之一所指出的那样):

Observe size of 126 divs during a 2 second animation of width/height.在宽度/高度的 2 秒 animation 期间观察 126 个 div 的大小。

  1. All elements are observed by a single ResizeObserver.所有元素都由单个 ResizeObserver 观察。

    Total observer time: 69ms总观察时间:69ms

  2. Each element is observed by separate ResizeObserver.每个元素由单独的 ResizeObserver 观察。

    Total observer time: 585ms总观察时间:585ms

However, as far as I can tell you can only really specify a single behavior for an instance of ResizeObserver , which then gets run for any elements that it's observing.但是,据我所知,您只能为ResizeObserver的实例真正指定一个行为,然后针对它正在观察的任何元素运行该行为。 How should I observe multiple elements with different behaviors?我应该如何观察具有不同行为的多个元素? From what I can see, I can either:据我所知,我可以:

  • Use a single instance of ResizeObserver and figure out at runtime what behavior should be used for a given element (which seems needlessly complex, and I don't know how reliable comparing element references would be in production), or使用ResizeObserver的单个实例并在运行时确定应该对给定元素使用什么行为(这似乎不必要地复杂,而且我不知道比较元素引用在生产中的可靠性如何),或者
  • Use multiple instances of ResizeObserver , which would make behavior simpler but theoretically reduce performance.使用多个ResizeObserver实例,这将使行为更简单,但理论上会降低性能。

Alternatively, are the performance losses for using multiple instances of ResizeObserver even something that I should be worried about at a small to medium scale?或者,使用多个ResizeObserver实例的性能损失甚至是我应该在中小型规模上担心的事情吗?

…figure out at runtime what behavior should be used for a given element (which seems needlessly complex, and I don't know how reliable comparing element references would be in production)… …在运行时弄清楚给定元素应该使用什么行为(这看起来不必要的复杂,而且我不知道比较元素引用在生产中的可靠性如何)…

I don't think this needs to be particularly complicated.我不认为这需要特别复杂。

You could have a collection of behaviors, each with a predicate function and a handler function.你可以有一个行为集合,每个行为都有一个predicate function 和一个handler function。 The predicate determines whether it's applicable to the given element, and the handler performs the behavior.谓词确定它是否适用于给定元素,并且处理程序执行该行为。 (This might not be super performant if you've got thousands of behaviors, but I'm assuming you don't.) (如果你有成千上万的行为,这可能不是超级性能,但我假设你没有。)

Something along these lines?这些方面的东西?

const behaviors = [
  {
    predicate: element => element.classList.contains('first'),
    handler: element => {
      // do stuff
    }
  },
  {
    predicate: element => element.classList.contains('second'),
    handler: element => {
      // do other stuff
    }
  },
];

const observer = new ResizeObserver((entries) => {
    for (let entry of entries) {
      // for single behavior
      behaviors.find(b => b.predicate(entry))?.handler(entry);
      
      // or every applicable behavior
      behaviors
        .filter(b => b.predicate(entry))
        .forEach(b => b.handler(entry);
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM