This commit is contained in:
李志鹏
2026-05-15 11:09:36 +08:00
parent 9235843f25
commit 99af8da607
3 changed files with 17 additions and 8 deletions

View File

@@ -87,7 +87,7 @@ function add(el, root = document) {
obj.observer.observe(el) obj.observer.observe(el)
return return
} }
resize.observe(root) resize.observe(isDocumentRoot(root))
root.addEventListener('scroll', handleScroll) root.addEventListener('scroll', handleScroll)
const observer = new IntersectionObserver((entries) => { const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => { entries.forEach((entry) => {
@@ -112,7 +112,7 @@ function remove(el, root = document) {
} }
if (obj.els.length === 0) { if (obj.els.length === 0) {
obj.observer.disconnect() obj.observer.disconnect()
resize.unobserve(root) resize.unobserve(isDocumentRoot(root))
root.removeEventListener('scroll', handleScroll) root.removeEventListener('scroll', handleScroll)
roots.delete(root) roots.delete(root)
} }
@@ -130,9 +130,10 @@ async function handleScroll({ target: root }) {
if (!item.scroll) return if (!item.scroll) return
const children = el.querySelectorAll(typesStr) const children = el.querySelectorAll(typesStr)
if (children.length === 0) return if (children.length === 0) return
const elTop_bottom = root.offsetHeight - (el.offsetTop - root.offsetTop - root.scrollTop) const rootEl = isDocumentRoot(root)
const maxHeight = root.offsetHeight + el.offsetHeight const elTop_bottom = rootEl.offsetHeight - (el.offsetTop - rootEl.offsetTop - rootEl.scrollTop)
const p = Math.min(1, Math.max(0, elTop_bottom / maxHeight)) const maxHeight = rootEl.offsetHeight + el.offsetHeight
const p = Math.min(1, Math.max(0, elTop_bottom / maxHeight))
children.forEach((item) => { children.forEach((item) => {
item.style.transition = 'transform 0.5s ease-out' item.style.transition = 'transform 0.5s ease-out'
@@ -165,4 +166,8 @@ function hasAttr(el, attr) {
} else { } else {
return el.hasAttribute(attr) return el.hasAttribute(attr)
} }
} }
// 检查document是否为根元素
function isDocumentRoot(root) {
return root === document ? document.documentElement : root
}

View File

@@ -2,7 +2,9 @@ import type { App } from 'vue'
export default { export default {
install(app: App) { install(app: App) {
const directivesList = import.meta.glob('./*.ts', { eager: true }) as any; const directivesList1 = import.meta.glob('./*.ts', { eager: true }) as any;
const directivesList2 = import.meta.glob('./*.js', { eager: true }) as any;
const directivesList = { ...directivesList1, ...directivesList2 };
Object.keys(directivesList).forEach(key => { Object.keys(directivesList).forEach(key => {
app.directive(directivesList[key].default.name, directivesList[key].default); app.directive(directivesList[key].default.name, directivesList[key].default);
}); });

View File

@@ -18,13 +18,15 @@
</script> </script>
<template> <template>
<section class="product-feature" :class="{ 'product-feature-reversed': reversed }"> <section class="product-feature" :class="{ 'product-feature-reversed': reversed }" v-custom-animation.scroll>
<div class="product-feature-art"> <div class="product-feature-art">
<img <img
class="product-feature-bg" class="product-feature-bg"
:src="backgroundImage" :src="backgroundImage"
:alt="backgroundAlt" :alt="backgroundAlt"
loading="lazy" loading="lazy"
translate-x-s="-100"
translate-x="100"
/> />
<img class="product-feature-panel" :src="panelImage" :alt="panelAlt" loading="lazy" /> <img class="product-feature-panel" :src="panelImage" :alt="panelAlt" loading="lazy" />
</div> </div>