vue 里实现文本超出时点击左右按钮滚动?

简介: vue 里实现文本超出时点击左右按钮滚动?

实现

编写 leftRightSwiperScroll.vue 组件。

<template>
<!-- 文本超出,左右按钮滚动 -->
<div ref="swiperScroll" class="left-right-swiper-scroll-box">
  <!-- 左边按钮 -->
  <div v-show="showLeftIcon" @click="handleLeftClick" class="scroll-icon left-icon">
    <i ></i>
  </div>
  <!-- 中间滚动区域 -->
  <div ref="swiperScrollContent" class="swiper-scroll-content">
    <slot></slot>
  </div>
  <!-- 右边按钮 -->
  <div v-show="showRightIcon" @click="handleRightClick" class="scroll-icon right-icon">
    <i ></i>
  </div>
</div>
</template>
<script>
export default {
  name: 'leftRightSwiperScroll',
  props: {
    swiperList: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      showRightIcon: false, // 是否显示右箭头
      swiperScrollWidth: 0, // 盒子的宽度
      swiperScrollContentWidth: 0, // 内容的宽度
      maxClickNum: 0, // 最大点击次数
      lastLeft: 0, // 上次滑动距离
      clickNum: 0, // 点击次数
      childrenList: [] // 传进来的子元素
    }
  },
  computed: {
    showLeftIcon() {
      return this.clickNum > 0
    }
  },
  mounted() {
    // 获取 HTMLCollection 转为 数组
    this.childrenList = [...this.$refs.swiperScrollContent.children];
    console.log('childrenList--->', this.childrenList);            
    console.log('swiperScroll--->', this.$refs.swiperScroll.getBoundingClientRect());
    console.log('swiperScrollContent--->', this.$refs.swiperScrollContent.getBoundingClientRect());
    // 盒子的宽度
    this.swiperScrollWidth = this.$refs.swiperScroll.getBoundingClientRect().width;
    // 内容的宽度
    this.swiperScrollContentWidth = this.$refs.swiperScrollContent.getBoundingClientRect().width;
    // 比较盒子的宽度,跟内容的宽度:判断是否需要展示右边的按钮
    if(this.swiperScrollWidth < this.swiperScrollContentWidth) {
      this.showRightIcon = true;
    }
  },
  methods: {
    // 点击右箭头(左侧滚动)
    handleRightClick() {
      // 如果点击次数小于数组长度-1时,执行左滑动效果。
      if (this.clickNum < this.childrenList.length - 1) {
        // 获取当前元素宽度
        let width = this.childrenList[this.clickNum].getBoundingClientRect().width;
        console.log(this.childrenList[this.clickNum], this.childrenList[this.clickNum].getBoundingClientRect());
        // 获取最后一个元素距离左侧的距离
        let lastItemOffsetLeft = this.childrenList[this.childrenList.length - 1].offsetLeft;
        // 获取最后一个元素宽度
        let lastWidth = this.childrenList[this.childrenList.length - 1].getBoundingClientRect().width;
        console.log('lastItemOffsetLeft-->', lastItemOffsetLeft, this.childrenList[this.childrenList.length - 1].getBoundingClientRect());
        // 获取可视区域宽度
        const lookWidth = this.$refs.swiperScroll.clientWidth;
        console.log('获取可视区域宽度-->lookWidth', lookWidth);
        // 如果最后一个元素距离左侧的距离+自身的宽度大于可视距离,表示最后一个元素没有出现,执行滚动效果
        if (lastItemOffsetLeft + lastWidth > lookWidth) {
          // 滚动距离(元素的magin-left值) = 负的它自己的长度 + 上一次滑动的距离
          this.$refs.swiperScrollContent.style.marginLeft = `${-width + this.lastLeft}px`;
          this.lastLeft = -width + this.lastLeft;
          // 点击次数+1
          this.clickNum++;
          // 记录到最后一个元素出现在可视区域时,点击次数的最大值。用于后面点击左侧箭头时判断右侧箭头的显示
          this.maxClickNum = this.clickNum;
        }
        // 如果最后一个元素距离左侧的距离小于于可视距离,需要隐藏右侧箭头
        let timer = setTimeout(() => {
          // 重新:获取最后一个元素距离左侧的距离
          let lastItemOffsetLeft2 = this.childrenList[this.childrenList.length - 1].offsetLeft;
          console.log('lastItemOffsetLeft2-新的->', lastItemOffsetLeft2);
          if(lastItemOffsetLeft2 + lastWidth <= lookWidth) {
            this.showRightIcon = false;
          }
          clearTimeout(timer);
        }, 200);
      }
    },
    // 点击左箭头(右侧滚动)
    handleLeftClick() {
      // 当点击次数大于0时才触发,这样当点击次数clickNum等于1的到时候,clickNum--等于0.根据计算属性的判断会隐藏掉左箭头
      if (this.clickNum > 0) {
        // 获取当前元素宽度
        let width = this.childrenList[this.clickNum - 1].getBoundingClientRect().width;
        // 公示:滚动距离(元素的magin-left值) = 它自己的长度 + 上一次滑动的距离
        this.$refs.swiperScrollContent.style.marginLeft = `${this.lastLeft + width}px`
        this.lastLeft = width + this.lastLeft;
        // 点击次数-1
        this.clickNum--;
        // 如果点击次数小于最大点击次数,说明最后一个元素已经不在可是区域内了,显示右箭头
        if (this.clickNum < this.maxClickNum) {
          this.showRightIcon = true;
        }
      }
    }
  }
}
</script>
<style lang='scss' scoped>
.left-right-swiper-scroll-box {
  position: relative;
  width: 100%;
  overflow: hidden;
  transition: all 0.3s;
  .scroll-icon {
    position: absolute;
    top: 0;
    width: 70px;
    height: 30px;
    z-index: 9;
    cursor: pointer;
    background-image: linear-gradient(270deg, invalid gradient);
    i {
      position: absolute;
      top: 5px;
      width: 20px;
      height: 20px;
    }
    &.left-icon {
      left: 0;
      background: url("~@/assets/img/img-mask-left.svg") -20px 0 no-repeat;
      i {
        left: 0;
        background: url("~@/assets/img/icon-arrow-left.svg") 0 0 no-repeat;
      }
    }
    &.right-icon {
      right: 0;
      background: url("~@/assets/img/img-mask-right.svg") 0 0 no-repeat;
      i {
        right: 0;
        background: url("~@/assets/img/icon-arrow-right.svg") 0 0 no-repeat;
      }
    }
  }   
  .swiper-scroll-content {
    display: inline-block;
    white-space: nowrap;
    transition: all 0.3s;
  }
}
</style>



