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 分钟前
Vue过渡动画从入门到装X:淡入淡出、滑动、列表动画、第三方库全搞定
前端·javascript·vue.js
IManiy39 分钟前
总结之Vibe Coding前端骨架
前端
小和尚敲木头43 分钟前
vue3 vite动态拼接图片路径
javascript
JS菌1 小时前
AI Agent 沙箱双层防护体系:从权限过滤到内核隔离的完整实现
前端·人工智能·后端
Aphasia3111 小时前
从输入URL到页面展示全流程
前端·面试
我叫黑大帅1 小时前
前端如何竖屏固定视口背景
前端·javascript·面试
abcy0712131 小时前
python pandas csv异步后台清洗前端优先返回成功信息
前端·python·pandas
不会敲代码12 小时前
我花了三天时间,终于把 Cookie、XSS、CSRF 和浏览器存储给整明白了
javascript·面试
IT_陈寒2 小时前
Vite这个坑我帮你踩了,动态导入居然这样才生效
前端·人工智能·后端
贩卖黄昏的熊2 小时前
flex 布局快速梳理
开发语言·javascript·css3·html5