class ParallaxSection extends HTMLElement { connectedCallback() { this.attachShadow({ mode: 'open' }); this.shadowRoot.innerHTML = `
`; this.layers = [ this.shadowRoot.querySelector('.layer-1'), this.shadowRoot.querySelector('.layer-2'), this.shadowRoot.querySelector('.layer-3') ]; // Set background images from attributes if (this.hasAttribute('layer1')) { this.layers[0].style.backgroundImage = `url('${this.getAttribute('layer1')}')`; } if (this.hasAttribute('layer2')) { this.layers[1].style.backgroundImage = `url('${this.getAttribute('layer2')}')`; } if (this.hasAttribute('layer3')) { this.layers[2].style.backgroundImage = `url('${this.getAttribute('layer3')}')`; } // Parallax effect window.addEventListener('scroll', () => { const scrollY = window.scrollY; const rect = this.getBoundingClientRect(); const offsetTop = rect.top + window.scrollY; const viewportHeight = window.innerHeight; // Only animate when section is in view if (scrollY + viewportHeight > offsetTop && scrollY < offsetTop + rect.height) { const progress = (scrollY - offsetTop + viewportHeight) / (viewportHeight + rect.height); this.layers.forEach((layer, index) => { const depth = (index + 1) * 0.2; const translateY = progress * 100 * depth; layer.style.transform = `translate3d(0, ${translateY}px, 0)`; }); } }); } } customElements.define('parallax-section', ParallaxSection);