用Vue保持滚动的滚动
我正在使用VUE制作一个类似聊天室的网站。
我使用显示:Flex& CSS上的列逆转到反向消息查看。
并使用Array Unshift插入新消息。
在Chrome& Firefox,这将始终保持最新消息(底部)。
但是野生动物园不会。
在附加新消息后,我尝试了方法“ scrollto”,但是如果没有用户交互,它似乎无法正常工作。
(在很短的时间之前,可能没有任何用户交互的新消息可以附加)
我还尝试检测底部是否使用“ scrolltop”(然后我会注意到用户一条新消息会出现),但在Safari上也失败了。
有其他方法使Safari的工作与Chrome/Firefox一样吗?
这里的演示可以与Safari一起开放。
<!-- Use preprocessors via the lang attribute! e.g. <template lang="pug"> -->
<template>
<div id="app" ref="app">
<p
v-for="message in messages"
>
{{ message }}
</p>
</div>
</template>
<script>
export default {
data() {
return {
messages: []
};
},
mounted() {
setInterval(() => {
this.messages.unshift((new Date()).toString())
this.$nextTick(() => {
this.$refs.app.scrollTo(0, 0)
})
}, 1000)
}
};
</script>
<!-- Use preprocessors via the lang attribute! e.g. <style lang="scss"> -->
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
text-align: center;
color: #2c3e50;
display: flex;
flex-direction: column-reverse;
overflow-y: scroll;
scroll-behavior: smooth;
height: 90vh;
}
a,
button {
color: #4fc08d;
}
button {
background: none;
border: solid 1px;
border-radius: 2em;
font: inherit;
padding: 0.75em 2em;
}
</style>
I'm making a chatroom-like website using vue.
I use display: flex & column-reverse on css to reverse messages view.
And use Array unshift to insert new message.
On chrome & firefox, this will always keep the newest message(at bottom) in view.
But safari don't.
I tried method "scrollTo" after new message append, but it seems not working without user interact.
(New message may append without any user interact in a short time before)
I also tried detect whether bottom or not using "scrollTop" (then I can notice user a new message came in) but failed on safari too.
Any other way to make safari work the same as chrome/firefox do?
A demo here, can open with safari with the problem.
<!-- Use preprocessors via the lang attribute! e.g. <template lang="pug"> -->
<template>
<div id="app" ref="app">
<p
v-for="message in messages"
>
{{ message }}
</p>
</div>
</template>
<script>
export default {
data() {
return {
messages: []
};
},
mounted() {
setInterval(() => {
this.messages.unshift((new Date()).toString())
this.$nextTick(() => {
this.$refs.app.scrollTo(0, 0)
})
}, 1000)
}
};
</script>
<!-- Use preprocessors via the lang attribute! e.g. <style lang="scss"> -->
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
text-align: center;
color: #2c3e50;
display: flex;
flex-direction: column-reverse;
overflow-y: scroll;
scroll-behavior: smooth;
height: 90vh;
}
a,
button {
color: #4fc08d;
}
button {
background: none;
border: solid 1px;
border-radius: 2em;
font: inherit;
padding: 0.75em 2em;
}
</style>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将您的
溢出
从滚动
更改为可见应该解决此问题。它不滚动的原因是您的
#App
容器不会在应用程序框中更改的内容高度,Changing your
overflow
fromscroll
tovisible
should solve this issue.The reason why it's not scrolling is your
#app
container does not pick up the content height changed inside the app box,