【Vue】实现滑动切换信息卡片方法分享

865 阅读1分钟

实现效果:

水平向右滑动即可切换卡片,点击可以直接在一级页面切换就诊人,减少了多一级切换的繁琐

image.png

image.png

实现方式:整体css+js即可实现,使用一个selectedIndex: 0参数控制active类,方便后续开发点击事件,以下是实现源码

  <!-- 选择就诊人区域 -->
        <div class="people_content">
            <!-- 顶部title -->
            <div class="patient_msg">
                <span class="title">请选择就诊人</span>
                <span class="patient">张三丰|12345678</span>
            </div>

            <!-- 就诊卡 -->
            <div class="patient_card">
                <!-- 已有的就诊卡 -->
                <div :class="{ active_card: selectedIndex === index }" class="card" v-for="(item, index) in cardList"
                    :key="index" @click="clickCard(index)">
                    <span class="name">{{ item.name }}</span>
                    <span class="relation">{{ item.relation }}</span>
                </div>

                <!-- 添加就诊卡 -->
                <div class="add" @click="goToAddPatient">
                    <van-icon name="add-o" />
                </div>
            </div>
        </div>

js部分如下:

data() {
        return {
            cardList: [
                {
                    name: '张三丰',
                    relation: '本人'

                },
                {
                    name: '张子枫',
                    relation: '亲属'

                },
                {
                    name: '张丰丰',
                    relation: '哥哥'

                },
                {
                    name: '张路由',
                    relation: '弟弟'

                }
            ], selectedIndex: 0
        }
    },
    methods: {
        onClickLeft() {
            this.$router.go(-1)
        },
        clickCard(index) {
            this.selectedIndex = index
        },
        goToAddPatient() {
            this.$router.push('/add_patient')
        }
    }

css实现如下,使用了scss:

.people_content {
        display: flex;
        flex-direction: column;
        width: 346px;
        border-radius: 10px;
        margin: 10px auto;
        background-color: #fff;

        .patient_msg {
            display: flex;
            height: 20px;
            font-size: 16px;
            font-weight: 700;
            // background-color: pink;
            // margin-top: 10px;
            margin: 10px 0 10px 10px;
            line-height: 20px;

            .patient {
                font-size: 14px;
                margin-left: 10px;
                font-weight: 400;
                color: rgb(62, 62, 62);
            }
        }

        .patient_card {
            display: flex;
            flex-direction: row;

            width: 346px;
            height: 60px;
            overflow-x: auto;

            margin-bottom: 10px;

            .card {
                display: flex;
                flex-direction: column;
                flex-shrink: 0;
                justify-content: center;
                align-content: center;
                text-align: center;
                width: 100px;
                height: 60px;
                margin-left: 10px;
                background-color: rgb(246, 247, 249);
                border-radius: 10px;
                color: rgb(62, 62, 62);

                font-size: 16px;
                font-weight: 700;

                .relation {
                    font-size: 14px;
                    font-weight: 400px;
                }

            }

            .add {
                display: flex;
                flex-shrink: 0;
                flex-direction: column;
                justify-content: center;
                align-content: center;
                text-align: center;
                width: 100px;
                height: 60px;
                margin-left: 10px;
                background-color: rgb(246, 247, 249);
                border-radius: 10px;
                color: rgb(62, 62, 62);
                font-size: 20px;
                font-weight: 700;
            }

        }



    }