vue2 div使用css的方式实现自动滚动

vue2 div使用css的方式实现自动滚动

html 复制代码
<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <title></title>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.14/vue.common.dev.min.js" crossorigin="anonymous"></script>

    <style>
      /* ========== 告警列表样式(原有) ========== */
      .alarm-container {
        width: 400px;
        height: 300px;
        background-color: #1e293b;
        border-radius: 8px;
        padding: 16px;
        color: white;
      }
      .alarm-title {
        font-size: 18px;
        margin-bottom: 12px;
        padding-bottom: 8px;
        border-bottom: 1px solid #334155;
      }
      .alarm-scroll-box {
        height: 220px;
        overflow: hidden;
        position: relative;
      }
      .alarm-scroll-content {
        display: flex;
        flex-direction: column;
        animation: alarm-scroll-up 15s linear infinite;
      }
      .alarm-item {
        padding: 8px 12px;
        margin-bottom: 8px;
        background-color: #334155;
        border-radius: 4px;
        font-size: 14px;
        line-height: 1.4;
      }
      .alarm-item .time {
        color: #94a3b8;
        font-size: 12px;
        margin-top: 4px;
      }
      @keyframes alarm-scroll-up {
        0% {
          transform: translate3d(0, 0, 0);
        }
        100% {
          transform: translate3d(0, -50%, 0);
        }
      }
      .alarm-scroll-box:hover .alarm-scroll-content {
        animation-play-state: paused;
        cursor: pointer;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <!-- 原有:告警列表滚动 -->
      <div class="alarm-container">
        <div class="alarm-title">告警列表</div>
        <div class="alarm-scroll-box" @mouseenter="pauseListScroll" @mouseleave="resumeListScroll">
          <div class="alarm-scroll-content" ref="scrollContent">
            <div class="alarm-item" v-for="item in alarmList" :key="'origin-' + item.id">
              <div>{{ item.content }}</div>
              <div class="time">{{ item.time }}</div>
            </div>
            <div class="alarm-item" v-for="item in alarmList" :key="'copy-' + item.id">
              <div>{{ item.content }}</div>
              <div class="time">{{ item.time }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <script>
      new Vue({
        el: '#app',
        data() {
          return {
            // 告警列表数据
            alarmList: [
              { id: 1, content: '区域1:检测到陌生人闯入', time: '2026-03-17 10:01:23' },
              { id: 2, content: '区域2:检测到车辆违停', time: '2026-03-17 10:02:45' },
              { id: 3, content: '区域3:检测到人员聚集', time: '2026-03-17 10:03:12' },
              { id: 4, content: '区域4:检测到消防通道堵塞', time: '2026-03-17 10:04:56' },
              { id: 5, content: '区域5:检测到翻越围栏', time: '2026-03-17 10:05:34' },
            ],
          };
        },
        mounted() {
          // 初始化告警列表滚动时长
          this.setScrollDuration();
        },
        beforeDestroy() {},
        methods: {
          // ========== 告警列表滚动相关方法(原有) ==========
          setScrollDuration() {
            const scrollContent = this.$refs.scrollContent;
            if (!scrollContent) return;
            const duration = Math.max(10, this.alarmList.length * 1.5);
            scrollContent.style.animationDuration = `${duration}s`;
          },
          pauseListScroll() {
            const scrollContent = this.$refs.scrollContent;
            if (scrollContent) scrollContent.style.animationPlayState = 'paused';
          },
          resumeListScroll() {
            const scrollContent = this.$refs.scrollContent;
            if (scrollContent) scrollContent.style.animationPlayState = 'running';
          },
        },
      });
    </script>
  </body>
</html>
相关推荐
计算机魔术师24 分钟前
NVIDIA 以 129.3 亿美元收购 Hugging Face
前端
平头哥技术团队26 分钟前
Day 01 | 用 HTML 写第一个网页:双击就能在浏览器打开
前端
weixin_422555421 小时前
el-menu子菜单点击就被激活,恢复之前的激活状态
javascript·vue.js·elementui
计算机魔术师1 小时前
两年翻45倍!英伟达靠买买买建成千亿投资帝国
前端
paopaokaka_luck1 小时前
基于springboot3+vue3的精准扶贫管理系统(AI 问答、ECharts 图形化分析)
前端·人工智能·echarts
l1m0_2 小时前
智能电动自行车管理后台实战:AI生成页面与React组件化技巧
前端·react.js·ui·ai·设计
Patrick_Wilson2 小时前
为什么gitlab的MR会默认有一个merge commit
前端·git·gitlab
en.en..2 小时前
Linux mmap 内存映射深度解析:基于帧缓冲 /dev/fb0
java·服务器·前端
Z兽兽2 小时前
行内块元素在垂直方向上对齐问题
css·html·css3
后台模板学习3 小时前
从0到1用Vite构建电商订单系统前端性能优化方案
前端