-
Notifications
You must be signed in to change notification settings - Fork 37
/
VueLoop.vue
183 lines (153 loc) · 3.47 KB
/
VueLoop.vue
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<template>
<div class="loop-container" :class="{'full': full, 'horizontal': horizontal}">
<slot></slot>
</div>
</template>
<script>
export default {
/**
* Define props
*/
props: {
full: {
type: Boolean,
default: true
},
horizontal: {
type: Boolean,
default: false
}
},
/**
* Initialize default data
*
* @return {object}
*/
data() {
return {
pageHeight: 0,
viewportHeight: 0,
pageWidth: 0,
viewportWidth: 0,
duplicates: false,
isTouch: false
}
},
/**
* When the component is mounted
*/
mounted() {
const vm = this;
//When the window has loaded we're going to work out the dimentions
window.addEventListener('load', () => {
vm.getDimensions()
})
//When the window is resized we want to re-calculate the dimentions
window.addEventListener('resize', () => {
vm.getDimensions()
}, true);
//When the element is scrolled
vm.$el.addEventListener('scroll', (event) => {
vm.scrollHandler();
}, true);
},
methods: {
/**
* Scroll handler
*/
scrollHandler() {
const container = this.$el
if (!this.horizontal) {
var y = container.scrollTop;
if (y + this.viewportHeight > this.pageHeight) {
container.scrollTop = y % this.pageHeight;
} else if (y + this.viewportHeight == this.pageHeight) {
container.scrollTop = 0;
}
} else {
var x = container.scrollLeft;
if (x + this.viewportWidth > this.pageWidth) {
container.scrollLeft = x % this.pageWdith;
} else if (x + this.viewportWidth == this.pageWidth) {
container.scrollLeft = 0;
}
}
},
/**
* Get dimentions of the page and viewport
*/
getDimensions() {
const vm = this;
const container = vm.$el
if(vm.duplicates === false) {
vm.duplicates = vm.makeDuplicates();
}
const numOfItems = container.querySelectorAll('.item').length - vm.duplicates;
for(var i = 0; i < numOfItems; i++) {
const itemWidth = container.querySelectorAll('.item')[i].clientWidth
const itemHeight = container.querySelectorAll('.item')[i].clientHeight
vm.pageHeight = vm.pageHeight + itemHeight
vm.pageWidth = vm.pageWidth + itemWidth
}
vm.viewportHeight = container.clientHeight
vm.viewportWidth = container.clientWidth
},
/**
* Make duplicates so the scroll is smooth
*/
makeDuplicates() {
const container = this.$el
if(this.horizontal) {
var containerSize = container.clientWidth
var itemSize = container.childNodes[0].clientWidth
}
if(!this.horizontal) {
var containerSize = container.clientHeight
var itemSize = container.childNodes[0].clientHeight
}
var division = containerSize / itemSize;
for(var i = 0; i < division + 1; i++) {
container.appendChild(container.childNodes[i].cloneNode(true));
}
return division;
}
}
}
</script>
<style>
.loop-container {
display: block;
overflow-y: scroll;
-webkit-overflow-scrolling: touch;
}
.loop-container.full {
display: flex;
flex-direction: column;
flex-wrap: nowrap;
align-items: stretch;
justify-content: flex-start;
position: absolute;
top: 0px;
bottom: 0px;
left: 0px;
right: 0px;
}
.loop-container.full > .item {
min-width: 100%;
min-height: 100%;
flex: 1;
backface-visibility: hidden;
transform: translate3d(0,0,0);
}
.loop-container.horizontal {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
overflow-y: hidden;
overflow-x: scroll;
}
.loop-container.full.horizontal > .item {
min-width: 100%;
flex: 1;
}
</style>