【前端常用功能效果】无缝滚动的公告栏

一:Notice.vue无缝滚动公告栏组件
<template>
  <div class="ScrollObjWrap" ref="ScrollObjMainParent">
    <div class="ScrollObjMain" ref="ScrollObjMain">
      <div class="ScrollObjMains" ref="ScrollObjMains">
        <a href="javascript:void(0)" v-for="item in ScrollObj">{{item}}</a>
        <a v-if="boors" href="javascript:void(0)" v-for="item in ScrollObj">{{item}}</a>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  props: ["ScrollObj"],
  data(){
    return {
      TimeAll:[],
      boors:false
    }
  },
  created() {
    this.$nextTick(()=>{
      this.animateScrollFun();
    })
  },
  methods:{
    animateScrollFun(){
      if(this.TimeAll && this.TimeAll.length>0){
        this.TimeAll.forEach(item=>{
          clearInterval(item)
        })
      }
      let ScrollObjMainParent = this.$refs.ScrollObjMainParent,
          ScrollObjMains = this.$refs.ScrollObjMains,
          ScrollObjMain = this.$refs.ScrollObjMain;
      let Num = 0;
      let MaxW = ScrollObjMainParent.clientWidth,
          Width = ScrollObjMains.clientWidth+2;
      let Time = null;
      let Animate = ()=>{
        if(Width>MaxW){
          ScrollObjMain.style.width = Width*2+"px";
          this.boors = true;
          Time = setInterval(()=>{
            Num++;
            if(Width-2 <= Num){
              Num = 0;
            }
            ScrollObjMain.style.marginLeft = -Num+"px";
          },60)
          this.TimeAll.push(Time)
        }
      }
      let Time1 = setTimeout(()=>{
        Animate();
        clearTimeout(Time1)
      },2000)
      ScrollObjMainParent.addEventListener("mouseenter",()=>{
        clearInterval(Time);
        clearTimeout(Time1)
      })
      ScrollObjMainParent.addEventListener("mouseleave",()=>{
        Animate();
      })
    }
  },
  beforeDestroy(){
    if(this.TimeAll && this.TimeAll.length>0){
      this.TimeAll.forEach(item=>{
        clearInterval(item)
      })
    }
  }
};
</script>
<style scoped>
.ScrollObjWrap {
  overflow:hidden;
}
.ScrollObjMain {
  width:1000%;
  overflow:hidden;
}
.ScrollObjMains {
  float:left;
  overflow:hidden;
}
.ScrollObjMain a {
  float:left;
}
.zytzWrap dl dd a {
  color: #203f78;
  font-size: 12px;
  margin-right:20px;
}
.zytzWrap dl dd a:hover {
  text-decoration:underline;
}
@media screen and (min-width: 1440px){
  .zytzWrap dl dd a {
    font-size: 14px;
  }
}
</style>

二:调用组件

<NoticeScroll :ScrollObj="ScrollObj"  />
<script>
import NoticeScroll from "../components/Notice";
export default {
  components: {
    NoticeScroll
  },
  data() {
    return {
      ScrollObj:["1此处添加通知公告内容,并且内容超出显示区域进行左右滚动!","2此处添加通知公告内容此处添加通知公告内容"]
    }
  }
}
</script>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值