VUE实现公告上下滚动组件封装

废话不多说直接上代码!!!

<template>
  <div class="noticebar" v-if="announces.length">
    <div class="notice-img">
      <img class="notice-img" src="../../static/images/ic_notice.png" alt="" />
    </div>
    <ul class="notice-list">
      <li 
        v-for="(item, index) in announces" 
        :key="item.title" 
        :class="!index && play ? 'toUp' : ''" 
        @click="onNotice(item.resUrl)"
      >
        {{ item.title }}
      </li>
    </ul>
    <div class="img-right">
      <img class="img-right"                     src="../../static/images/ic_wish_fill_right.png" alt="" />
    </div>
  </div>
</template>

<script>
export default {
  name: "noticeBar",
  props: {
    announces: {
      type: Array,
      default() {
        return [
          {
            resUrl: "https://www.baidu.com/",
            title: "百度",
          },
        ];
      },
    },
  },
  data() {
    return {
      play: false, // 是否开始播放
    };
  },
  activated() {
    setInterval(this.startPlay, 4000);
  },
  methods: {
    // 控制文案轮播
    startPlay() {
      let that = this;
      that.play = true; //开始播放
      setTimeout(() => {
        that.announces.push(that.announces[0]); //将第一条数据塞到最后一个
        that.announces.shift(); //删除第一条数据
        that.play = false; //暂停播放
      }, 500);
    },


    // 点击"公告"跳转
    onNotice(url) {
      console.log("公告url", url);
      this.$emit("onInfoTo", {
        url,
        title: "资讯详情",
      });
    },
  },
};
</script>

<style lang="less" scoped>
.noticebar {
  width: 100%;
  background: #fff;
  padding: 0.3rem 0.24rem;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 0.3rem;
}
.toUp {
  transform: translateY(-20px);
  transition: all 0.5s linear;
}
.notice-list {
  display: block;
  list-style: none;
  width: 100%;
  overflow: hidden;
  height: 20px;
}
.notice-list li {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.notice-img {
  width: 0.72rem;
  margin-right: 0.24rem;
}
.img-right {
  width: 0.16rem;
}
li {
  height: 20px;
  line-height: 20px;
}
</style>


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值