最近在研究小程序的时候,遇到了一个问题,就是tabbar切卡,在android上有fragment,在RN上也有提供一个第三方的组件来用,微信小程序,好像没有专门的一个组件来实现这个功能,度娘了大半天,虽然也有好多人实现过这样的功能,但是我发现要么是不符合我的需求,那么就是实现起来过于繁琐。于是我参考多个高手写的博客,实现出了一个符合自己需求的。先上代码。

先看js文件:

 const util = require('../../utils/util.js');

 Page({
data: {
navTab:["投资","理财"],
/**
* 页面配置
*/
winWidth: 0,
winHeight: 0, // tab切换
currentTab: 0,
arrayList:[1,2,3,4,5,6,6,7,6,3,2,1,8,9,6,0,4],
modalHidden:true,
content:"这是一个表情",
imagesHeightList:[],
imageList:[
"../../images/image1.jpg",
"../../images/image2.jpg",
"../../images/image3.jpg",
],
datalist:[
"../../images/image1.jpg",
"../../images/image2.jpg",
"../../images/image3.jpg",
], }, /**
* 页面初始化
* options 为页面跳转所带来的参数
*/
onLoad: function (options) {
var that = this; /**
* 获取系统信息
*/
wx.getSystemInfo({ success: function (res) {
that.setData({
winWidth: res.windowWidth,
winHeight: res.windowHeight
});
} }); },
onReady: function () { }, /**
* 显示弹窗
*/
modalShow:function(){
var that = this;
that.setData({
modalHidden:false,
}); }, /**
* 点击取消,确认按钮
*/
modalChange:function(){
var that = this;
that.setData({
modalHidden:true,
});
}, /**
* 滑动切换tab
*/
bindChange: function (e) { var that = this;
that.setData({
currentTab: e.detail.current
}); },
/**
* 点击tab切换
*/
swichNav: function (e) {
console.log("--------");
var that = this; if (that.data.currentTab === e.target.dataset.idx) {
return false;
} else {
that.setData({
currentTab: e.target.dataset.idx
})
} },
})

css代码

 /**index.wxss**/
