阅读 298

uniapp tab选项卡简单demon

<template>
    <view class="content">
        <view class="title-bar">
            <view :class="selIndex==n?‘selIndex‘:‘‘" v-for="(i,n) in titleList" @click="selIndexs(n)" :key="n">{{ i }}
            view>
        view>
        <view class="tab">
            <scroll-view class="tab-scroll-view" :scroll-left="scrollLeft" scroll-with-animation :scroll-x="true">
                <view class="tab-item" v-for="(item, index) in titleList" :key="index" @touchstart="start" @touchmove="move" @touchend="end">
                    {{item}}
                view>
            scroll-view>
        view>
    view>
template>

<script>
    export default {
        data() {
            return {
                scrollLeft: 0,
                selIndex: 0,
                titleList: [待付款, 待收货, 已收货, 待评价, 售后],
                winWidth: 0,
                startX: 0,
                moveX: 0
            }
        },
        onLoad() {
            this.getSysInfo()
        },
        methods: {
            getSysInfo () {
                let that = this
                uni.getSystemInfo({
                    complete(res) {
                        that.winWidth = res.windowWidth
                        console.log(that.winWidth)
                    }
                })
            },
            selIndexs (n) {
                this.selIndex = n
                this.scrollLeft = n*this.winWidth
            },
            start (e) {
                this.startX = e.touches[0].clientX
            },
            move (e) {
                this.moveX = e.touches[0].clientX
            },
            end (e) {
                e.preventDefault();
                if (this.moveX - this.startX < 0) {
                    if (this.titleList.length*this.winWidth>this.scrollLeft) {
                        this.scrollLeft += this.winWidth
                        if ((this.titleList.length-1) > this.selIndex) {
                            this.selIndex +=1
                        }
                    }
                } else {
                    if (this.scrollLeft == 0) {
                        this.scrollLeft = 0
                        this.selIndex = 0
                    } else {
                        this.scrollLeft -= this.winWidth
                        this.selIndex -= 1
                    }
                }
                console.log(this.scrollLeft)
                console.log(this.selIndex)
            }
        }
    }
script>

<style>
    .content {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }

    .title-bar {
        width: 100%;
        height: 40px;
        font-size: 15px;
        display: flex;
        align-items: center;
        justify-content: space-around;
    }

    .selIndex {
        color: red;
    }

    .tab-scroll-view {
        width: 750rpx;
        height: 100vh;
        flex-direction: row;
        white-space: nowrap;
    }

    .tab-item {
        display: inline-block;
        width: 100vw;
        height: 100%;
        font-size: 16px;
        color: #555;
        border: 1px solid yellow;
        transition: all .5 ease-in-out;
    }
style>

 

原文:https://www.cnblogs.com/Strangers/p/14878686.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