uniapp 滑动切换

不管现实多么惨不忍睹,都要持之以恒地相信,这只是黎明前短暂的黑暗而已。不要惶恐眼前的难关迈不过去,不要担心此刻的付出没有回报,别再花时间等待天降好运。真诚做人,努力做事!你想要的,岁月都会给你。uniapp 滑动切换,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

近期会更新最新版的tab切换,敬请期待……

说明:本案例的样式基于colorui组件库 感兴趣的小伙伴可以看下教程 colorui组件库开发文档或者csdn的文档,顺便再分享下 colorui的群资源

最近项目中需要用到滑动切换的效果,自己懒得写就去网上找了下,发现网上的也不靠谱,不是样式错乱就是其他bug,反正到你的电脑上就是各种bug般的存在,刚好趁着过年的时间把这个完整的效果给小伙伴们陈列下我的gitee

效果图

效果图

组件封装

  • swiper-tab.vue
  • html
<template>
	<view class="">
	  <scroll-view scroll-x class="nav bg-white" style="scrollStyle">
	    <view class="flex text-center">
	      <view class="cu-item flex-sub" :style="scrollItemStyle" @tap="tabtap(index)" :class="tabIndex==index?'text-blue cur':''" v-for="(tab,index) in tabBars" :key="tab.id">{{tab.name}} {{tab.num?tab.num:''}}</view></view>
	  </scroll-view>
	</view>
</template>
  • css
<style>
	.uni-swiper-tab {
		border-bottom:1upx solid #EEEEEE;
	}
	.swiper-tab-list {
		color:#969696;
		font-weight:bold;
	}
	.uni-tab-bar .active {
		color:#343434;
	}
	.active .swiper-tab-line {
		border-bottom:6upx solid #FEDE33;
		width:70upx;
		margin:auto;
		border-top:6upx solid #FEDE33;
		border-radius:20upx;
	}

</style>
  • js
export default {
props: {
tabBars: Array,
tabIndex: Number,
scrollStyle: {
type: String,
default: ""
},
scrollItemStyle: {
type: String,
default: ""
}
},
methods: {
tabtap(index) {
//点击tab时把自己的index传给父组件index.vue
this.$emit('tabtap', index);
},
}
}

页面中使用

  • 引用组件
<script>import swiperTab from "../../components/swiper-tab/swiper-tab.vue"
export default {
    components:
    {
      swiperTab
    },
    onLoad() {
      uni.getSystemInfo({
        success: (res) = >{
          let height = res.windowHeight - uni.upx2px(100) 
          this.swiperheight = height;
        }
      })
    },
    data() {
      return {
        tabIndex: 0,
        // 选中的
        tabBars: [{
          name: "关注",
          id: "guanzhu"
        },
        {
          name: "推荐",
          id: "tuijian"
        },
        {
          name: "体育",
          id: "tiyu"
        },
        {
          name: "热点",
          id: "redian"
        },
        {
          name: "财经",
          id: "caijing"
        },
        {
          name: "娱乐",
          id: "yule"
        },
        ],
        swiperheight: 500,
        //高度
        newslist: [{
          list: [{
            username: "昵称",
            title: "我是标题",
          },
          {
            username: "昵称",
            title: "我是标题",
          },
          {
            username: "昵称",
            title: "我是标题",
          },
          {
            username: "昵称",
            title: "我是标题",
          },
          {
            username: "昵称",
            title: "我是标题",
          },
          {
            username: "昵称",
            title: "我是标题",
          },
          {
            username: "昵称",
            title: "我是标题",
          },
          {
            username: "昵称",
            title: "我是标题",
          },

          ]
        },
        {
          list: [{
            username: "昵称",
            title: "我是标题",
          },
          ]
        },
        {
          list: [{
            username: "昵称",
            title: "我是标题",
          },
          ]
        },
        {
          list: [{
            username: "昵称",
            title: "我是标题",
          },
          ]
        },
        {
          list: [{
            username: "昵称",
            title: "我是标题",
          },
          ]
        },
        {
          list: [{
            username: "昵称",
            title: "我是标题",
          },
          ]
        }]
      }
    },
    methods: {
      tabtap(index) {
        this.tabIndex = index;
      },
      //滑动切换导航
      tabChange(e) {
        this.tabIndex = e.detail.current;
        // console.log(this.tabIndex)
      },
    }
  }</script>
  • css(重点)
<style>
	/* 解决不能滚动问题 */
	/* uni-swiper .uni-swiper-wrapper, */
	uni-swiper-item {
	    overflow-y: scroll !important;
	}
</style>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/158668.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!