vue 增加页面顶部横屏滚动组件

 

组件封装好,直接调用即可。

代码如下:

/*
* @desc: 滚动组件
*/
<template>  
  <div class="note-block" ref="noteBlocking" @mouseover="handStop" @mouseleave="handStart" v-show="noteContent" v-popover:notePopover>
    <div class="noteScrolling-block" ref="noteScrollingBlock" @mouseover="handStop" @mouseleave="handStart">
      {{noteContent}}
    </div>
    <el-popover ref="notePopover" placement="top" title="" trigger="hover" :content="noteContent"></el-popover>
  </div>

</template>

<script>
export default {
  name: "scroll-nav",
  data() {
    return {
      noteContent:'内容内容内容',
    }
  },
  mounted() {
    this.startScroll()
  },
  methods: {
    startScroll() {
      const that = this
      this.adWidth = this.$refs.noteBlocking.clientWidth
      this.con1 = this.$refs.noteScrollingBlock
      this.conWidth = this.$refs.noteScrollingBlock.clientWidth
      this.con1.style.right =`0px`
      this.count = 0
      document.addEventListener("visibilitychange", function() {
          if (document.hidden === true) {
              that.handStop()
          } else {
              that.handStart()
          }
      });
      this.scroll()
    },
    handStop(){
        this.scroll(true)
    },
    handStart(){
        this.scroll(false)
    },
    scroll(flag){
      clearTimeout(this.timeOutObj)
      if(flag) {
        return
      }
      this.timeOutObj = setTimeout(() => {
        this.count += 15
        this.con1.style.right = `${this.count}px`
        if(this.adWidth + this.conWidth+ 20 < this.count){
          this.con1.style.opacity = 0
          this.count = 0
          this.con1.style.right =`-0px`
          setTimeout(() => {
            this.con1.style.opacity = 1
            clearTimeout(this.timeOutObj)
            this.scroll(flag)
          },2000)
      }else{
          this.scroll(flag)
        }
      }, 500)
    }
  }
}
</script>

<style lang="scss" scoped>
  .note-block{
    position: absolute;
    left: 0;
    right: 0;
    overflow: hidden;
    cursor: pointer;
    width: 100%;
    height: 30px;
    background: #FFF0DF;
    font-family: SourceHanSansCN-Regular;
    font-size: 14px;
    color: #DA3438;
    letter-spacing: -0.35px;

    .noteScrolling-block{
      height: 30px;
      line-height: 30px;
      transition: right 1s linear;
      display: inline-block;
      position: absolute;
      right: 0;
      white-space: nowrap;
      transform: translate(100%,0);
      cursor: pointer;
    }
  }
</style>

 

样式如下:

 

 

 

 

 

 

 

 

 

posted @ 2021-12-15 15:28  Fourteen  阅读(348)  评论(0编辑  收藏  举报