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>
相关推荐
踩着两条虫2 小时前
AI 驱动的 Vue3 应用开发平台 深入探究(二十):CLI与工具链之构建配置与Vite集成
前端·vue.js·ai编程
踩着两条虫2 小时前
AI 驱动的 Vue3 应用开发平台 深入探究(二十):CLI与工具链之自定义构建插件
前端·vue.js·ai编程
极梦网络无忧3 小时前
基于 Vite + Vue3 的组件自动注册功能
前端·javascript·vue.js
雪碧聊技术4 小时前
前端vue代码架子搭建
前端·javascript·vue.js·前端项目代码框架搭建
极客小云5 小时前
【Electron-Vue 企业级安全启动模板:electron-vue-theme-template 使用指南】
vue.js·安全·electron
计算机学姐5 小时前
基于SpringBoot的校园二手书籍交易系统【个性化推荐+数据可视化统计+我买到的+我卖出的】
vue.js·spring boot·后端·mysql·信息可视化·intellij-idea·mybatis
SuperEugene5 小时前
Vue3 + Element Plus 表单开发实战:防重复提交、校验、重置、loading 统一|表单与表格规范篇
前端·javascript·vue.js
SuperEugene5 小时前
Vue3 + Element Plus 中后台弹窗规范:开闭、传参、回调,告别弹窗地狱|Vue 组件与模板规范篇
开发语言·前端·javascript·vue.js·前端框架
孟祥_成都5 小时前
前端下午茶:这 3 个网页特效建议收藏(送源码)
前端·javascript·css
SuperEugene5 小时前
VXE-Table 4.x 实战规范:列配置 + 合并单元格 + 虚拟滚动,避坑卡顿 / 错乱 / 合并失效|表单与表格规范篇
开发语言·前端·javascript·vue.js·前端框架·vxetable