Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
falstack committed Aug 6, 2019
1 parent 5be1448 commit 4396532
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 31 deletions.
3 changes: 2 additions & 1 deletion examples/pages/known-single-for.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
ref="render"
:total="1000"
:remain="10"
:height="100"
>
<div
v-for="(item, index) in items"
Expand All @@ -65,7 +66,7 @@
import Item from '../Item'
export default {
name: 'UnknownSingle',
name: 'KnownSingleFor',
data() {
return {
items: this.$factory.get(1000),
Expand Down
2 changes: 1 addition & 1 deletion examples/pages/known-single-prop.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
import Item from '../Item'
export default {
name: 'UnknownSingle',
name: 'KnownSingleProp',
data() {
return {
items: this.$factory.get(1000),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-flow-render",
"version": "1.0.0",
"version": "1.0.1",
"main": "dist/vue-flow-render.umd.min.js",
"files": [
"dist"
Expand Down
50 changes: 22 additions & 28 deletions src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ export default {
offsetTop: 0,
lastScrollTop: 0,
start: 0,
style: {
height: 0,
paddingTop: 0
},
flowHeight: 0,
paddingTop: 0,
cache: {}
}
},
Expand Down Expand Up @@ -115,7 +113,7 @@ export default {
*/
if (scrollTop <= 0) {
this.start = 0
this.style.paddingTop = 0
this.paddingTop = 0
return
}
/**
Expand All @@ -126,7 +124,7 @@ export default {
* 触顶,数值修正
*/
if (this.start <= 0) {
this.style.paddingTop = 0
this.paddingTop = 0
this.start = 0
return
}
Expand All @@ -138,7 +136,7 @@ export default {
cache[start + remain - 1].top > scrollTop + this.wrapHeight ||
cache[start].top > scrollTop
) {
this.style.paddingTop -= cache[start - 1].height
this.paddingTop -= cache[start - 1].height
this.start--
}
} else {
Expand All @@ -147,7 +145,7 @@ export default {
*/
if (start + remain >= total) {
this.start = total - remain
this.style.paddingTop = cache[total - remain].top
this.paddingTop = cache[total - remain].top
return
}
/**
Expand All @@ -158,16 +156,14 @@ export default {
cache[start].bottom < scrollTop ||
cache[start + remain - 1].bottom < scrollTop + this.wrapHeight
) {
this.style.paddingTop += cache[start].height
this.paddingTop += cache[start].height
this.start++
}
}
},
clear () {
this.style = {
height: 0,
paddingTop: 0
}
this.flowHeight = 0
this.paddingTop = 0
this.start = 0
this.cache = {}
},
Expand All @@ -184,7 +180,7 @@ export default {
*/
const scrollTop = this.lastScrollTop - this.offsetTop
if (scrollTop <= 0) {
this.style.paddingTop = 0
this.paddingTop = 0
this.start = 0
return
}
Expand All @@ -194,7 +190,7 @@ export default {
const scrollBottom = scrollTop + this.wrapHeight
if (scrollBottom >= cache[total - 1].bottom) {
this.start = total - remain
this.style.paddingTop = cache[total - remain].top
this.paddingTop = cache[total - remain].top
return
}

Expand All @@ -217,7 +213,7 @@ export default {
*/
const decreaseCount = Math.abs(Math.ceil(deltaHeight / height / column))
this.start -= decreaseCount
this.style.paddingTop -= decreaseCount * height
this.paddingTop -= decreaseCount * height
} else {
/**
* 如果元素不等高
Expand All @@ -226,9 +222,8 @@ export default {
*/
for (let i = start - 1; i >= 0; i--) {
if (cache[i].top <= scrollTop) {
const index = Math.max(i - (remain / 2 | 0), 0)
this.style.paddingTop = cache[index].top
this.start = index
this.paddingTop = cache[i].top
this.start = i
break
}
}
Expand All @@ -252,7 +247,7 @@ export default {
*/
const increaseCount = Math.abs(Math.floor(deltaHeight / height / column))
this.start += increaseCount
this.style.paddingTop += increaseCount * height
this.paddingTop += increaseCount * height
} else {
/**
* 如果元素不等高
Expand All @@ -261,9 +256,8 @@ export default {
*/
for (let i = start + remain; i < total; i++) {
if (cache[i].bottom >= scrollBottom) {
const index = Math.min(i - (remain / 2 | 0), total - remain)
this.style.paddingTop = cache[index].top
this.start = index
this.paddingTop = cache[i - remain].top
this.start = i - remain
break
}
}
Expand Down Expand Up @@ -292,7 +286,7 @@ export default {
bottom: height + top
}
}
this.style.height = height * total / column
this.flowHeight = height * total / column
} else {
if (this.isSingleColumn) {
let beforeHeight = offset ? cache[offset - 1].bottom : 0
Expand All @@ -305,7 +299,7 @@ export default {
}
beforeHeight += hgt
})
this.style.height = beforeHeight
this.flowHeight = beforeHeight
} else {
let offsets
if (offset) {
Expand All @@ -327,7 +321,7 @@ export default {
}
offsets[offsets.indexOf(beforeHeight)] += hgt
})
this.style.height = Math.max(...offsets)
this.flowHeight = Math.max(...offsets)
}
}
},
Expand All @@ -354,8 +348,8 @@ export default {
'style': {
boxSizing: 'border-box',
willChange: 'padding-top',
paddingTop: `${this.style.paddingTop}px`,
height: `${this.style.height}px`
paddingTop: `${this.paddingTop}px`,
height: `${this.flowHeight}px`
},
'class': 'vue-flow-render'
}, this._filter(h))
Expand Down

0 comments on commit 4396532

Please sign in to comment.