VUE篇之推送+瀑布流

html 复制代码
<template>
  <div class="root">
    <div class="container" ref="container" @scroll="onScroll">
      <!-- 瀑布流列表 -->
      <transition-group name="list" tag="div" class="list">
        <div
          v-for="(item, index) in items"
          :key="item._uid"
          class="list-item"
        >
          {{ item.text }}
        </div>
      </transition-group>

      <!-- 加载状态 -->
      <div class="loading" v-if="loading">加载中...</div>
      <div class="loading" v-else-if="allLoaded">没有更多数据了</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [],
      page: 1,
      pageSize: 10,
      allLoaded: false,
      loading: false,
      pushTimer: null, // 定时器
    };
  },
  created() {
    this.loadMore();

    // 定时推送消息,每 3 秒一次
    this.pushTimer = setInterval(() => {
      this.pushMessage();
    }, 3000);
  },
  beforeDestroy() {
    // 清理定时器
    if (this.pushTimer) {
      clearInterval(this.pushTimer);
    }
  },
  methods: {
    loadMore() {
      if (this.loading || this.allLoaded) return;
      this.loading = true;

      setTimeout(() => {
        const newItems = Array.from({ length: this.pageSize }).map((_, i) => ({
          _uid: Date.now() + Math.random() + i,
          text: `列表数据 第${(this.page - 1) * this.pageSize + i + 1}条`,
        }));

        this.items = [...this.items, ...newItems];
        this.page++;
        this.loading = false;

        if (this.page > 5) this.allLoaded = true;
      }, 500);
    },

    pushMessage() {
      const message = {
        _uid: Date.now() + Math.random(),
        text: `【推送消息】 ${new Date().toLocaleTimeString()}`,
      };
      // 插入到最前面
      this.items = [message, ...this.items];
    },

    onScroll() {
      const container = this.$refs.container;
      if (!container) return;

      const scrollBottom =
        container.scrollHeight - container.scrollTop - container.clientHeight;
      if (scrollBottom < 100) {
        this.loadMore();
      }
    },
  },
};
</script>

<style scoped>
.root {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.container {
  width: 400px;
  height: 500px;
  overflow-y: auto;
  border: 1px solid #ccc;
  padding: 10px;
  box-sizing: border-box;
  margin-bottom: 10px;
}

.list {
  display: flex;
  flex-direction: column;
}

.list-item {
  background: #f0f0f0;
  margin-bottom: 10px;
  padding: 15px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

/* transition-group 动画 */
.list-enter-active {
  animation: fadeInDown 0.4s;
}
.list-leave-active {
  animation: fadeOutUp 0.4s;
}

@keyframes fadeInDown {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeOutUp {
  from {
    opacity: 1;
    transform: translateY(0);
  }
  to {
    opacity: 0;
    transform: translateY(-20px);
  }
}

.loading {
  text-align: center;
  padding: 10px;
  color: #999;
}
</style>
相关推荐
C+++Python1 小时前
如何使用CSS Grid?
css
Henry_Lau6174 小时前
主流IDE常用快捷键对照
前端·css·ide
C+++Python4 小时前
CSS Grid和Flexbox有什么区别?
css
JIngJaneIL4 小时前
基于java + vue校园快递物流管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js
OpenTiny社区6 小时前
2025OpenTiny星光ShowTime!年度贡献者征集启动!
前端·vue.js·低代码
狗哥哥6 小时前
从零到一:打造企业级 Vue 3 高性能表格组件的设计哲学与实践
前端·vue.js·架构
计算机毕设VX:Fegn08956 小时前
计算机毕业设计|基于springboot + vue图书借阅管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
褪色的笔记簿7 小时前
在 Vue 项目里管理弹窗组件:用 ref 还是用 props?
前端·vue.js
一只小阿乐7 小时前
前端vue3 web端中实现拖拽功能实现列表排序
前端·vue.js·elementui·vue3·前端拖拽
AAA阿giao7 小时前
从“操纵绳子“到“指挥木偶“:Vue3 Composition API 如何彻底改变前端开发范式
开发语言·前端·javascript·vue.js·前端框架·vue3·compositionapi