使用

接口获取完之后就渲染,isLoading 就为 true。

引用 leftRightSwiperScroll.vue 组件,使用就行

<left-right-swiper-scroll v-if="isLoading">
  <span class="list-btn active">测试数据·0000</span>
  <span class="list-btn">测试数据·1111</span>
  <span class="list-btn">测试数据·2222</span>
  <span class="list-btn">测试数据·3333</span>
  <span class="list-btn">测试数据·4444</span>
  <span class="list-btn">测试数据·5555</span>
  <span class="list-btn">测试数据·6666</span>
  <span class="list-btn">测试数据·7777</span>
  <span class="list-btn">测试数据·8888</span>
  <span class="list-btn">测试数据·9999</span>
</left-right-swiper-scroll>


效果如下

a08ae09cf88a45d5ab46806bd3989e03.png

a08ae09cf88a45d5ab46806bd3989e03.png

47ed282c29124a43913b470612ebf853.png




目录
相关文章
|
JavaScript API
vue尚品汇商城项目-day04【24.点击搜索按钮跳转后的页面商品列表、平台售卖属性动态展示(开发Search组件)】
vue尚品汇商城项目-day04【24.点击搜索按钮跳转后的页面商品列表、平台售卖属性动态展示(开发Search组件)】
228 1
vue尚品汇商城项目-day04【24.点击搜索按钮跳转后的页面商品列表、平台售卖属性动态展示(开发Search组件)】
|
JavaScript
Vue实现按钮级别权限
文章介绍了在Vue中实现按钮级别权限的两种方法:使用自定义Vue指令和使用v-if指令配合自定义方法。
191 4
Vue实现按钮级别权限
|
JavaScript
Vue3文本省略(Ellipsis)
这是一个基于Vue3的文本省略组件(Ellipsis),支持单行或多行文本的自动省略与展开功能,并可自定义提示框(Tooltip)的内容与样式。
402 1
Vue3文本省略(Ellipsis)
|
JavaScript
Vue3按钮(Button)
这是一个高度可定制的按钮组件,支持多种属性设置,包括按钮类型、形状、图标、尺寸、背景透明度、波纹颜色、跳转地址、打开方式、禁用状态、加载状态及指示符样式等。预览图展示了不同配置下的按钮样式变化。组件通过Vue实现,并提供了丰富的自定义选项以适应各种场景需求。
887 1
Vue3按钮(Button)
|
JavaScript
Vue3文字滚动(TextScroll)
这是一个可定制的文字滚动组件,支持水平和垂直滚动。主要属性包括滚动文字数组 `scrollText`、是否启用单条文字滚动 `single`、滚动区域宽高 `width` 和 `height`、滚动区域和文字样式 `boardStyle` 和 `textStyle`、滚动条数 `amount`、间距 `gap`、动画间隔 `interval` 和 `step`、以及垂直滚动时间间隔 `verticalInterval`。组件内置多种样式调整功能,并提供在线预览示例。
572 0
Vue3文字滚动(TextScroll)
|
JavaScript
如何通过点击商品的信息(图片或者文字)跳转到更加详细的商品信息介绍(前后端分离之Vue实现)
该博客文章介绍了如何在Vue 2框架下实现前后端分离的商品信息展示和详情页跳转,包括排序筛选、详情展示、加入购物车和分享功能。
如何通过点击商品的信息(图片或者文字)跳转到更加详细的商品信息介绍(前后端分离之Vue实现)
|
JavaScript
在Vue中使用Swiper轮播图、同时解决点击轮播图左右切换按钮不生效的问题、同时将轮播图抽离出为一个公共组件
这篇文章介绍了在Vue中如何使用Swiper插件创建轮播图,解决Swiper左右切换按钮不生效的问题,并展示了如何将Swiper轮播图抽离成一个可复用的公共组件,同时提供了详细的安装、配置和优化建议。
在Vue中使用Swiper轮播图、同时解决点击轮播图左右切换按钮不生效的问题、同时将轮播图抽离出为一个公共组件
|
JavaScript 测试技术 容器
Vue2+VueRouter2+webpack 构建项目
1). 安装Node环境和npm包管理工具 检测版本 node -v npm -v 图1.png 2). 安装vue-cli(vue脚手架) npm install -g vue-cli --registry=https://registry.
1253 0
|
4月前
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
370 2
|
3月前
|
缓存 JavaScript
vue中的keep-alive问题(2)
vue中的keep-alive问题(2)
339 137