首页 > 其他分享 >封装用于循环播放展示一组内容的组件

封装用于循环播放展示一组内容的组件

时间:2023-01-30 17:36:05浏览次数:41  
标签:const default type value rem 组件 封装 播放 ref

<template>
  <div v-if="show" class="command-code" :style="{ color: color }">
    <div class="command-code-box" ref="boxRef" :style="{ width: contentWidth }">
    <span
      ref="contentRef"
      class="command-code-content"
      @mouseenter="cursorEnter"
      @mouseleave="cursorLeave"
    >
      {{ content }}
    </span>
  </div>
  </div>
</template>

<script setup>

const props = defineProps({
  show: {
    type: Boolean,
    default: false
  },
  content: {
    type: String,
    default: '内容'
  },
  delay: { // 动画延迟时间
    type: Number,
    default: 1000
  },
  space: { // 每次移动的距离
    type: Number,
    default: 5
  },
  color: {
    type: String,
    default: '#27CBF3'
  }
});

const boxRef = ref(null);
const contentRef = ref(null);
const boxWidth = ref('');
const contentWidth = ref('');
const timer = ref(null); // 定时器

function textCarousel() {
  nextTick(() => {
    let left = 0;
    if (boxRef.value && contentRef.value) {
      boxWidth.value = boxRef.value?.offsetWidth;
      contentWidth.value = contentRef.value?.offsetWidth;
      if (contentWidth.value > boxWidth.value) { // 内容溢出则滚动展示
        cursorEnter();
        timer.value = setInterval(() => {
          // 一次向左移动的距离
          left -= props.space;
          // 当左移超过这个宽度时,重新设置left
          if (left <= -contentWidth.value) {
            left = 0;
          }
          contentRef.value.style.left = left + "px";
        }, props.delay);
      }
    }

});
}
// 鼠标进入,停止定时器
function cursorEnter() {
  timer.value && clearInterval(timer.value);
}
// 鼠标离开,继续定时器
function cursorLeave() {
  textCarousel();
}

watch(() => props.show, () => {
  if (props.show) {
    textCarousel();
  } else {
    cursorLeave();
  }
});

</script>
<style lang="scss" scoped>
.command-code {
  position: absolute;
  top: 0.4rem;
  left: 6.7rem;
  width: 2.8rem;
  height: 0.54rem;
  padding: 0 0.2rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.16rem;
  font-weight: 600;
  color: #27CBF3;
  background: url(@/assets/map_images/code-bg.png) no-repeat 50%;
  background-size: 100% 100%;
}
.command-code-box {
  width: 100%;
  overflow: hidden;
  white-space: nowrap;
  text-align: center;
  .command-code-content {
    position: relative;
  }
}
</style>

 

标签:const,default,type,value,rem,组件,封装,播放,ref
From: https://www.cnblogs.com/zhaoxiaoying/p/17076756.html

相关文章

  • 将自己写的组件放入npm仓库使用
    前言有时候我们经常封装一些通用的组件在项目当中,而这些组件可能会在多个项目中使用,以往的做法是直接粘贴复制过来,但其实放入npm仓库会更方便,这里介绍的是公开的npm包......
  • 网页红警,如何将存入本地的音频转成可播放状态
    之前资源加载,我实现了将资源以ArrayBuffer的形式保存到的本地Indexeddb中,里面包含很多的音频文件,那么,如何将音频文件拿出以后,直接播放呢?有两个方法可以实现:URL.createObject......
  • jQuery_1_封装原理
    jQuery是使用JavaScript 编写的函数库,用于简化了JavaScript编程。 问题:在一个页面中,无论是引入外部的js代码,还是内部分开写的js代码块,在此页面解析执行的时候,......
  • tcp/ip详解之02 tcp/ip封装
    1.tcp/ip封装 当应用程序用TCP传送数据时,数据被送入协议栈中,然后逐个通过每一层直到被当作一串比特流送入网络。其中每一层对收到的数据都要增加一些首部信息(有时还要......
  • 搭建直播平台,iYiuMessage 消息提示组件
    搭建直播平台,iYiuMessage消息提示组件引入和创建iYiuMessage实例 <scriptsrc="./js/iYiu.js"type="text/javascript"></script><scripttype="text/javascript"> ......
  • 开源组件ExcelReport 3.x.x 使用手册
     可用   https://www.cnblogs.com/hanzhaoxin/p/10505872.html github https://github.com/hanzhaoxin/ExcelReport......
  • 《安富莱嵌入式周报》第301期:ThreadX老大离开微软推出PX5 RTOS第5代系统,支持回流焊的
    往期周报汇总地址:http://www.armbbs.cn/forum.php?mod=forumdisplay&fid=12&filter=typeid&typeid=104祝大家开工大吉视频版:https://www.bilibili.com/video/BV1GT411o......
  • Bootstrap_组件_导航条&分页条、插件_轮播图
    Bootstrap_组件_导航条&分页条导航条<!doctypehtml><htmllang="zh-CN"><head><metacharset="utf-8"><metahttp-equiv="X-UA-Compatible"content="IE=e......
  • ExtJS-组件查询
    更新记录点击查看>2023年1月6日更新Ext.getCmp>2022年12月3日开始。ExtJS教程汇总:https://www.cnblogs.com/cqpanda/p/16328016.htmlFindingcomponentsbase......
  • react组件实例属性props
    propsprops简单使用classPersonextendsReact.Component{render(){return(<ul>......