mirror of
https://github.com/youzan/vant.git
synced 2025-10-21 19:24:16 +00:00
[new feature] add List component (#682)
This commit is contained in:
118
packages/list/index.vue
Normal file
118
packages/list/index.vue
Normal file
@@ -0,0 +1,118 @@
|
||||
<template>
|
||||
<div class="van-list">
|
||||
<slot />
|
||||
<div class="van-list__loading" v-show="loading">
|
||||
<slot name="loading">
|
||||
<loading />
|
||||
<span class="van-list__loading-text">{{ $t('loadingTip') }}</span>
|
||||
</slot>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import create from '../utils/create';
|
||||
import utils from '../utils/scroll';
|
||||
import { on, off } from '../utils/event';
|
||||
|
||||
export default create({
|
||||
name: 'van-list',
|
||||
|
||||
model: {
|
||||
prop: 'loading'
|
||||
},
|
||||
|
||||
props: {
|
||||
loading: Boolean,
|
||||
finished: Boolean,
|
||||
immediateCheck: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
offset: {
|
||||
type: Number,
|
||||
default: 300
|
||||
}
|
||||
},
|
||||
|
||||
mounted() {
|
||||
this.scroller = utils.getScrollEventTarget(this.$el);
|
||||
this.handler(true);
|
||||
|
||||
if (this.immediateCheck) {
|
||||
this.$nextTick(this.onScroll);
|
||||
}
|
||||
},
|
||||
|
||||
destroyed() {
|
||||
this.handler(false);
|
||||
},
|
||||
|
||||
activated() {
|
||||
/* istanbul ignore next */
|
||||
this.handler(true);
|
||||
},
|
||||
|
||||
deactivated() {
|
||||
/* istanbul ignore next */
|
||||
this.handler(false);
|
||||
},
|
||||
|
||||
watch: {
|
||||
loading() {
|
||||
this.$nextTick(this.onScroll);
|
||||
},
|
||||
|
||||
finished() {
|
||||
this.$nextTick(this.onScroll);
|
||||
}
|
||||
},
|
||||
|
||||
methods: {
|
||||
onScroll() {
|
||||
if (this.loading || this.finished) {
|
||||
return;
|
||||
}
|
||||
|
||||
const el = this.$el;
|
||||
const { scroller } = this;
|
||||
const scrollerHeight = utils.getVisibleHeight(scroller);
|
||||
|
||||
/* istanbul ignore next */
|
||||
if (!scrollerHeight) {
|
||||
return;
|
||||
}
|
||||
|
||||
const scrollTop = utils.getScrollTop(scroller);
|
||||
const targetBottom = scrollTop + scrollerHeight;
|
||||
|
||||
let reachBottom = false;
|
||||
|
||||
/* istanbul ignore next */
|
||||
if (el === scroller) {
|
||||
reachBottom = scroller.scrollHeight - targetBottom < this.offset;
|
||||
} else {
|
||||
const elBottom =
|
||||
utils.getElementTop(el) -
|
||||
utils.getElementTop(scroller) +
|
||||
utils.getVisibleHeight(el);
|
||||
reachBottom = elBottom - scrollerHeight < this.offset;
|
||||
}
|
||||
|
||||
/* istanbul ignore else */
|
||||
if (reachBottom) {
|
||||
this.$emit('input', true);
|
||||
this.$emit('load');
|
||||
}
|
||||
},
|
||||
|
||||
handler(bind) {
|
||||
/* istanbul ignore else */
|
||||
if (this.binded !== bind) {
|
||||
this.binded = bind;
|
||||
(bind ? on : off)(this.scroller, 'scroll', this.onScroll);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
</script>
|
Reference in New Issue
Block a user