v-intersection
v0.1.6
Published
vue directive use Intersection Observer API
Downloads
565
Readme
v-intersection
a vue component for IntersectionObserver
一个 vue 的 IntersectionObserver 组件
support vue3.0
demo and document
demo and documment github page
use
install :
npm install v-intersection --save
import:
vue 2.6.11
import Vue from 'vue'
import intersection from "v-intersection";
Vue.use(intersection);
vue 3.0
import intersection from "v-intersection";
createApp(App).use(intersection ).mount('#app')
use
vue directive
<div v-intersection="handler"></div>
<script>
export default {
methods:{
handler(entries, observer, isIntersecting, ratio){
}
}
};
</script>
options:
<div
v-intersection="{handler:yourMehtod,
options = {
root: document.querySelector('#scrollArea'),
rootMargin: '0px',
threshold: [0,1]
}
}"
>
</div>
more options info in MDN document
更多配置参数请见 MDN document
modifier
once
just run once ,and IntersectionObserver.unobserve( target)
只执行一次, 执行结束后 , IntersectionObserver.unobserve(target)
<div v-intersection.once="handler"></div>
quiet
don't handle when binding directive
在绑定指令时不触发,
<div v-intersection.quiet="handler"></div>
disconect( )
<div v-intersection="handler" ref="mydom"></div>
<button @click="disconect"> disconect( ) </button>
<script>
export default {
methods:{
handler(entries, observer, isIntersecting, ratio){
},
disconect(){
this.$refs.mydom._observe.observer.disconnect()
}
}
};
</script>
unobserved ( )
<div v-intersection="handler" ref="mydom"></div>
<button @click="unobserved"> unobserved( ) </button>
<script>
export default {
methods:{
handler(entries, observer, isIntersecting, ratio){
},
unobserved(){
var observer = new IntersectionObserver(callback);
observer.observe(this.$ref.mydom)
}
}
};
</script>