uniapp vue3 实现可滑动切换tabs标题栏

<template>
  <view class="tab-bar">
    <scroll-view class="scroll-view_H" scroll-x="true">
      <view class="tab-bar-item" v-for="(tab, index) in tabs" :key="index" @tap="onTabTap(index)">
        <image
          :src="index == tabIndex ? '图片1' : '图片2'"
          mode="aspectFit" class="tab-item-icon" />
        <text class="tab-bar-item-text" :style="{ color: index == tabIndex ? '#FBFDFC' : '#BACDDE' }">
          {{ tab.name }} </text>
      </view>
    </scroll-view>
  </view>
</template>

<script lang="ts" setup>
import { ref, onMounted, type PropType } from 'vue'

const tabIndex = ref(0)

const props = defineProps({
  tabs: {
    type: Array,
    default() {
      return [{
        type: 1 ,
        name: '标题1'
      },
      {
        type: 2
        name: '标题2'
      },
      {
        type: 3,
        name: '标题3'
      }]
    }
  },
})


const onTabTap = (index: number) => {
  tabIndex.value = index
  emit('onTabTap', tabIndex.value, props.tabs[tabIndex.value].type)
}

const emit = defineEmits(['onTabTap'])
</script>

<style scoped lang="scss">
.tab-bar {
  margin: 10rpx;
  padding: 12rpx 0;
  background: #fff;
  border-radius: 24rpx;
  display: flex;
  align-items: center;
  justify-content: center;

  .tab-bar-item {
    width: 236rpx;
    height: 56rpx;
    display: inline-block;
    position: relative;

    .tab-item-icon {
      width: 236rpx;
      height: 56rpx;
    }

    .tab-bar-item-text {
      width: 236rpx;
      font-weight: 600;
      position: absolute;
      left: 0;
      top: 10%;
      margin-left: 0 auto;
      text-align: center;


    }
  }
}

.scroll-view_H {
  white-space: nowrap;
  width: 100%;
}
</style>

上面是子组件 

<TopTabBar  @onTabTap="onTabTap"  :tabs="tabs"></TopTabBar>

<script setup lang="ts">
const rewardBoxType = ref()

let  tabs= ref( [{
        type: 1,
        name: '标题1'
      },
      {
        type: 2,
        name: '标题2'
      },
      {
        type: 3,
        name: '标题3'
      }])


const onTabTap = (index: number, type: any) => {
 //点击之后要做的事件
  rewardBoxType.value = type

}    


</script>

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现下拉列表二级标题可滑动选择,可以使用uni-ui提供的Picker组件。 首先,在需要使用的页面中引入Picker组件: ```vue <template> <view> <picker :show="showPicker" :picker-data="pickerData" @cancel="cancelPicker" @confirm="confirmPicker"></picker> </view> </template> <script> import { Picker } from 'uni-ui'; export default { components: { Picker }, data() { return { showPicker: false, pickerData: [ { label: '标题1', value: 'title1', children: [ { label: '选项1', value: 'option1' }, { label: '选项2', value: 'option2' }, { label: '选项3', value: 'option3' } ] }, { label: '标题2', value: 'title2', children: [ { label: '选项4', value: 'option4' }, { label: '选项5', value: 'option5' }, { label: '选项6', value: 'option6' } ] } ] } }, methods: { openPicker() { this.showPicker = true; }, cancelPicker() { this.showPicker = false; }, confirmPicker(value) { console.log(value); this.showPicker = false; } } } </script> ``` 在上面的代码中,我们定义了一个Picker组件,并传入了两个props: - `show`:是否显示Picker组件,可以通过设置该值控制组件的显示和隐藏。 - `picker-data`:Picker组件的数据源,该数据源是一个数组,每个元素都包含一个`label`和`value`属性,以及一个`children`数组,用于表示该元素下的子选项。 除了props之外,我们还定义了三个方法: - `openPicker`:用于打开Picker组件。 - `cancelPicker`:用于取消Picker组件的选择。 - `confirmPicker`:用于确认选择,并将选择的值通过`value`参数返回。 最后,我们在template中使用`v-for`循环生成Picker组件中的选项,用于实现二级标题可滑动选择的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值