Skip to content

Commit 762bc4d

Browse files
committed
readme
1 parent 2499804 commit 762bc4d

File tree

2 files changed

+199
-13
lines changed

2 files changed

+199
-13
lines changed

.travis.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
language: node_js
2+
node_js: stable
3+
4+
# cache:
5+
# directories:
6+
# - node_modules
7+
8+
before_install:
9+
- export TZ='Asia/Beijing' # 更改时区
10+
11+
install:
12+
- npm install
13+
14+
script:
15+
# 为了能在git-pages预览 单独发布带有二级域名的包
16+
- npm run build
17+
18+
after_script:
19+
- cd ./dist
20+
- git init
21+
- git config user.name "BiYuqi"
22+
- git config user.email "biyuqiwan@163.com"
23+
- git add .
24+
- git commit -m "Travis CI Auto Builder at `date +"%Y-%m-%d %H:%M"`"
25+
- git push --force --quiet "https://${VueIntersectionObserver}@${GH_REF}" master:gh-pages
26+
27+
branches:
28+
only:
29+
- master
30+
env:
31+
global:
32+
- GH_REF: github.com/BiYuqi/vue-intersection-observer.git

README.md

Lines changed: 167 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,178 @@
1-
# vue-intersection-observer
1+
# [vue-intersection-observer](https://github.com/BiYuqi/webpack-seed)
22

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+
* 根据用户是否已滚动到相应区域来灵活开始执行任务或动画
714

8-
### Compiles and hot-reloads for development
15+
## 如何使用 ( How to use? )
16+
17+
```js
18+
npm i vue-intersection-observer -S
919
```
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+
}
1159
```
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>
1271

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>
1485
```
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>
16123
```
17124

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
19129
```
20-
npm run lint
130+
131+
## IntersectionObserver做了些什么( What does IntersectionObserver do? )
132+
133+
![](http://loadingmore-1254319003.coscd.myqcloud.com/observe.png)
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+
}
21176
```
22177

23-
### Customize configuration
24-
See [Configuration Reference](https://cli.vuejs.org/config/).
178+
> 文档不够完善,会持续补充.

0 commit comments

Comments
 (0)