1、本文转载至:http://blog.csdn.net/sophie_u/article/details/71745125

2、效果:

3、最终效果如上。问题: 
1)、tab标题总共8个,所以一屏无法全部显示。 
2)、tab内容区左右滑动切换时,tab标题随即做标记(active)。 
3)、当active的标题不在当前屏显示时,要使其能显示到当前屏中。

4、

一、wxml结构

tab标题因一排八个,所以使用 scroll-view组件,使其可横向滚动。 
tab内容可左右滑动切换,使用swiper组件实现 
为了偷懒,所以数据都通过wx:for遍历重复出来。

说明:

1、设置data-current属性用于:点击当前项时,通过点击事件swichNav中处理e.dataset.current取到点击的目标值。 
2、swiper组件的current组件用于控制当前显示哪一页 
3、swiper组件绑定change事件switchTab,通过e.detail.current拿到当前页

<view >
<scroll-view scroll-x="true" class="tab-h" scroll-left="{{scrollLeft}}">
<view class="tab-item {{currentTab==0?'active':''}}" data-current="0" bindtap="swichNav">健康</view>
<view class="tab-item {{currentTab==1?'active':''}}" data-current="1" bindtap="swichNav">情感</view>
<view class="tab-item {{currentTab==2?'active':''}}" data-current="2" bindtap="swichNav">职场</view>
<view class="tab-item {{currentTab==3?'active':''}}" data-current="3" bindtap="swichNav">育儿</view>
<view class="tab-item {{currentTab==4?'active':''}}" data-current="4" bindtap="swichNav">纠纷</view>
<view class="tab-item {{currentTab==5?'active':''}}" data-current="5" bindtap="swichNav">青葱</view>
<view class="tab-item {{currentTab==6?'active':''}}" data-current="6" bindtap="swichNav">全部</view>
<view class="tab-item {{currentTab==7?'active':''}}" data-current="7" bindtap="swichNav">其他</view>
</scroll-view>
<swiper class="tab-content" current="{{currentTab}}" duration="300" bindchange="switchTab"
style="height:{{winHeight}}rpx">
<swiper-item wx:for="{{[0,1,2,3,4,5,6,7]}}">
<scroll-view scroll-y="true" class="scoll-h" >
<block wx:for="{{[1,2,3,4,5,6,7,8]}}" wx:key="*this">
<view class="item-ans">
<view class="avatar">
<image class="img" src="http://ookzqad11.bkt.clouddn.com/avatar.png"></image>
</view>
<view class="expertInfo">
<view class="name">欢颜</view>
<view class="tag">知名情感博主</view>
<view class="answerHistory">134个回答,2234人听过 </view>
</view>
<navigator url="/pages/askExpert/expertDetail" class="askBtn">问TA</navigator>
</view>
</block>
</scroll-view>
</swiper-item>
</swiper>
</view>

三、js部分

var app = getApp();
Page({
data:{
winHeight:"",//窗口高度
currentTab:0, //预设当前项的值
scrollLeft:0, //tab标题的滚动条位置
expertList:[{ //假数据
img:"avatar.png",
name:"欢顔",
tag:"知名情感博主",
answer:134,
listen:2234
}]
},
// 滚动切换标签样式
switchTab:function(e){
var that = this;
that.setData({
currentTab:e.detail.current
});
that.checkCor();
},
// 点击标题切换当前页时改变样式
swichNav:function(e){
     var that = this; 
var cur=e.target.dataset.current;
if(that.data.currentTaB==cur){return false;}
else{
that.setData({
currentTab:cur
})
}
},
//判断当前滚动超过一屏时,设置tab标题滚动条。
checkCor:function(){
    var that = this;
if (that.data.currentTab>4){
that.setData({
scrollLeft:300
})
}else{
that.setData({
scrollLeft:0
})
}
},
onLoad: function() {
var that = this;
// 高度自适应
wx.getSystemInfo( {
success: function( res ) {
var clientHeight=res.windowHeight,
clientWidth=res.windowWidth,
rpxR=750/clientWidth;
var calc=clientHeight*rpxR-180;
console.log(calc)
that.setData( {
winHeight: calc
});
}
});
},
footerTap:app.footerTap
})

三、wxss样式

