|
1 | | -# vue-intersection-observer |
| 1 | +# [vue-intersection-observer](https://github.com/BiYuqi/webpack-seed) |
2 | 2 |
|
3 | | -## Project setup |
4 | | -``` |
5 | | -npm install |
6 | | -``` |
| 3 | +<p align="left"> |
| 4 | +</p> |
| 5 | + |
| 6 | +## 前言 |
| 7 | +**Vue Intersection Observer** 是一个Vue组件,基于[IntersectionObserver API](https://developer.mozilla.org/zh-CN/docs/Web/API/Intersection_Observer_API)的集成, 包装,便于实现需要用到元素交集变化的信息. |
| 8 | + |
| 9 | +比如: |
| 10 | +* 当页面滚动时,实现懒加载图片功能。 |
| 11 | +* 实现`可无限滚动`网站,也就是当用户滚动网页时直接加载更多内容,无需翻页。 |
| 12 | +* 为计算广告收益,检测其广告元素的曝光情况。 |
| 13 | +* 根据用户是否已滚动到相应区域来灵活开始执行任务或动画 |
7 | 14 |
|
8 | | -### Compiles and hot-reloads for development |
| 15 | +## 如何使用 ( How to use? ) |
| 16 | + |
| 17 | +```js |
| 18 | +npm i vue-intersection-observer -S |
9 | 19 | ``` |
10 | | -npm run serve |
| 20 | + |
| 21 | +在懒加载图片中使用(Usage in Lazy-load-image): |
| 22 | + |
| 23 | +[Demo](http://loadingmore.com/vue-intersection-observer/#/lazy-load) |
| 24 | + |
| 25 | +```js |
| 26 | +<!-- 基于本插件封装LazyImage组件 --> |
| 27 | +<template> |
| 28 | + <observer @on-change="onChange" class="test-lazy"> |
| 29 | + <img height="200" style="max-width: 100%" :src="currentInfo" alt=""> |
| 30 | + </observer> |
| 31 | +</template> |
| 32 | + |
| 33 | +<script> |
| 34 | +import Observer from 'vue-intersection-observer' |
| 35 | +export default { |
| 36 | + data() { |
| 37 | + return{ |
| 38 | + currentInfo: false |
| 39 | + } |
| 40 | + }, |
| 41 | + props: { |
| 42 | + imageSrc: { |
| 43 | + type: [String, Number] |
| 44 | + } |
| 45 | + }, |
| 46 | + components: { |
| 47 | + Observer |
| 48 | + }, |
| 49 | + methods: { |
| 50 | + onChange(entry, unobserve) { |
| 51 | + // 加载后 取消监听,优化性能 |
| 52 | + if (entry.isIntersecting) { |
| 53 | + unobserve() |
| 54 | + } |
| 55 | + this.currentInfo = entry.isIntersecting ? this.imageSrc : 'https://avatars2.githubusercontent.com/u/20992106?s=460&v=4' |
| 56 | + } |
| 57 | + } |
| 58 | +} |
11 | 59 | ``` |
| 60 | +实战懒加载: |
| 61 | +```js |
| 62 | +<template> |
| 63 | + <div class="w800"> |
| 64 | + <div class="header">图片懒加载测试<i class="tips">请快速向下滚动屏幕测试懒加载</i></div> |
| 65 | + <div class="content"></div> |
| 66 | + <div v-for="item in imgList" :key="item" class="img-box-mock"> |
| 67 | + <LazyImage :imageSrc="item"></LazyImage> |
| 68 | + </div> |
| 69 | + </div> |
| 70 | +</template> |
12 | 71 |
|
13 | | -### Compiles and minifies for production |
| 72 | +<script> |
| 73 | +import LazyImage from './LazyImage' |
| 74 | +export default { |
| 75 | + data() { |
| 76 | + return { |
| 77 | + imgList: [] |
| 78 | + } |
| 79 | + }, |
| 80 | + components: { |
| 81 | + LazyImage |
| 82 | + } |
| 83 | +} |
| 84 | +</script> |
14 | 85 | ``` |
15 | | -npm run build |
| 86 | + |
| 87 | +在普通场景中使用(Usage in normal): |
| 88 | +```js |
| 89 | +<template> |
| 90 | + <div> |
| 91 | + <div class="left w800"> |
| 92 | + <div class="header w800" :class="{active: visible}"> |
| 93 | + 检测元素从屏幕区域显示隐藏{{visible ? 'visible' : 'unvisible'}} |
| 94 | + </div> |
| 95 | + <div class="content"> |
| 96 | + Please Scroll Page Down and Up To Test |
| 97 | + </div> |
| 98 | + <observer @on-change="onChange"> |
| 99 | + <div class="test" :class="{active: visible}">终于出现了</div> |
| 100 | + </observer> |
| 101 | + </div> |
| 102 | + </div> |
| 103 | +</template> |
| 104 | + |
| 105 | +<script> |
| 106 | +import Observer from 'vue-intersection-observer' |
| 107 | +export default { |
| 108 | + data() { |
| 109 | + return{ |
| 110 | + visible: false |
| 111 | + } |
| 112 | + }, |
| 113 | + components: { |
| 114 | + Observer |
| 115 | + }, |
| 116 | + methods: { |
| 117 | + onChange(entry, obv) { |
| 118 | + this.visible = entry.isIntersecting |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | +</script> |
16 | 123 | ``` |
17 | 124 |
|
18 | | -### Lints and fixes files |
| 125 | +Optionally add the polyfill and make sure it's required on your dependendencies for unsupporting browsers: |
| 126 | + |
| 127 | +```js |
| 128 | +npm install --save intersection-observer |
19 | 129 | ``` |
20 | | -npm run lint |
| 130 | + |
| 131 | +## IntersectionObserver做了些什么( What does IntersectionObserver do? ) |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | +## 为什么要用这个组件 ( Why use this component? ) |
| 136 | + |
| 137 | +该组件的目的是为观察在Vue代码库上进入视口的元素提供最简单的解决方案. 它完全是声明性的,所有复杂性都被抽象出来,专注于可重用性和低内存消耗. |
| 138 | + |
| 139 | +## 文档 ( Documentation ) |
| 140 | + |
| 141 | +[DEMO](http://loadingmore.com/vue-intersection-observer) |
| 142 | + |
| 143 | +## Options And Method |
| 144 | +Name | Type | Default | Required | Description |
| 145 | +--------- | --------- | --------- | --------- | --------- |
| 146 | +root | HTMLElement | | false | 如果root参数指定为null或者不指定的时候默认使用浏览器视口做为root |
| 147 | +rootMargin | String | '0px' | false | 定义根元素的margin,用来扩展或缩小rootBounds这个矩形的大小,从而影响intersectionRect交叉区域的大小。它使用CSS的定义方法,比如10px 20px 30px 40px,表示 top、right、bottom 和 left 四个方向的值 |
| 148 | +threshold | Number or Array\<number> | 0 | false | threshold属性决定了什么时候触发回调函数。它是一个数组,每个成员都是一个门槛值,默认为[0],即交叉比例(intersectionRatio)达到0时触发回调函数 |
| 149 | +on-change | Function | | required | (entry, unobserve) => {} |
| 150 | + |
| 151 | +## Method Detail Callback parms |
| 152 | +```js |
| 153 | +<observer @on-change="onChange" class="test-lazy"> |
| 154 | + <img height="200" style="max-width: 100%" :src="currentInfo" alt=""> |
| 155 | +</observer> |
| 156 | + |
| 157 | +onChange(entry, unobserve) { |
| 158 | + // entry: |
| 159 | + // time:可见性发生变化的时间,是一个高精度时间戳,单位为毫秒 |
| 160 | + |
| 161 | + // target:被观察的目标元素,是一个 DOM 节点对象 |
| 162 | + |
| 163 | + // rootBounds:根元素的矩形区域的信息, |
| 164 | + |
| 165 | + // getBoundingClientRect()方法的返回值,如果没有根元素(即直接相对于视口滚动),则返回null |
| 166 | + |
| 167 | + // boundingClientRect:目标元素的矩形区域的信息 |
| 168 | + |
| 169 | + // intersectionRect:目标元素与视口(或根元素)的交叉区域的信息 |
| 170 | + // isIntersecting: boolean 返回当前元素在可视区域是否显示 (很重要,大部分场景基于此字段判断) |
| 171 | + |
| 172 | + // intersectionRatio:目标元素的可见比例,即intersectionRect占boundingClientRect的比例,完全可见时为1,完全不可见时小于等于0 |
| 173 | + // unobserve |
| 174 | + // The method can remove target describe |
| 175 | +} |
21 | 176 | ``` |
22 | 177 |
|
23 | | -### Customize configuration |
24 | | -See [Configuration Reference](https://cli.vuejs.org/config/). |
| 178 | +> 文档不够完善,会持续补充. |
0 commit comments