Skip to content

Commit 4396532

Browse files
committed
optimize code
1 parent 5be1448 commit 4396532

File tree

4 files changed

+26
-31
lines changed

4 files changed

+26
-31
lines changed

examples/pages/known-single-for.vue

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
ref="render"
4747
:total="1000"
4848
:remain="10"
49+
:height="100"
4950
>
5051
<div
5152
v-for="(item, index) in items"
@@ -65,7 +66,7 @@
6566
import Item from '../Item'
6667
6768
export default {
68-
name: 'UnknownSingle',
69+
name: 'KnownSingleFor',
6970
data() {
7071
return {
7172
items: this.$factory.get(1000),

examples/pages/known-single-prop.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
import Item from '../Item'
5858
5959
export default {
60-
name: 'UnknownSingle',
60+
name: 'KnownSingleProp',
6161
data() {
6262
return {
6363
items: this.$factory.get(1000),

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-flow-render",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"main": "dist/vue-flow-render.umd.min.js",
55
"files": [
66
"dist"

src/render.js

+22-28
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,8 @@ export default {
5151
offsetTop: 0,
5252
lastScrollTop: 0,
5353
start: 0,
54-
style: {
55-
height: 0,
56-
paddingTop: 0
57-
},
54+
flowHeight: 0,
55+
paddingTop: 0,
5856
cache: {}
5957
}
6058
},
@@ -115,7 +113,7 @@ export default {
115113
*/
116114
if (scrollTop <= 0) {
117115
this.start = 0
118-
this.style.paddingTop = 0
116+
this.paddingTop = 0
119117
return
120118
}
121119
/**
@@ -126,7 +124,7 @@ export default {
126124
* 触顶,数值修正
127125
*/
128126
if (this.start <= 0) {
129-
this.style.paddingTop = 0
127+
this.paddingTop = 0
130128
this.start = 0
131129
return
132130
}
@@ -138,7 +136,7 @@ export default {
138136
cache[start + remain - 1].top > scrollTop + this.wrapHeight ||
139137
cache[start].top > scrollTop
140138
) {
141-
this.style.paddingTop -= cache[start - 1].height
139+
this.paddingTop -= cache[start - 1].height
142140
this.start--
143141
}
144142
} else {
@@ -147,7 +145,7 @@ export default {
147145
*/
148146
if (start + remain >= total) {
149147
this.start = total - remain
150-
this.style.paddingTop = cache[total - remain].top
148+
this.paddingTop = cache[total - remain].top
151149
return
152150
}
153151
/**
@@ -158,16 +156,14 @@ export default {
158156
cache[start].bottom < scrollTop ||
159157
cache[start + remain - 1].bottom < scrollTop + this.wrapHeight
160158
) {
161-
this.style.paddingTop += cache[start].height
159+
this.paddingTop += cache[start].height
162160
this.start++
163161
}
164162
}
165163
},
166164
clear () {
167-
this.style = {
168-
height: 0,
169-
paddingTop: 0
170-
}
165+
this.flowHeight = 0
166+
this.paddingTop = 0
171167
this.start = 0
172168
this.cache = {}
173169
},
@@ -184,7 +180,7 @@ export default {
184180
*/
185181
const scrollTop = this.lastScrollTop - this.offsetTop
186182
if (scrollTop <= 0) {
187-
this.style.paddingTop = 0
183+
this.paddingTop = 0
188184
this.start = 0
189185
return
190186
}
@@ -194,7 +190,7 @@ export default {
194190
const scrollBottom = scrollTop + this.wrapHeight
195191
if (scrollBottom >= cache[total - 1].bottom) {
196192
this.start = total - remain
197-
this.style.paddingTop = cache[total - remain].top
193+
this.paddingTop = cache[total - remain].top
198194
return
199195
}
200196

@@ -217,7 +213,7 @@ export default {
217213
*/
218214
const decreaseCount = Math.abs(Math.ceil(deltaHeight / height / column))
219215
this.start -= decreaseCount
220-
this.style.paddingTop -= decreaseCount * height
216+
this.paddingTop -= decreaseCount * height
221217
} else {
222218
/**
223219
* 如果元素不等高
@@ -226,9 +222,8 @@ export default {
226222
*/
227223
for (let i = start - 1; i >= 0; i--) {
228224
if (cache[i].top <= scrollTop) {
229-
const index = Math.max(i - (remain / 2 | 0), 0)
230-
this.style.paddingTop = cache[index].top
231-
this.start = index
225+
this.paddingTop = cache[i].top
226+
this.start = i
232227
break
233228
}
234229
}
@@ -252,7 +247,7 @@ export default {
252247
*/
253248
const increaseCount = Math.abs(Math.floor(deltaHeight / height / column))
254249
this.start += increaseCount
255-
this.style.paddingTop += increaseCount * height
250+
this.paddingTop += increaseCount * height
256251
} else {
257252
/**
258253
* 如果元素不等高
@@ -261,9 +256,8 @@ export default {
261256
*/
262257
for (let i = start + remain; i < total; i++) {
263258
if (cache[i].bottom >= scrollBottom) {
264-
const index = Math.min(i - (remain / 2 | 0), total - remain)
265-
this.style.paddingTop = cache[index].top
266-
this.start = index
259+
this.paddingTop = cache[i - remain].top
260+
this.start = i - remain
267261
break
268262
}
269263
}
@@ -292,7 +286,7 @@ export default {
292286
bottom: height + top
293287
}
294288
}
295-
this.style.height = height * total / column
289+
this.flowHeight = height * total / column
296290
} else {
297291
if (this.isSingleColumn) {
298292
let beforeHeight = offset ? cache[offset - 1].bottom : 0
@@ -305,7 +299,7 @@ export default {
305299
}
306300
beforeHeight += hgt
307301
})
308-
this.style.height = beforeHeight
302+
this.flowHeight = beforeHeight
309303
} else {
310304
let offsets
311305
if (offset) {
@@ -327,7 +321,7 @@ export default {
327321
}
328322
offsets[offsets.indexOf(beforeHeight)] += hgt
329323
})
330-
this.style.height = Math.max(...offsets)
324+
this.flowHeight = Math.max(...offsets)
331325
}
332326
}
333327
},
@@ -354,8 +348,8 @@ export default {
354348
'style': {
355349
boxSizing: 'border-box',
356350
willChange: 'padding-top',
357-
paddingTop: `${this.style.paddingTop}px`,
358-
height: `${this.style.height}px`
351+
paddingTop: `${this.paddingTop}px`,
352+
height: `${this.flowHeight}px`
359353
},
360354
'class': 'vue-flow-render'
361355
}, this._filter(h))

0 commit comments

Comments
 (0)