.tab-h{
height: 80rpx;width: 100%; box-sizing: border-box;overflow: hidden;line-height: 80rpx;background: #F7F7F7; font-size: 16px; white-space: nowrap;position: fixed;top:; left:; z-index:;}
.tab-item{margin:0 36rpx;display: inline-block;}
.tab-item.active{color: #4675F9;position: relative;}
.tab-item.active:after{ content: "";display: block;height: 8rpx;width: 52rpx;background: #4675F9;position: absolute; bottom:;left: 5rpx;border-radius: 16rpx;}
.item-ans{ width: 100%;display: flex; flex-grow: row no-wrap;justify-content: space-between; padding: 30rpx;box-sizing: border-box; height: 180rpx;align-items: center;border-bottom: 1px solid #F2F2F2;}
.avatar{width: 100rpx;height: 100rpx;position: relative;padding-right: 30rpx;}
.avatar .img{width: 100%;height: 100%;}
.avatar .doyen{width: 40rpx;height: 40rpx;position: absolute;bottom: -2px;right: 20rpx;}
.expertInfo{font-size: 12px;flex-grow:;color: #B0B0B0;line-height: 1.5em;}
.expertInfo .name{font-size: 16px;color:#000;margin-bottom: 6px;}
.askBtn{ width: 120rpx;height: 60rpx;line-height: 60rpx;text-align: center;font-size: 14px; border-radius: 60rpx;border: 1px solid #4675F9; color:#4675F9;}
.tab-content{margin-top: 80rpx;}
.scoll-h{height: 100%;}

转载【微信小程序】:微信小程序滚动Tab选项卡:左右可滑动切换(仿某宝)的更多相关文章

  1. 微信小程序滚动Tab实现左右可滑动切换

    --------------------------------------------------------wxml---------------------------------------- ...

  2. 微信小程序-滚动Tab选项卡

    前言:今天呢 给大家详细讲解一下滚动Tab选项卡:左右可滑动切换的效果,希望对大家做项目时候有用! 以前也遇到过这个,但是没有做记录.转载来源于:https://www.jianshu.com/p/9 ...

  3. 微信小程序滚动Tab选项卡:左右可滑动切换

    最终效果如上.问题: 1.tab标题总共8个,所以一屏无法全部显示. 2.tab内容区左右滑动切换时,tab标题随即做标记(active). 3.当active的标题不在当前屏显示时,要使其能显示到当 ...

  4. 微信小程序(三)页面跳转和图片滑动切换

    跳转部分: 在index2.wxml中,添加跳转函数,如下所示:

  5. 微信小程序滚动tab的实现

    微信小程序滚动tab的实现 1.目的:为了解决滚动版本的tab,以及实现虹吸效果. 2.方案:自己动手写了一个Demo,用于测试实现如上的效果.其代码有做参考,在这里先声明.具体的参照如下:重庆大学二 ...

  6. 微信小程序tab切换,可滑动切换,导航栏跟随滚动实现

    简介 看到今日头条小程序页面可以滑动切换,而且tab导航条也会跟着滚动,点击tab导航,页面滑动,切导航栏也会跟着滚动,就想着要怎么实现这个功能 像商城类商品类目如果做成左右滑动切换类目用户体验应该会 ...

  7. 微信小程序左右滑动切换页面示例代码--转载

    微信小程序——左右滑动切换页面事件 微信小程序的左右滑动触屏事件,主要有三个事件:touchstart,touchmove,touchend. 这三个事件最重要的属性是pageX和pageY,表示X, ...

  8. 微信正式开放内测“小程序”,不开发APP的日子真的来了?

    关注,QQ群,微信应用号社区 511389428 微信正式开放内测“小程序”,不开发APP的日子真的来了? 明星公司 缪定纯 • 2016-09-22 09:05 讨论了很久的微信应用号终于来了,不过 ...

  9. 微信小程序——智能小秘“遥知之”源码分享(语义理解基于olami)

    微信小程序智能生活小秘书开发详解 >>>>>>>>>>>>>>>>>>>>> ...

随机推荐

  1. python接口自动化5-Json数据处理

    前言 有些post的请求参数是json格式的,这个前面第二篇post请求里面提到过,需要导入json模块处理. 一般常见的接口返回数据也是json格式的,我们在做判断时候,往往只需要提取其中几个关键的 ...

  2. [runtime] initialize方法讲解

    + (void)initializeDescription(描述)    Initializes the class before it receives its first message. 在这个 ...

  3. 将properties文件放在Jar包并读取

    有时候需要在一个library内部打包一个properties文件,包含一些配置信息,而不能部署在外部. 在maven工程里面,将properties文件放在src/main/resources目录下 ...

  4. 用户组篇endgrent getpw getuid

    endgrent(关闭组文件) 相关函数 getgrent,setgrent 表头文件 #include<grp.h> #include<sys/types.h> 定义函数 v ...

  5. 混沌数学之Kent模型

    相关软件:混沌数学之离散点集图形DEMO 相关代码: // http://wenku.baidu.com/view/7c6f4a000740be1e650e9a75.html // 肯特映射 clas ...

  6. SSM(SpringMVC+Spring+Mybatis)框架程序on IDEA

    有了之前文章搭建的SSH框架之后,现在搭建基于Mybatis的框架.主要基于如下这篇文章: http://blog.csdn.net/gallenzhang/article/details/51932 ...

  7. 模态框在IE下的问题,即position:fixed在IE下不兼容的处理方式

    项目中遇到的问题,模态框在IE下总出现如图所示双层遮罩框,经排查发现是由于bootstrap里写的modal的样式里position:fixed不兼容IE的原因,导致铺不满整个窗口. 解决方案:在项目 ...

  8. C++的基本类型

  9. shell中使用if判断时用到的一些参数

    shell 编程中使用到得if语句内判断参数 –b 当file存在并且是块文件时返回真 -c 当file存在并且是字符文件时返回真 -d 当pathname存在并且是一个目录时返回真 -e 当path ...

  10. ArcGIS按字段属性分割文件

    在ArcGIS中我们有时需要将一个文件安字段的属性分割成多个文件,网友总结了几乎所有的方法:http://blog.sina.com.cn/s/blog_4e87fb4c0100fcjh.html , ...