vue跑马灯

vue跑马灯


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

<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 列插槽名称 -

相关推荐

  1. vue跑马

    2023-12-28 11:24:05       51 阅读
  2. WPF —— 跑马

    2023-12-28 11:24:05       29 阅读
  3. 03_led_horse_run_v1 跑马

    2023-12-28 11:24:05       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-28 11:24:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-28 11:24:05       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-28 11:24:05       82 阅读
  4. Python语言-面向对象

    2023-12-28 11:24:05       91 阅读

热门阅读

  1. 计算机基础面试题总结

    2023-12-28 11:24:05       43 阅读
  2. SpringBoot实用开发(四)-- RedisTemplate 常用API

    2023-12-28 11:24:05       53 阅读
  3. C++期末复习总结继承

    2023-12-28 11:24:05       56 阅读
  4. Optimization(优化)

    2023-12-28 11:24:05       55 阅读
  5. Git常用命令大全

    2023-12-28 11:24:05       64 阅读
  6. 前端八股文(HTML篇)

    2023-12-28 11:24:05       43 阅读
  7. Redis自动部署脚本编写

    2023-12-28 11:24:05       54 阅读