Skip to content

Different native and polyfill #481

@this-spring

Description

@this-spring

use

  1. img in shadowroot
  2. transform to change img position

different

When the button is clicked, the native performance will output shadowImg2, and the polyfill will display abnormally.

code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <h1>native</h1>
  <template id="userCardTemplate">
    <style>
       .scroll1 {
        height: 200px;
        width: 400px;
        position: relative;
        overflow: hidden;
      }
    </style>
    <div class="scroll1">
      <img id="shadowImg1" src="../png.png" style="transform: translate(0%, 0%) translateZ(0)" alt="" srcset="">
      <img id="shadowImg2" src="../png.png" style="transform: translate(0%, 0%) translateZ(0)" alt="" srcset="">
    </div>
    <button id="translate">translate</button>
  </template>
  <user-card></user-card>

</body>
<script>
const intersectionObserver = new IntersectionObserver(function(entries) {
  for (let i = 0; i < entries.length; i += 1) {
    if (entries[i].intersectionRatio !== 0) {
        console.log(entries[i].target.id);
      }
  }
});
class UserCard extends HTMLElement {
  constructor() {
    super();
    const shadow = this.attachShadow( { mode: 'closed' } );

    const templateElem = document.getElementById('userCardTemplate');
    this.content = templateElem.content.cloneNode(true);

    const imgs = this.content.querySelectorAll('img');
    for(let i = 0; i < imgs.length; i += 1) {
      intersectionObserver.observe(imgs[i]);
    };

    const img = this.content.querySelector('#shadowImg');
    this.count = 1;
    this.content.querySelector('#translate').addEventListener('click', () => {
      imgs[this.count - 1].style['transform'] = `translate(0%, ${-100}%) translateZ(0)`;
      imgs[this.count].style['transform'] = `translate(0%, ${-100}%) translateZ(0)`;
    });
    shadow.appendChild(this.content);
  }
}
  window.customElements.define('user-card', UserCard);
</script>
</html>

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions