class CustomBackToTop extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
`;
const btn = this.shadowRoot.getElementById('back-to-top');
// Show/hide button based on scroll position
window.addEventListener('scroll', () => {
if (window.scrollY > 500) {
this.classList.add('visible');
} else {
this.classList.remove('visible');
}
});
// Scroll to top on click
btn.addEventListener('click', () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
}
}
customElements.define('custom-back-to-top', CustomBackToTop);