美文网首页微信小程序开发微信小程序开发者
2018-07-19 wepy生成小程序 横向tab

2018-07-19 wepy生成小程序 横向tab

作者: GeeFib | 来源:发表于2018-07-19 12:08 被阅读9次

    上文件#

    <template>
        <view >
            <scroll-view scroll-x="true" class="tab-h" scroll-left="{{scrollLeft}}">
                <view class="tab-item {{currentTab==0?'active':''}}"  data-current="0" @tap="swichNav">健康</view>
                <view class="tab-item {{currentTab==1?'active':''}}" data-current="1" @tap="swichNav">情感</view>
                <view class="tab-item {{currentTab==2?'active':''}}" data-current="2" @tap="swichNav">职场</view>
                <view class="tab-item {{currentTab==3?'active':''}}" data-current="3" @tap="swichNav">育儿</view>
                <view class="tab-item {{currentTab==4?'active':''}}" data-current="4" @tap="swichNav">纠纷</view>
                <view class="tab-item {{currentTab==5?'active':''}}" data-current="5" @tap="swichNav">青葱</view>
                <view class="tab-item {{currentTab==6?'active':''}}" data-current="6" @tap="swichNav">全部</view>
                <view class="tab-item {{currentTab==7?'active':''}}" data-current="7" @tap="swichNav">其他</view>
            </scroll-view>
            <swiper class="tab-content" current="{{currentTab}}" duration="300" @change="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">1个回答,2人听过 </view>
                                </view>
                                <view  class="askBtn">问TA</view>
                            </view>
                        </block>
                    </scroll-view>
                </swiper-item>
            </swiper>
        </view>
    </template>
    <style lang="less">
        .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: 0; left: 0; z-index: 99;}
        .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: 0;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: 2;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%;}
    </style>
    <script>
        import wepy from 'wepy';
        import config from '../config';
        import request from '../vendor/request';
        import language from '../language';
        export default class popularScience extends wepy.page {
            methods = {
                // 滚动切换标签样式
                switchTab(e){
                    this.currentTab = e.detail.current
                    this.checkCor();
                },
                // 点击标题切换当前页时改变样式
                swichNav(e){
                    var cur=e.target.dataset.current;
                    if(this.currentTaB==cur){return false;}
                    else{
                        this.currentTab= cur
                    }
                }
            };
            data = {
                winHeight:"",//窗口高度
                currentTab:0, //预设当前项的值
                scrollLeft:0, //tab标题的滚动条位置
                expertList:[{ //假数据
                    img:"avatar.png",
                    name:"欢顔",
                    tag:"知名情感博主",
                    answer:134,
                    listen:2234
                }]
            };
            components = {
    
            };
            async onShow(){
    
            };
            // async onLoad() {
            //     // await this.getCities();
            // };
            //判断当前滚动超过一屏时,设置tab标题滚动条。
            checkCor(){
                if (this.currentTab>4){
                    this.scrollLeft = 300
                }else{
                    this.scrollLeft = 0
                }
            }
            onLoad() {
                var that = this;
                //  高度自适应
                wepy.getSystemInfo( {
                    success: function( res ) {
                        var clientHeight=res.windowHeight,
                            clientWidth=res.windowWidth,
                            rpxR=750/clientWidth;
                        var  calc=clientHeight*rpxR-180;
                        that.winHeight = calc
                    }
                });
            }
            footerTap(){
                return this.$wxapp.footerTap
            }
            onReachBottom() {
    
            };
            onShareAppMessage(){
                return config.shareConfig;
            }
        }
    </script>
    
    

    小程序原生编写 参考:
    https://www.jianshu.com/p/94849f9c2ff2

    相关文章

      网友评论

        本文标题:2018-07-19 wepy生成小程序 横向tab

        本文链接:https://www.haomeiwen.com/subject/jyxtmftx.html