Files
3dealer/resources/js/stimulus/back-to-top-controller.js
T
2026-07-31 17:41:55 +03:00

23 lines
574 B
JavaScript

import { Controller } from '@hotwired/stimulus'
export default class extends Controller {
connect() {
this.scrollHandler = this.onScroll.bind(this)
window.addEventListener('scroll', this.scrollHandler, { passive: true })
this.onScroll()
}
disconnect() {
window.removeEventListener('scroll', this.scrollHandler)
}
onScroll() {
this.element.classList.toggle('is-visible', window.scrollY > 300)
}
scrollToTop(e) {
e.preventDefault()
window.scrollTo({ top: 0, behavior: 'smooth' })
}
}