/ 定义工作 registerPaint('scroll-based-background', class { static get inputProperties() { return ['--scroll-position ']; } paint(ctx, size, properties) { const scrollPosition = properties.get('--scroll-position' scrollPosition来改变背景 // ... } }); CSS 调用: body { background-paint: scroll-based-background; --scroll-position : 0; } 用 JavaScript 来根据滚动位置更新 --scroll-position: window.addEventListener('scroll', () => { document.body.style.setProperty ('--scroll-position', window.scrollY); }); 再比如,来绘制一个圆形: // 首先,检测浏览器是否支持Paint API if ('paintWorklet' in
this.style.willChange = 'auto'; } 语法说明 Formal syntax: auto | <animateable-feature># <animateable-feature> = scroll-position | contents | <custom-ident> will-change: auto will-change: scroll-position will-change: contents will-change scroll-position 表示开发者将要改变元素的滚动位置。 例如,浏览器通常仅呈现可滚动元素“滚动窗口”中的内容。而某些内容超过该窗口。
will-change: auto will-change: scroll-position will-change: contents will-change: transform //
MDN上显示该属性语法如下: /* 关键字值 */ will-change: auto; will-change: scroll-position; will-change: contents; will-change scroll-position 告诉浏览器,我要开始翻滚了。 contents 告诉浏览器,内容要动画或变化了。 <custom-ident> 顾名思意,自定义的识别。 同时不能是以下这些关键字值:unset, initial, inherit, will-change, auto, scroll-position, 或 contents.
默认写在前面的时间为duration */ will-change 增强页面渲染性能,提前通知浏览器元素将要做什么动画,让浏览器提前准备合适的优化设置 will-change: auto / scroll-position / contents / <custom-ident> / <animateable-feature>; /*参数说明 auto:此关键字表示没有特定的意图,适用于它通常所做的任何启发式和优化 scroll-position
隔离列表项的渲染范围,提升浏览器优化能力/* 列表容器优化 */.virtual-list-container { contain: strict; /* 告诉浏览器此区域独立渲染 */ will-change: scroll-position
webkit-transform: translateZ(0);}/* 优化滚动性能 */scroll-view { -webkit-overflow-scrolling: touch; will-change: scroll-position
Pura70.ProAI代码解释/* 列表容器优化 */.virtual-list-container { contain: strict; /* 告诉浏览器此区域独立渲染 */ will-change: scroll-position
will-change 接收各种各样的属性值,比如一个或多个 CSS 属性 ( transform, opacity)、 contents 或者 scroll-position。
scroll-position表示,顾名思义,您希望在不久的将来随时更改元素的滚动位置。 一些位于在可滚动的元素中的内容需要未来在滚动视窗内可见的时候,该值可用于提示浏览器准备渲染内容。
scroll-position表示,顾名思义,您希望在不久的将来随时更改元素的滚动位置。 该值可用于提示浏览器准备渲染内容,而不是可滚动元素上的滚动窗口中可见的内容。
max-content', fit: 'fit-content', }), willChange: { auto: 'auto', scroll: 'scroll-position
使用方法示例:(具体每个取值的意义,去翻翻文档) will-change: auto will-change: scroll-position will-change: contents will-change