vue滚动监听插件 vue-waypoint

1.安装依赖:

npm install vue-waypoint --save-dev

2.使用:

在main.js中:

import Vue from 'vue'
import VueWaypoint from 'vue-waypoint'

// Waypoint plugin
Vue.use(VueWaypoint)

在demo.vue组件中:

html:

    <div
      class="waypoint"
      v-waypoint="{ active: true, callback: onWaypoint, options: intersectionOptions }"
    >
      <p v-for="v in 20" :key="v">qwertyuiop---{{v}}</p>
    </div>

js:

data中的参数配置: 

    intersectionOptions: {
        root: document.querySelector("#app"), //用作视口的元素,用于检查目标的可见性。必须是目标的祖先。如果未指定,则默认为浏览器视口null
        rootMargin: "0px",
        threshold: 0.33 //范围为 0-1: 阈值为1.0意味着当100%的目标在root选项指定的元素中可见时,将调用回调
      }

intersectionOptions配置参考:https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API

 

methods中滚动触发的回调方法:

    onWaypoint({ el, going, direction, _entry }) {
      // going: in, out
      // direction: top, right, bottom, left
      if (going === this.$waypointMap.GOING_IN) {
        console.log("waypoint going in!"); //激活-出现了
      }
      if (going === this.$waypointMap.GOING_OUT) {
        console.log("waypoint going out!");
      }

      if (direction === this.$waypointMap.DIRECTION_TOP) {
        console.log("waypoint going top!");
      }
      if (direction === this.$waypointMap.DIRECTION_BOTTOM) {
        console.log("waypoint going bottom!");
      }
    }

css:

.waypoint {
  height: 500px;
  background: #420983;
}
.waypoint p {
  background: #42b983;
  margin: 3px;
}

3.介绍:

1.当向上滚动到出现33%的时候,触发回调函数的top

2.当继续向上滚动到出现剩余的33%的时候,再次触发回调函数的top

3.当向下滚动到出现33%的时候,触发回调函数的bottom

4.当继续向下滚动到出现剩余的33%的时候,触发回调函数的bottom

 

参考资料:https://github.com/scaccogatto/vue-waypoint

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值