交叉

QIntersection 组件本质上是 Intersection 指令的封装,它的好处是可以自己处理状态(不需要您手动添加状态),并且可以有显示/隐藏的过渡效果。

但是,使用 QIntersection 的最主要的好处是,DOM 树释放了隐藏的节点,因此使用了尽可能少的内存,并使页面感觉非常流畅。此外,您还可以为包装器元素指定 tag 属性以满足自己的需要,从而消除另一个 DOM 节点。

它在底层使用了 Intersection Observer API

WARNING

并非所有浏览器都支持 Intersection Observer API。 大多数现代浏览器都可以,但是其他浏览器,如 IE 11 却没有支持。 如果需要支持较旧的浏览器,则可以安装和导入(在启动文件中)W3C 官方的 polyfill

QIntersection API

QIntersection API


once
: Boolean
说明
只触发一次
ssr-prerender
: Boolean
说明
如果使用 SSR,请在服务器端预渲染内容(使用它来预渲染可见区域的内容)
root
: Element | null
说明
[Intersection API 根属性] 允许您定义一个替代视口作为根元素(通过其 DOM 元素);需要注意的是,根元素必须是被观察元素的祖先。
margin
: String
说明
[Intersection API rootMargin prop] 允许您指定根元素的边距,有效地允许您扩大或缩小用于交叉的区域
threshold
: Array | Number
说明
[Intersection API threshold prop] 触发的阈值,以比例或比例列表的形式指定,表示观察元素的可见区域与总区域之间的比例。
transition
: String
说明
Quasar 的内置过渡之一
transition-duration
: String | Number
v2.3.1+
说明
过渡持续时间(以毫秒为单位,不包括单位)
disable
: Boolean
说明
禁用可见性观察(内容将保持原样,可见或隐藏)

用法

WARNING

在大多数情况下,要求将 CSS 应用于 QIntersection 元素,以便在不渲染内部内容时将其用作必要的填充符。 这将提供平滑的滚动体验,因为不这样的话滚动将会不规律地跳跃。

需要的 CSS 属性示例,例如,固定高度或至少最小高度(甚至可能是固定宽度,如下面的示例所示,可以在同一行上显示多个 QIntersections)。

WARNING

如果使用 transition 属性,则要求将内容包裹在一个且仅一个元素中。

TIP

在某些情况下,默认 viewport 不起作用。例如,当您的代码托管在 iframe(如 Codepen )中时。这时您需要使用 root 属性。它允许您将 viewport 的替代项定义为根元素(通过其 DOM 元素)。记住根元素必须是被观察元素的祖先。

基础



<template>
  <div class="q-pa-md">
    <div class="row justify-center q-gutter-sm">
      <q-intersection
        v-for="index in 60"
        :key="index"
        class="example-item"
      >
        <q-card class="q-ma-sm">
          <img src="https://cdn.quasar.dev/img/mountains.jpg">

          <q-card-section>
            <div class="text-h6">Card #{{ index }}</div>
            <div class="text-subtitle2">by John Doe</div>
          </q-card-section>
        </q-card>
      </q-intersection>
    </div>
  </div>
</template>

过渡效果

在下面的示例中,我们使用了 Quasar 中的过渡效果。有关完整列表,请转到过渡效果页面。



<template>
  <div class="q-pa-md">
    <div class="row justify-center q-gutter-sm">
      <q-intersection
        v-for="index in 60"
        :key="index"
        transition="scale"
        class="example-item"
      >
        <q-card class="q-ma-sm">
          <img src="https://cdn.quasar.dev/img/mountains.jpg">

          <q-card-section>
            <div class="text-h6">Card #{{ index }}</div>
            <div class="text-subtitle2">by John Doe</div>
          </q-card-section>
        </q-card>
      </q-intersection>
    </div>
  </div>
</template>



<template>
  <div class="q-pa-md flex justify-center">
    <div style="max-width: 90%; width: 300px;">
      <q-intersection
        v-for="index in 60"
        :key="index"
        transition="flip-right"
        class="example-item"
      >
        <q-item clickable v-ripple>
          <q-item-section avatar>
            <q-avatar color="primary" text-color="white">
              Q
            </q-avatar>
          </q-item-section>

          <q-item-section>
            <q-item-label>Contact #{{ index }}</q-item-label>
            <q-item-label caption lines="1">some@email.com</q-item-label>
          </q-item-section>

          <q-item-section side>
            <q-icon name="chat_bubble" color="green" />
          </q-item-section>
        </q-item>
      </q-intersection>
    </div>
  </div>
</template>

一次性

但是,仅触发一次就意味着您失去了释放 DOM 树的好处。无论可见性如何,内容都将保留在 DOM 中



<template>
  <div class="q-pa-md">
    <div class="row justify-center q-gutter-sm">
      <q-intersection
        v-for="index in 60"
        :key="index"
        once
        transition="scale"
        class="example-item"
      >
        <q-card class="q-ma-sm">
          <img src="https://cdn.quasar.dev/img/mountains.jpg">

          <q-card-section>
            <div class="text-h6">Card #{{ index }}</div>
            <div class="text-subtitle2">by John Doe</div>
          </q-card-section>
        </q-card>
      </q-intersection>
    </div>
  </div>
</template>

下面的示例使用了 root 属性,因此可以在 Codepen 中使用(在 iframe 中)。



<template>
  <div class="q-pa-md">
    <div ref="myListRef" class="row justify-center q-gutter-sm">
      <q-intersection
        v-for="index in 60"
        :key="index"
        :root="listEl"
        transition="scale"
        class="example-item"
      >
        <q-card class="q-ma-sm">
          <img src="https://cdn.quasar.dev/img/mountains.jpg">

          <q-card-section>
            <div class="text-h6">Card #{{ index }}</div>
            <div class="text-subtitle2">by John Doe</div>
          </q-card-section>
        </q-card>
      </q-intersection>
    </div>
  </div>
</template>