.swiper-tab{
width: 100%;
border-bottom: 0.5rpx solid #cccccc;
text-align: center;
line-height: 80rpx;} .top-tab{
width: 750rpx;
height: 100rpx;
background: #fff;
color: #000;
font-size: 28rpx;
font-weight: bold;
line-height: 100rpx;
border-bottom: 4rpx solid #eee;
margin: 0 20rpx 8rpx 0rpx;
position: fixed;
top: 0;
z-index: 9999;
} .shift{
position:absolute;
bottom: 1px;
height: 3px;
width: 20%;
background: red;
} .on{
color: red;
border-bottom: 2rpx solid red ;
} .swiper-box{
display: block;
height: 100%;
width: 100%;
overflow: hidden;
} .swiper-box view{
text-align: center;
} .swiper-tab{
height: 30px;
line-height: 30px;
background: #FFF;
display: flex;
position: relative;
z-index: 2;
border-bottom: 1px solid #F4F4F4;
flex-direction:row;
justify-content:center;
align-items:center;
} .swiper-tab-list{
margin: 0 20px;
padding: 0 4px;
font-size: 28rpx;
font-family: Helvetica, Arial, "Hiragino Sans GB", "Source Han Sans CN", "PingFang SC", Roboto, "微软雅黑", "Heiti SC", "Microsoft Yahei", sans-serif }
.on{ border-bottom: 1px solid #48C23D; color: #48C23D; } .swiper-box{
display: block;
height: 100%;
width: 100%;
overflow: hidden;
} .hot-box{ display: block; height: 100%; font-family: Helvetica, Arial, "Hiragino Sans GB", "Source Han Sans CN", "PingFang SC", Roboto, "微软雅黑", "Heiti SC", "Microsoft Yahei", sans-serif }
.hot-box-main{ display: block; overflow: hidden; margin-bottom: 20px; background: #FFF; } .hot-main{ height: 50px; background: url("../../static/001.jpg") #FFF no-repeat 0 50%; background-size: cover; padding: 30px 20px; position: relative; z-index: 1; }
.hot-main::before{ position: absolute; top: 0; bottom: 0; left: 0; right: 0; content: ''; background-image: linear-gradient(180deg, rgba(0,0,0,0.05) 5%,rgba(0,0,0,0.85) 100%); } .hot-main-box{ position: relative; z-index: 2; }
.hot-main-title{ color: #FFF; font-size: 34rpx; } .list-box{ padding: 20px 20px 20px 40px; display: block; font-size: 30rpx; border-bottom: 1px solid #F6F6F6; position: relative;}
.list-box::after{ display: block; content: ""; position: absolute; width: 6px; height: 6px; background: #ff6511; border-radius: 50%; top: 25px; left: 20px; }
.list-box-title{ font-weight: 400; line-height: 42rpx; }
.list-box-sub{ font-size: 24rpx; color: #666; padding-top: 6px; } .hot-box-more{ text-align: center; font-size: 22rpx; color: #999; margin-bottom: 30px; }
.hot-box-more image{ width: 80px; height: 16px; display: block; margin: 0 auto;} .swiper-boxs { height: 200px;
background: #F2F2F2;
}
.swiper-boxs-img {
display: block;
width: 100%;
height: 200px;
position: relative;
}
.swiper-boxs-img::before{
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
content: '';
background-image: linear-gradient(180deg, rgba(0,0,0,0.05) 5%,rgba(0,0,0,0.5) 100%);
} /*理财 */
.themes-box{
display: block;
}
.themes-box-top{
text-align: center;
padding-bottom: 16px;
padding-top: 16px;
background: #F9F9F9; }
.themes-box-title{
font-size: 16px;
font-weight: 500;
display: block; }
.themes-box-subtitle{
font-size: 12px;
color: #666;
display: block;
margin-top: 4px;
} /* 投资列表 */
.themes-list{
background: #FFF;
display: block;
margin-bottom: 20px;
}
.themes-list-box{
display: block;
position: relative;
padding: 16px 20px;
border-bottom: 1px solid #F2F2F2;
}
.themes-list-thumbnail{
position: absolute;
left: 20px;
top: 16px;
height: 40px;
width: 40px;
}
.themes-list-img{
width: 40px;
height: 40px;
border-radius: 4px;
border: 1px solid #F2F2F2;
}
.themes-list-main{
margin-left: 50px;
}
.themes-list-name{
font-size: 14px;
color: #444;
height: 20px;
line-height: 20px;
overflow: hidden;
}
.themes-list-description{
font-size: 12px;
color: #999;
height: 20px;
line-height: 20px;
overflow: hidden;
} /*理财系列*/ .WxMasonryView{
column-count:2;
column-gap: 2px;
width: 100%;
}
.WxMasonry{
width: 95%;
background: #fefefe;
border: 2px solid #fcfcfc;
box-shadow: 0px 2px 2px rgba(34, 25, 25, 0.4);
margin: 5px 2px 2px 2px;
padding: 1px;
padding-bottom: 5px;
transition: opacity .4s ease-in-out;
display: inline-block;
}
.WxMasonryImage{
width: 100% !important;
display: inline-block;
} .swiper-box{
display: block;
height: 100%;
width: 100%;
overflow: hidden;
} .hot-box{
display: block;
height: 100%;
font-family: Helvetica, Arial, "Hiragino Sans GB", "Source Han Sans CN", "PingFang SC", Roboto, "微软雅黑", "Heiti SC", "Microsoft Yahei", sans-serif
}
.hot-box-main{
display: block;
overflow: hidden;
margin-bottom: 20px;
background: #FFF;
} .hot-main::before{ position: absolute; top: 0; bottom: 0; left: 0; right: 0; content: ''; background-image: linear-gradient(180deg, rgba(0,0,0,0.05) 5%,rgba(0,0,0,0.85) 100%); } .hot-box-top{ padding: 20px; border-bottom: 1px solid #F6F6F6; text-align: center; }
.hot-box-title{ font-size: 32rpx; height: 30px; line-height: 32px; font-weight: 400; color: #444; padding: 5px 10px; border: 1px solid #444; } .list-box{
padding: 0;
display: inline-block;
font-size: 30rpx;
border-bottom: 1px solid #F6F6F6;
}
.list-box-title{ font-weight: 300; line-height: 42rpx; }

在看看html上面的代码

 <view class="swiper-tab">

 <view class="swiper-tab-list flex-item {{currentTab==idx ? 'on' : ''}}" wx:for="{{navTab}}" wx:for-index="idx" wx:for-item="itemName" data-idx="{{idx}}" bindtap="swichNav">
{{itemName}}
</view>
</view> <swiper current="{{currentTab}}" class="swiper-box" duration="300" style="height:{{winHeight - 31}}px" bindchange="bindChange"> <!-- 投资页面 -->
<swiper-item> <scroll-view class="hot-box" scroll-y="true" upper-threshold="50"
lower-threshold="100"
bindscrolltolower="scrolltolower"> <view class="themes-list"> <view class="themes-list-box" wx:for="{{arrayList}}">
<!--<navigator url="../themes/themes?id={{item.id}}">-->
<view class="themes-list-thumbnail">
<!--<image class="themes-list-img" src="{{item.thumbnail}}" />-->
</view>
<view class="themes-list-main" bindtap="modalShow">
<view class="themes-list-name">
<text>皇家林</text>
</view>
<view class="themes-list-description">
<text>是帅哥</text>
</view>
</view> <!--</navigator>-->
<modal title="标题" confirm-text="确认" cancel-text="取消" hidden="{{modalHidden}}" bindconfirm="modalChange" bindcancel="modalChange">
确定要进入详情页面吗。
</modal>
</view> </view>
</scroll-view>
</swiper-item> <!-- 理财页面 -->
<swiper-item>
<scroll-view class="hot-box" scroll-y="true" upper-threshold="50"
lower-threshold="100" bindscrolltolower="scrolltolower">
<text>理财页面</text> <swiper class="swiper-box" duration="300" style="height:{{winHeight - 31}}px" bindchange="bindChange"> <swiper-item>
<scroll-view class="hot-box" scroll-y="true" upper-threshold="50"
lower-threshold="100" bindscrolltolower="scrolltolower"> <view class="hot-box-main WxMasonryView" >
<block wx:for="{{datalist}}" wx:for-item="item" wx:for-index="i" wx:key=""> <view class="WxMasonry" wx:for="{{imageList}}" wx:for-index="j" wx:for-item="items" wx:key=""> <image class="WxMasonryImage" id="{{i*10+j}}" style="width:{{imagesHeightList[i*10+j].width}}px;height:{{imagesHeightList[i*10+j].height}}px" bindload="WxMasonryImageLoad" src="../../images/imagebiaoqing.jpg"/>
<view class="list-box-title">
<text>{{content}}</text>
</view> </view>
</block>
</view>
</scroll-view>
</swiper-item>
</swiper> </scroll-view>
</swiper-item> </swiper>

最后我们看看效果图

这里如果全部代码复制,会发现往左边滑动的时候滑动不了,不要奇怪,那是因为在理财页面我想实现一个瀑布流,是一个半成品,存在bug,大家去掉那一部分就会正常了。

微信小程序之tabbar切卡的更多相关文章

  1. 微信小程序消息通知-打卡考勤

    微信小程序消息通知-打卡考勤 效果: 稍微改一下js就行,有不必要的错误,我就不改了,哈哈! index.js //index.js const app = getApp() // 填写微信小程序ap ...

  2. 微信小程序自定义 tabbar

    一定的需求情况下,无法使用小程序原生的 tabbar 的时候,需要自行实现一个和 tabbar 功能一模一样的自制组件. 查阅了海量的博客和文档之后,亲自踩坑.总结了三种在不使用微信小程序原生 tab ...

  3. 微信小程序自定义tabbar的实现

    微信小程序自定义tabbar的实现 目的:当采用微信的自定义tabbar组件的时候,切换的时候会出现闪屏的效果:当使用微信默认的tabbar的时候,限制了tabbar的数量以及灵活配置. 方案:自己动 ...

  4. 图解微信小程序---添加tabBar底部菜单,添加已做好轮播图操作

    图解微信小程序---添加tabBar底部菜单,添加已做好轮播图操作 什么是tabBar? 顶部或者底部tab栏如果小程序是一个多 tab 应用(客户端窗口的底部或顶部有 tab 栏可以切换页面),可以 ...

  5. 微信小程序关于tabbar点击切换数据不刷新问题

    微信小程序中经常遇到的需求就是我提交了一个表单或者进行了一个操作,需要在我的个人中心页面中实时显示出来,但是小程序中的tabbar切换类似于tab切换 并不会进行页面刷新请求 所以总是会造成一些数据更 ...

  6. 解决微信小程序ios端滚动卡顿的问题

    方案1:直接使用微信小程序提供的 “scroll-view " 组件. <scroll-view scroll-y style="height: 100%;"> ...

  7. 微信小程序自定义TabBar

    项目中需要根据用户角色控制TabBar中各Item的显示和隐藏,然而小程序自带TabBar没有提供隐藏某个item的功能,只好自己动手写了一个. 1.wxml文件 <view class=&qu ...

  8. 【微信小程序】tabBar的显示问题

    tabBar不显示 在app.json中配置了4个页面,在tabBar的list中随意写了两个页面,编译后发现不能显示tabBar. { "pages": [ "page ...

  9. 微信小程序自定义tabbar的问题

    个人感觉小程序的tab样式自定义的能力有所欠缺,不够美观,于是今天自己diy了一个tab 测试的时候发现,无论是使用navigator跳转(会出现点击的效果)还是用bindtap(触摸),因为没有定义 ...

随机推荐

  1. 结对作业——四则运算 Part2. 封装与对接相关问题

    结对作业——四则运算 Part2. 封装与对接相关问题 PB15061303 刘梓轩PB16061489 艾寅中 GITHUB 地址 戳这里 目录 Part 1. Core代码编写部分Part 2. ...

  2. 【BZOJ 3261】最大异或和【可持久化字典树】

    题意 给出一个长度为n的整数序列,给出m个操作.操作有两种.1,Ax表示在序列结尾增加x.2,Qlrx表示找到一个位置p满足 l<=p<=r,使得a[p] xor a[p+1]xor... ...

  3. Opencv 发现轮廓 findContours

    vector<vector<Point>> vec_p; vector<Vec4i> vec_4f; findContours(img_canny1, vec_p, ...

  4. 477. Total Hamming Distance总的二进制距离

    [抄题]: The Hamming distance between two integers is the number of positions at which the correspondin ...

  5. 面向对象的JavaScript-001

    一. Question是父类,MultipleChoiceQuestion和DragDropQuestion是子类 二. 1. <script> // 面向对象 function Ques ...

  6. 清北学堂 day6 兔子

    ---恢复内容开始--- [问题描述] 在一片草原上有N个兔子窝,每个窝里住着一只兔子,有M条路径连接这些窝.更特殊地是,至多只有一个兔子窝有3条或更多的路径与它相连,其它的兔子窝只有1条或2条路径与 ...

  7. bootstrap缩略图及警示框制作

    缩略图在网站中最常用的地方就是产品列表页面,一行显示几张图片,有的在图片底下(左侧或右侧)带有标题.描述等信息.Bootstrap框架将这一部独立成一个模块组件.并通过“thumbnail”样式配合b ...

  8. [GO]单向channel和应用

    var ch1 chan int  //ch1是一个正常的channel,不是单向的 var ch2 chan <- float64   //ch2是一个单向的channel,只用于写float ...

  9. NBA常识 位置的划分 足球:越位等于抢跑

    篮球:1号位——组织后卫(控球,组织)2号位——得分后卫(中远投篮,突破)3号位-----小前锋(突破,中远投篮)4号位——大前锋(二中锋,篮板,背身单打,禁区防守)5号位——中锋(篮板.背身单打,禁 ...

  10. 编写高质量代码改善C#程序的157个建议——建议80:用Task代替ThreadPool

    建议80:用Task代替ThreadPool ThreadPool相对于Thread来说具有很多优势,但是ThreadPool在使用上却存在一定的不方便.比如: ThreadPool不支持线程的取消. ...