fixed(log): scroll bottom not emit event bug,add throttle

This commit is contained in:
JiwenBai 2019-12-31 16:47:18 +08:00
parent fe9b900528
commit 220184f32d
3 changed files with 50 additions and 34 deletions

View File

@ -1,9 +1,13 @@
# Scroll
You can easily make log scroll to top or bottom. Also you can make the scroll action silent (don't trigger events of Log of this scroll action).
```html
<n-button-group>
<n-button @click="scrollTo('bottom', false)">Scroll To Bottom</n-button>
<n-button @click="scrollTo('bottom', true)">Scroll To Bottom (silent)</n-button>
<n-button @click="scrollTo('bottom', true)"
>Scroll To Bottom (silent)</n-button
>
<n-button @click="scrollTo('top', false)">Scroll To Top</n-button>
<n-button @click="scrollTo('top', true)">Scroll To Top (silent)</n-button>
</n-button-group>
@ -19,51 +23,51 @@ You can easily make log scroll to top or bottom. Also you can make the scroll ac
```
```js
function log () {
const l = []
for (let i = 0; i < 40; ++i) {
l.push((Math.random()).toString(16))
function log() {
const l = [];
for (let i = 0; i < 10; ++i) {
l.push(Math.random().toString(16));
}
return l.join('\n') + '\n'
return l.join("\n") + "\n";
}
export default {
data () {
data() {
return {
loading: false,
log: log()
}
};
},
methods: {
clear () {
this.log = ''
clear() {
this.log = "";
},
handleRequireMore (from) {
this.$NMessage.info('Require More from ' + from)
if (this.loading) return
this.loading = true
handleRequireMore(from) {
this.$NMessage.info("Require More from " + from);
if (this.loading) return;
this.loading = true;
setTimeout(() => {
if (from === 'top') {
this.log = log() + this.log
} else if (from === 'bottom') {
this.log = this.log + log()
if (from === "top") {
this.log = log() + this.log;
} else if (from === "bottom") {
this.log = this.log + log();
}
this.loading = false
}, 1000)
this.loading = false;
}, 1000);
},
handleReachTop () {
this.$NMessage.info('Reach Top')
handleReachTop() {
this.$NMessage.info("Reach Top");
},
handleReachBottom () {
this.$NMessage.info('Reach Bottom')
handleReachBottom() {
this.$NMessage.info("Reach Bottom");
},
scrollTo(to, dismissEvent = false) {
if (to === 'bottom') {
this.$refs.log.scrollToBottom(dismissEvent)
if (to === "bottom") {
this.$refs.log.scrollToBottom(dismissEvent);
} else {
this.$refs.log.scrollToTop(dismissEvent)
this.$refs.log.scrollToTop(dismissEvent);
}
}
}
}
```
};
```

View File

@ -1,6 +1,6 @@
{
"name": "naive-ui",
"version": "0.6.7",
"version": "0.6.8",
"description": "",
"main": "index.js",
"scripts": {

View File

@ -11,7 +11,11 @@
@wheel="handleWheel"
>
<n-scrollbar ref="scrollbar" @scroll="handleScroll">
<n-log-line v-for="(line, index) in synthesizedLines" :key="index" :line="line" />
<n-log-line
v-for="(line, index) in synthesizedLines"
:key="index"
:line="line"
/>
</n-scrollbar>
<n-fade-in-height-expand-transition width>
<n-log-loader v-if="loading" />
@ -26,6 +30,7 @@ import NScrollbar from '../../Scrollbar'
import NLogLoader from './LogLoader'
import NLogLine from './LogLine'
import NFadeInHeightExpandTransition from '../../../transition/FadeInHeightExpandTransition'
import { throttle } from 'lodash'
export default {
name: 'NLog',
@ -40,7 +45,7 @@ export default {
NLog: this
}
},
mixins: [ withapp, themeable ],
mixins: [withapp, themeable],
props: {
loading: {
type: Boolean,
@ -107,6 +112,9 @@ export default {
return this.log.split('\n')
}
},
created () {
this.handleWheel = throttle(this.handleWheel, 300)
},
methods: {
getHljs () {
return this.hljs || this.$naive.hljs
@ -132,6 +140,7 @@ export default {
this.$emit('reach-bottom')
}
},
handleWheel (e) {
if (this.dismissEvent) {
this.$nextTick().then(() => {
@ -147,10 +156,13 @@ export default {
const content = this.$refs.scrollbar.$refs.scrollContent
const contentHeight = content.offsetHeight
const scrollTop = containerScrollTop
const scrollBottom = contentHeight - containerScrollTop - containerHeight
const scrollBottom =
contentHeight - containerScrollTop - containerHeight
const deltaY = e.deltaY
if (scrollTop === 0 && deltaY < 0) this.$emit('require-more', 'top')
if (scrollBottom === 0 && deltaY > 0) this.$emit('require-more', 'bottom')
if (scrollBottom <= 0 && deltaY > 0) {
this.$emit('require-more', 'bottom')
}
}
}
},