用Vue保持滚动的滚动

发布于 2025-02-05 11:26:45 字数 1574 浏览 4 评论 0原文

我正在使用VUE制作一个类似聊天室的网站。

我使用显示:Flex& CSS上的列逆转到反向消息查看。
并使用Array Unshift插入新消息。

在Chrome& Firefox,这将始终保持最新消息(底部)。
但是野生动物园不会。

在附加新消息后,我尝试了方法“ scrollto”,但是如果没有用户交互,它似乎无法正常工作。
(在很短的时间之前,可能没有任何用户交互的新消息可以附加)

我还尝试检测底部是否使用“ scrolltop”(然后我会注意到用户一条新消息会出现),但在Safari上也失败了。

有其他方法使Safari的工作与Chrome/Firefox一样吗?

这里的演示可以与Safari一起开放。

codepen

<!-- 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.

CodePen

<!-- 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

全部不再 2025-02-12 11:26:45

将您的溢出滚动更改为可见应该解决此问题。

#app {
  overflow: visible;
}

它不滚动的原因是您的#App容器不会在应用程序框中更改的内容高度,

Changing your overflow from scroll to visible should solve this issue.

#app {
  overflow: visible;
}

The reason why it's not scrolling is your #app container does not pick up the content height changed inside the app box,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文