yasTest.vue
3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<!--
* @description:
* @fileName: yasTest.vue
* @author: huzhiming
* @date: 2019-11-28 10:09:21
* @后台人员:
* @version: v1.0.0
* @path: 页面访问路径及参数说明
!-->
<template>
<div class="yasTest-wrap">
<div class="top">height:1000px</div>
<div v-expose:foo.a.b="true" ref="exposeRef" :id="`box${item.id}`" class="box" v-for="(item, index) in yasList" :key="index">
<pre>
height:100px
id: {{item.id}}
</pre>
</div>
<div class="down">height:1000px</div>
</div>
</template>
<script>
// horizontal vertical
// initHorizontalExposure
import { debounce, throttle } from 'lodash';
const mixins = {
data() {
return {
yasList: [...Array(20).keys()].map((item,index)=>({id: index, offset:null}))
}
},
created() {},
mounted() {
console.log(this);
setTimeout(() => {
window.addEventListener('scroll', debounce(throttle(this.handleScroll,500),200));
}, 0);
// window.addEventListener('scroll', throttle(this.handleScroll,500));
},
activated() {},
deactivated() {},
// beforeRouteEnter (to, from, next) {},
// beforeRouteUpdate(to, from, next) {},
// beforeRouteLeave(to, from, next) {},
destroyed() {
window.removeEventListener('scroll', null);
},
methods: {
handleScroll(e) {
const viewPortHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
let isInViewPortArr = this.$refs['exposeRef'].map(el => {
const Rect = el.getBoundingClientRect();
const top = el.getBoundingClientRect().top;
const elHeight = Rect.height;
console.log(`窗口宽度:${viewPortHeight},元素距离可视基准线top值:${top},是否在可视区域内:${top+ elHeight<0?false:top <= viewPortHeight}`);
return (top > 0 || top + elHeight > 0) ? top <= viewPortHeight : false;
});
console.log('isInViewPortArr', isInViewPortArr);
isInViewPortArr.filter((isInViewPort,index)=>{
if (isInViewPort===true) {
console.log(`此处进行曝光事件上报,需要曝光索引为`, index, isInViewPort);
}
});
}
},
computed: {},
watch: {},
components: {},
directives: {
expose: {
// 指令的定义
inserted: function (el,binding,vnode,oldVnode) {
console.log('inserted', el.getBoundingClientRect());
},
bind: function (el,binding,vnode,oldVnode) {
console.log('bind', el);
// var s = JSON.stringify
// el.innerHTML =
// 'name: ' + s(binding.name) + '<br>' +
// 'value: ' + s(binding.value) + '<br>' +
// 'expression: ' + s(binding.expression) + '<br>' +
// 'argument: ' + s(binding.arg) + '<br>' +
// 'modifiers: ' + s(binding.modifiers) + '<br>' +
// 'vnode keys: ' + Object.keys(vnode).join(', ')
},
update: function (el,binding,vnode,oldVnode) {
console.log('update', el);
},
componentUpdated: function (el,binding,vnode,oldVnode) {
console.log('componentUpdated', el);
},
unbind: function (el,binding,vnode,oldVnode) {
console.log('unbind', el);
},
}
}
}
export default {
name: 'yasTest',
mixins: [mixins],
props: {},
data() {
return {}
},
created() {},
mounted() {},
activated() {},
deactivated() {},
// beforeRouteEnter (to, from, next) {},
// beforeRouteUpdate(to, from, next) {},
// beforeRouteLeave(to, from, next) {},
destroyed() {},
methods: {},
computed: {},
watch: {},
components: {}
};
</script>
<style rel='stylesheet/scss' lang='scss' scoped>
.yasTest-wrap {
background: #f5f5f5;
.box {
width: 100vw;
height: 200px;
&:nth-child(2n) {
background: red;
}
&:nth-child(2n+1) {
background: green;
}
}
.top,.down {
height: 2000px;
}
}
//@import "./style.scss";
</style>