代码之家  ›  专栏  ›  技术社区  ›  Daniel Birowsky Popeski

更改转换css样式将导致重新计算样式/更新层树

  •  0
  • Daniel Birowsky Popeski  · 技术社区  · 4 年前

    this example

    enter image description here

    在这个例子中,除了compositor/gpu转换之外,我如何防止其他工作?

    	const scroller = document.querySelector('scroller');
    	const width = scroller.offsetWidth;
    	const scrollWidth = scroller.scrollWidth;
    	const elements = document.querySelectorAll('scrolled-item');
    	scroller.addEventListener('scroll', () => {
    		const scroll = scroller.scrollLeft;
    		const ratio = (scroll + width) / scrollWidth;
    		requestAnimationFrame(() => {
    			elements.forEach(el => {
    				el.firstChild.style.transform = `rotateZ(${ratio * 360}deg) rotateX(${ratio * 360}deg) scaleX(${1 - ratio})`;
    			})
    		})
    	});
    	scroller {
    		display: block;
    		width: 80%;
    		margin-left: 10%;
    		margin-top: 20%;
    		overflow: auto;
    		white-space: nowrap;
    		font-size: 0;
    		background: silver
    	}
    	scrolled-item {
    		position: relative;
    		display: inline-block;
    		margin-left: 10px;
    		margin-right: 10px;
    		width: 40px;
    		height: 40px;
    
    	}
    	scrolled-item gymnastics {
    		display: block;
    		position: absolute;
    		top: 0; right: 0; bottom: 0; left: 0;
    		background: gray;
    		z-index: 1;
    	}
    	scrolled-item div {
    		position: relative;
    		text-align: center;
    		font-size: 14px;
    		z-index: 2;
    	}
    <scroller>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    	<scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    </scroller>
    0 回复  |  直到 4 年前
        1
  •  0
  •   Mister Jojo Łukasz Daniel Mastalerz    4 年前

    我不知道你在用viewport做什么,我也不认为它会妨碍css变量的使用。

    const scroller    = document.querySelector('scroller')
      ,   element0    = scroller.querySelector('scrolled-item')
      ,   off_width   = scroller.offsetWidth
      ,   scrollWidth = scroller.scrollWidth
      ;
    for (let i=0;i<58;i++)
      {
      scroller.appendChild (element0.cloneNode(true))
      }
    scroller.onscroll=()=>
      {
      let scroll = scroller.scrollLeft 
        , ratio  = (scroll + off_width) / scrollWidth
        , ratioDeg = Math.fround(ratio * 360) 
        ;
      scroller.style.setProperty('--turnXZ', ratioDeg + 'deg')
      scroller.style.setProperty('--scalX', (1 - ratio))
      }
    scroller {
      display: block;
      width: 80%;
      height: 70px;
      margin-left: 10%;
      margin-top: 20%;
      overflow: auto;
      white-space: nowrap;
      font-size: 0;
      background: silver;
      --turnXZ : 0deg;
      --scalX  : 1;
    }
    scrolled-item {
      position: relative;
      display: inline-block;
      margin-left: 10px;
      margin-right: 10px;
      width: 40px;
      height: 40px;
    }
    scrolled-item gymnastics {
      display: block;
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      background: rgba(50, 47, 209, 0.658);
      z-index: 1;
      transform: scaleX(var(--scalX)) rotateX(var(--turnXZ) ) rotateZ(var(--turnXZ) );
    }
    scrolled-item div {
      position: relative;
      text-align: center;
      font-size: 14px;
      z-index: 2;
    }
    <scroller>
      <scrolled-item><gymnastics></gymnastics><div>do</div></scrolled-item>
    </scroller>