Vue组件-广告滚动

1.广告组件

<!-- 
 * 滚动广告
 *@parmas msg 广告内容
 *@parmas height 容器高度
-->
<template>
  <div class="inner" :style="{height:height}">
    <ul 
      :class="{marquee_top:animate}" 
      :style="{marginTop:(animate?`-${height}`: 0)}" 
      @mouseover='stop' 
      @mouseleave='start'
    >
      <li v-for="(item, index) in msg" :key="index" class="txtWrap">
          <a-tooltip>
            <template slot="title">
              {{item}}
            </template>
            <span class="txt">{{item}}</span>
          </a-tooltip>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'Advertising',
  props:{
      msg:{
        type:Array,
        default:() => []
      },
      height:{
        type:String,
        default:'16px'
      }
  },
  data () {
    return {
      animate: false,
      setInTime:'' // 定时器
    }
  },
  components:{
  },
  watch:{
  },
  computed: {
  },
  methods: {
    // 滚动栏滚动
    showMarquee () {
      this.animate = true
      setTimeout(() => {
        this.msg.push(this.msg[0])
        this.msg.shift()
        this.animate = false
      }, 500)
    },
    stop(){
      clearInterval(this.setInTime)
    },
    start(){
      this.setInTime = setInterval(this.showMarquee, 3000)
    }
  },
  mounted () {
    this.setInTime = setInterval(this.showMarquee, 3000)
  },
  destroyed () {
    clearInterval(this.setInTime) // 页面销毁时清除定时器
  },
  
}
</script>

<style lang="less" scoped>
ul,li{ padding:0;margin:0;list-style:none}
.inner{
    width: 100%;
    overflow: hidden;
    li {
      width: 90%;
      font-size: 12px;
      font-weight: 100;
      overflow: hidden;
      text-overflow:ellipsis; 
      white-space: nowrap;
    }
}
.marquee_top {
  transition: all 0.5s;
  margin-top: -16px; /* 容器高度 */
}
</style>

2.父组件使用

  template
<advertising :msg='msg' height='16px'/>

  js

<script>
import Advertising from '../components/advertising/Advertising.vue'
export default {
  name: 'home',
  data(){
    return{
      msg: ['3车间1号隧道炉电机运转异常!','3车间2号隧道炉电机运转异常!'],
    }
  },
  components:{
    'advertising':Advertising
  },
  computed:{
  },
  mounted () {
  },
  methods: {
  },
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值