Intersection observer
v-intersect
指令使用了 Intersection Observer API。 它提供了一个易于使用的接口,用于检测元素在用户视图中可见的时间。这也用于 v-lazy 组件。
用例
滚动窗口并观看彩色的点。注意 v-card 会从 error 变为 success
template script
<template>
<div>
<div class="d-flex align-center text-center justify-center">
<v-avatar
:color="isIntersecting ? 'green lighten-1' : 'red darken-2'"
class="mr-3 swing-transition"
size="32"
></v-avatar>
</div>
<v-responsive
class="overflow-y-auto"
max-height="400"
>
<v-responsive
height="200vh"
class="d-flex align-center text-center pa-2"
>
<v-card
v-intersect="onIntersect"
class="mx-auto"
max-width="336"
>
<v-card-title>Card title</v-card-title>
<v-card-text>
Phasellus magna. Quisque rutrum. Nunc egestas, augue at pellentesque laoreet, felis eros vehicula leo, at malesuada velit leo quis pede. Aliquam lobortis. Quisque libero metus, condimentum nec, tempor a, commodo mollis, magna.
In turpis. In dui magna, posuere eget, vestibulum et, tempor auctor, justo. In turpis. Pellentesque dapibus hendrerit tortor. Ut varius tincidunt libero.
</v-card-text>
</v-card>
</v-responsive>
</v-responsive>
</div>
</template>
<script>
export default {
data: () => ({
isIntersecting: false,
}),
methods: {
onIntersect (entries, observer) {
// More information about these options
// is located here: https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API
this.isIntersecting = entries[0].isIntersecting
},
},
}
</script>
API
从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。
示例
下面是一些简单到复杂的例子。
和选项一起使用
v-intersect
指令接受选项。 可用的选项可以在 Intersection Observer API 中找到。 以下是使用 threshold
选项的示例。
template script
<template>
<div>
<div class="d-flex align-center text-center justify-center">
<v-avatar
:color="isIntersecting ? 'green lighten-1' : 'red darken-2'"
class="mr-3 swing-transition"
size="32"
></v-avatar>
</div>
<v-responsive class="overflow-y-auto" max-height="400">
<v-responsive height="200vh" class="d-flex align-center text-center pa-2">
<v-card
v-intersect="{
handler: onIntersect,
options: {
threshold: [0, 0.5, 1.0]
}
}"
class="mx-auto"
max-width="336"
>
<v-card-title>Card title</v-card-title>
<v-card-text>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.
</v-card-text>
</v-card>
</v-responsive>
</v-responsive>
</div>
</template>
<script>
export default {
data: () => ({
isIntersecting: false,
}),
methods: {
onIntersect (entries, observer) {
// More information about these options
// is located here: https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API
this.isIntersecting = entries[0].intersectionRatio >= 0.5
},
},
}
</script>
Polyfill
虽然 Intersection Observer API 默认在 IE11 中不可用,但它可以使用 polyfill 来实现。