点击左右滚动

<template>
  <div class="container">
    <div class="icon-left common" @click="preOrNextClick(false)">
      <LeftOutlined />
    </div>
    <div class="scroll">
      <div class="tabs" ref="tabsRef">
        <template v-for="item in agentSidebarActions" :key="item">
          <div class="item" @click="handlerItemClick(item)">
            {{ item.title }}
          </div>
        </template>
      </div>
    </div>

    <div class="icon-right common" @click="preOrNextClick(true)">
      <RightOutlined />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, inject } from 'vue';
import { LeftOutlined, RightOutlined } from '@ant-design/icons-vue';
import Chat from '/@/views/chat/framework/chat';
import { AgentSidebarActionsType } from '/@/views/chat/types/types';

const chat: Chat = inject<Chat>('chat') as Chat;
const agentSidebarActions: AgentSidebarActionsType[] = chat.config.agentSidebarActions;

const tabsRef = ref();

let stop = ref();
let posIndex = ref(0);
let totalWidth = ref();
let clientWidth = ref();
let scrollWidth = ref();
const preOrNextClick = (isRight: Boolean) => {
  if (isRight && scrollWidth.value <= clientWidth.value) return;
  if (isRight && stop.value) return;
  let newIndex = isRight ? posIndex.value + 1 : posIndex.value - 1;
  const ele = tabsRef.value.children[newIndex];
  if (!ele) return;
  const eleOffsetLeft = ele.offsetLeft;
  tabsRef.value.style.transform = `translate(-${eleOffsetLeft}px)`;
  posIndex.value = newIndex;
  if (totalWidth.value < eleOffsetLeft) {
    stop.value = true;
  } else {
    stop.value = false;
  }
};

onMounted(() => {
  clientWidth.value = tabsRef.value.clientWidth;
  scrollWidth.value = tabsRef.value.scrollWidth;
  totalWidth.value = scrollWidth.value - clientWidth.value;
});
const emits = defineEmits(['itemClick']);
const handlerItemClick = (item) => {
  emits('itemClick', item);
};
</script>

<style lang="less" scoped>
.container {
  position: relative;
  width: 100%;
  padding: 0px 30px;
}
.scroll {
  overflow: hidden;
}
.tabs {
  display: flex;
  transition: transform 250ms ease;

  &::-webkit-scrollbar {
    display: none;
  }

  .item {
    padding: 5px 10px;
    margin-right: 10px;
    white-space: nowrap;
    background-color: #fff;
    border: 1px solid #ddd;
    font-size: 12px;
    border-radius: 10px;
    cursor: pointer;
  }
}
.icon-left {
  left: 0px;
  top: 0px;
}
.icon-right {
  right: 0px;
  top: 0px;
}
.common {
  text-align: center;
  width: 30px;
  height: 30px;
  line-height: 30px;
  position: absolute;
  &:hover {
    background-color: #ddd;
    color: #ed8936;
    border-radius: 5px;
  }
}
</style>

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值