magnetar/fe_calckey/frontend/client/src/directives/appear.ts

23 lines
518 B
TypeScript
Raw Normal View History

2023-07-07 19:22:30 +00:00
import { Directive } from "vue";
export default {
mounted(src, binding, vn) {
const fn = binding.value;
if (fn == null) return;
2023-07-07 19:22:30 +00:00
const observer = new IntersectionObserver((entries) => {
if (entries.some((entry) => entry.isIntersecting)) {
fn();
}
});
2023-07-07 19:22:30 +00:00
observer.observe(src);
2023-07-07 19:22:30 +00:00
src._observer_ = observer;
},
2023-07-07 19:22:30 +00:00
unmounted(src, binding, vn) {
if (src._observer_) src._observer_.disconnect();
},
2023-07-07 19:22:30 +00:00
} as Directive;