vue跑马灯

vue跑马灯

使用elementui的布局组件实现列宽度

vue 复制代码
<template>
  <div class="lamp-container">
    <div class="lamp-header">
      <el-row :gutter="10">
        <el-col v-for="col in column" :key="col.prop" :span="col.span || 6">
          <slot :name="col.headSlot" :col="col">{{ col.label }}</slot>
        </el-col>
      </el-row>
    </div>
    <div
      class="lamp-wrapper"
      :style="{ height: height + 'px' }"
      ref="wrapper"
      @mouseover="stopCount"
      @mouseout="count"
    >
      <div class="lamp-list" ref="list">
        <div class="lamp-slide" v-for="item in data">
          <el-row :gutter="10">
            <el-col v-for="col in column" :key="col.prop" :span="col.span || 6">
              <slot :name="col.slot" :row="item">{{ item[col.prop] }}</slot>
            </el-col>
          </el-row>
        </div>
      </div>
      <div class="lamp-list" v-if="showClone">
        <div class="lamp-slide" v-for="item in data">
          <el-row :gutter="10">
            <el-col v-for="col in column" :key="col.prop" :span="col.span || 6">
              <slot :name="col.slot" :row="item">{{ item[col.prop] }}</slot>
            </el-col>
          </el-row>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
/**
 * @file 跑马灯组件 my-lamp
 * @author 孔
 * @date 2023-12-25
 * @param column {() => ({
 *  label: '名称',  列标题
 *  prop: 'name',   data 中的属性
 *  span: 6, 列宽度
 *  slot: 'name', 数据插槽
 *  headSlot:'name', 表头插槽}[])} 列配置
 * @param data {Array} 数据
 * @param height {Number} 高度
 * @param speed {Number}    速度
 * @slot headSlot {String} 表头插槽
 * @slot slot {String} 数据插槽
 *
 * column item配置   {
 * }
 */
export default {
  name: "MyLamp",
  props: {
    column: Array,
    data: Array,
    height: {
      type: [Number, String],
      default: 200,
    },
    speed: {
      type: Number,
      default: 1,
    },
  },
  data() {
    return {
      showClone: false,
      timer: null,
    };
  },
  watch: {
    data: {
      handler() {
        this.init();
      },
      deep: true,
      immediate: true,
    },
  },
  beforeDestroy() {
    if (this.timer) clearInterval(this.timer);
  },
  methods: {
    init() {
      this.$nextTick(() => {
        if (this.$refs.list.offsetHeight > this.height) {
          this.showClone = true;
          this.count();
        } else {
          this.showClone = false;
          this.stopCount();
        }
      });
    },
    stopCount() {
      if (this.timer) clearInterval(this.timer);
    },
    count() {
      if (!this.showClone) return;
      if (this.timer) clearInterval(this.timer);
      this.timer = setInterval(() => {
        this.Marquee();
      }, 50);
    },
    Marquee() {
      if (this.$refs.wrapper.scrollTop >= this.$refs.list.offsetHeight) {
        this.$refs.wrapper.scrollTop = 0;
      } else {
        this.$refs.wrapper.scrollTop += Number(this.speed);
      }
    },
  },
};
</script>

<style scoped>
.lamp-header {
  height: 40px;
  line-height: 40px;
  text-align: center;
  font-size: 14px;
}
.lamp-wrapper {
  overflow: hidden;
  position: relative;
}
.lamp-wrapper .lamp-slide {
  height: 40px;
  line-height: 40px;
  text-align: center;
}
.lamp-wrapper .lamp-slide:nth-child(even) {
  background: #08247e;
}
</style>

组件props

属性 类型 说明 默认值
column colOption[] 列配置 -
data Array 显示得数据 -
height Number|String 滚动区域高度 200
speed Number 滚动速度 1

colOption

属性 类型 说明 默认值
label String 列名称 -
prop String data中得属性 -
span Number 列宽度 6
headSlot String 列头插槽名称 -
slot String 列插槽名称 -
相关推荐
懋学的前端攻城狮24 分钟前
Vue源码解析-01:从创建到挂载的完整流程
前端·vue.js·源码
程序员小张丶1 小时前
React Native在HarmonyOS 5.0阅读类应用开发中的实践
javascript·react native·react.js·阅读·harmonyos5.0
EndingCoder1 小时前
React Native 是什么?为什么学它?
javascript·react native·react.js
摸鱼仙人~1 小时前
Redux Toolkit 快速入门指南:createSlice、configureStore、useSelector、useDispatch 全面解析
开发语言·javascript·ecmascript
程序员小张丶2 小时前
基于React Native开发HarmonyOS 5.0主题应用技术方案
javascript·react native·react.js·主题·harmonyos5.0
teeeeeeemo2 小时前
Vue数据响应式原理解析
前端·javascript·vue.js·笔记·前端框架·vue
Sahas10192 小时前
__VUE_PROD_HYDRATION_MISMATCH_DETAILS__ is not explicitly defined.
前端·javascript·vue.js
Jinxiansen02112 小时前
Vue 3 实战:【加强版】公司通知推送(WebSocket + token 校验 + 心跳机制)
前端·javascript·vue.js·websocket·typescript
JohnYan3 小时前
Bun技术评估 - 05 SQL
javascript·后端·bun
前端农民晨曦3 小时前
深入浏览器事件循环与任务队列架构
前端·javascript·面试