vue 消息左右滚动(前后无缝衔接)

之前一直用vue-seamless-scroll,无奈此组件有两个缺点不满足实际效果:1)标题过长被截取、2)标题果断也会滚动,无奈我自己封装一个,满足此两个弊端,也能达到vue-seamless-scroll组件的功能,如果你想要上下滚动,代码自己更换下translate(${this.left}px, 0px)改成Y轴,样式在更替下即可。

演示效果

封装的组件

<!--
 * @Author:
 * @Date: 2024-03-21 19:21:58
 * @LastEditTime: 2024-03-21 20:31:50
 * @LastEditors: Please set LastEditors
 * @Description: 消息左右滚动
-->
<template>
  <div
    id="textScroll"
    class="text-scroll"
    @mousemove="inBox"
    @mouseleave="leaveBox"
  >
    <div
      v-for="i in 2"
      :id="'scrollItem' + i"
      :key="'scrollItem' + i"
      class="scroll-item"
      :style="{ display: i === 1 ? 'flex' : 'none' }"
    >
      <slot></slot>
    </div>
  </div>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      left: 0,
      textScrollDiv: null,
      timer: null,
      scrollItemWidth: 0,
      isScroll: false
    };
  },

  computed: {},
  destroyed() {
    clearInterval(this.timer);
  },
  mounted() {
    const that = this;
    this.$nextTick(() => {
      that.textScrollDiv = document.querySelector('#textScroll');
      that.scrollItemWidth = document.querySelector('#scrollItem1').clientWidth;
      const outerBoxWidth = that.textScrollDiv.clientWidth;
      if (outerBoxWidth < that.scrollItemWidth) {
        this.isScroll = true;
        that.textScrollDiv.style.width = `${that.scrollItemWidth * 2}px`;
        that.timer = setInterval(function() {
          that.moveLeft();
        }, 30);
        document.querySelector('#scrollItem2').style.display = 'flex';
      }
    });
  },
  methods: {
    moveLeft() {
      if (this.textScrollDiv) {
        this.left -= 1;
        if (Math.abs(this.left) > this.scrollItemWidth) {
          this.left = 0;
        }
        this.textScrollDiv.style.transform = `translate(${this.left}px, 0px)`;
      }
    },
    // 鼠标划入区域
    inBox() {
      if (this.isScroll) {
        clearInterval(this.timer);
        this.timer = null;
      }
    },
    // 鼠标离开区域
    leaveBox() {
      if (this.isScroll) {
        const that = this;
        this.timer = setInterval(function() {
          that.moveLeft();
        }, 30);
      }
    }
  }
};
</script>
<style lang="less" scoped>
.text-scroll {
  display: flex;
  flex-wrap: nowrap;
  transition: all 0ms ease-in 0s;
  .scroll-item {
    display: flex;
    flex-wrap: nowrap;
  }
}
</style>

外部引用

<template>
        <!-- 公告信息板块 -->
    <div v-if="noticeInfo && noticeInfo.length > 0" class="notice-plate">
      <div class="plate-body">
        <div class="plate-icon">
          <i class="sxqyjj-iconfont sxqyjj-xiaoxi1"></i>
        </div>
        <div class="plate-info" @click="handleInfo($event)">
          <textScroll>
            <div
              v-for="(item, i) in noticeInfo"
              :key="'notice' + i"
              class="info-item"
              :data-my-id="item.id"
            >
              {{ item.title }}
              <div v-if="i < noticeInfo.length - 1" class="line-split"></div>
            </div>
          </textScroll>
        </div>
        <div class="plate-more" @click="moreInfo">
          更多
          <i class="sxqyjj-iconfont sxqyjj-chevron-right"></i>
        </div>
      </div>
    </div>
</template>
<script>
import textScroll from '@packages/views/components/text-scroll/index.vue';

export default {
  name: 'Index',
  components: {
    textScroll
  },
  data() {
    return {
      noticeInfo: [],
    };
  },
  created() {
    this.getLastThreeConsultation();
  },
  methods: {
    // 获取重点资讯
    getLastThreeConsultation() {
      this.$api['search/getLastThreeConsultation']()
        .then(res => {
          if (res.code === this.$constant.apiServeCode.SUCCESS_CODE) {
            this.noticeInfo = res.data || [];
          }
          return true;
        })
        .catch(err => {
          console.log(err);
         });
        }
    },
    // 查看资讯详情
    handleInfo(e) {
      const id = e.srcElement.dataset.myId;
      this.$router.push({name:'master-info-detail',params:{id});
    }
  }
};
</script>
<style lang="less" scoped>
/* stylelint-disable */

.notice-plate {
  width: 1328px;
  margin: 0 auto;
  margin-top: 24px;
  .plate-body {
    display: flex;
    align-items: flex-start;
    width: 100%;
    height: 48px;
    padding: 10px 16px;
    margin-left: -64px;
    background: white;
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 4px 4px 4px 4px;
    .plate-icon {
      width: 28px;
      height: 28px;
      margin-right: 16px;
      line-height: 28px;
      color: rgba(255, 143, 31, 1);
      text-align: center;
      background: rgb(255, 247, 241);
      border-radius: 4px 4px 4px 4px;
    }
    .plate-info {
      width: calc(100% - 112px);
      height: 28px;
      overflow: hidden;
      line-height: 28px;
      .info-item {
        position: relative;
        margin-right: 32px;
        font-weight: 500;
        white-space: nowrap;
        &:hover {
          color: rgba(0, 128, 255, 1);
          cursor: pointer;
        }
      }
      .line-split {
        position: absolute;
        top: 6px;
        right: -16px;
        width: 2px;
        height: 12px;
        border-right: 1px solid rgba(217, 217, 217, 1);
      }
    }
    .plate-more {
      height: 28px;
      margin-left: 16px;
      font-size: 14px;
      line-height: 28px;
      color: @text-2;
      cursor: pointer;
      i {
        margin-left: 4px;
      }
    }
  }
}
</style>

相关推荐

  1. uniapp ios input disabled为true时 无法左右滚动

    2024-03-26 14:52:02       33 阅读
  2. 无缝衔接,完美对接:探索系统对接的最佳实践

    2024-03-26 14:52:02       32 阅读

最近更新

  1. 4085行代码还原2D我的世界(上)

    2024-03-26 14:52:02       0 阅读
  2. 大数据面试题之GreenPlum(1)

    2024-03-26 14:52:02       0 阅读
  3. 量化机器人能否识别市场机会?

    2024-03-26 14:52:02       0 阅读
  4. 探讨SpringMVC的工作原理

    2024-03-26 14:52:02       1 阅读
  5. CSS布局艺术:掌握水平与垂直对齐的秘诀

    2024-03-26 14:52:02       1 阅读
  6. SQL 游标

    2024-03-26 14:52:02       0 阅读
  7. 0706_ARM8

    2024-03-26 14:52:02       0 阅读

热门阅读

  1. Docker in Docker原理与实战

    2024-03-26 14:52:02       20 阅读
  2. 通用人工智能与人类工作的未来

    2024-03-26 14:52:02       23 阅读
  3. 高级 IO

    高级 IO

    2024-03-26 14:52:02      21 阅读
  4. oslo_policy学习小结

    2024-03-26 14:52:02       18 阅读
  5. Qt源码分析:QMetaObject实现原理

    2024-03-26 14:52:02       20 阅读