- Spring Boot 后端服务 (hss-home-service) - delivery-miniapp 配送小程序 - website 官网 (Nuxt) - docs 架构设计文档 - Docker 容器化部署配置 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
41 lines
1.0 KiB
TypeScript
41 lines
1.0 KiB
TypeScript
import { ref, onMounted, onBeforeUnmount } from 'vue'
|
|
|
|
interface ScrollAnimOptions {
|
|
delay?: number
|
|
class?: string
|
|
threshold?: number
|
|
}
|
|
|
|
export function useScrollAnim(options: ScrollAnimOptions = {}) {
|
|
const { delay = 0, threshold = 0.15 } = options
|
|
const isVisible = ref(false)
|
|
let observer: IntersectionObserver | null = null
|
|
|
|
function observe(el: Element, overrides: ScrollAnimOptions = {}) {
|
|
const d = overrides.delay ?? delay
|
|
const cls = overrides.class ?? 'flow-visible'
|
|
observer = new IntersectionObserver(
|
|
(entries) => {
|
|
entries.forEach((entry) => {
|
|
if (entry.isIntersecting) {
|
|
setTimeout(() => {
|
|
entry.target.classList.add(cls)
|
|
}, d)
|
|
observer?.unobserve(entry.target)
|
|
}
|
|
})
|
|
},
|
|
{ threshold }
|
|
)
|
|
observer.observe(el)
|
|
}
|
|
|
|
function unobserveAll() {
|
|
observer?.disconnect()
|
|
}
|
|
|
|
onBeforeUnmount(() => unobserveAll())
|
|
|
|
return { isVisible, observe, unobserve: unobserveAll }
|
|
}
|