uniapp(vue)實現點擊左側菜單,右側顯示對應的內容


 

 

 

<template>
    <view class="container">
        <view class="fication-search">
            <input type="text" value="" placeholder="請輸入您要搜索的內容"/><image class="search-icon" src="../../static/images/search.png" mode=""></image>
        </view>
        <!-- 滾動區域 -->
        <view class="scroll-panel" id="scroll-panel">
            <view class="list-box">
                <view class="left">
                    <scroll-view scroll-y="true" :style="{ height: scrollHeight + 'px' }" :scroll-into-view="leftIntoView">
                        <view
                            class="item"
                            v-for="(item, index) in leftArray"
                            :key="index"
                            :class="{ active: index == leftIndex }"
                            :id="'left-' + index"
                            :data-index="index"
                            @tap="leftTap"
                        >
                        <view class="activelink"></view>
                         <text class="item-name">{{ item }}</text> 
                        </view>
                    </scroll-view>
                </view>
                <view class="main">
                    <scroll-view scroll-y="true" :style="{ height: scrollHeight + 'px' }" @scroll="mainScroll" :scroll-into-view="scrollInto" scroll-with-animation="true">
                        <view class="item main-item" v-for="(item, index) in mainArray" :key="index" :id="'item-' + index">
                            <view class="title">
                                <view>{{ item.title }}</view>
                            </view>
                            <view class="goods" v-for="(item2, index2) in item.list" :key="index2">
                                <view class="orderlist-list">
                                    <view class="list-left">
                                        <image src="../../static/images/store_bg.png" mode=""></image>
                                    </view>
                                    <view class="list-right">
                                        <view class="list-name">香辣火鍋</view>
                                        <view class="list-ping">
                                            <u-rate :count="count" active-color="#FFB800" inactive-color="#E0E0E0" size='32' v-model="value"></u-rate>
                                            <text>{{4.8}}分</text>
                                        </view>    
                                        <view class="list-meuns"><text>銷量{{99}}+</text><text>配送費¥{{3}}</text><text>距離{{1.2}}km</text></view>    
                                    </view>
                                </view>
                            </view>
                        </view>
                        <view class="fill-last" :style="{ height: fillHeight + 'px' }"></view>
                    </scroll-view>
                </view>
            </view>
        </view>
    </view>
    
</template>
<script>
export default{
    data() {
            return {
                scrollHeight: 400,
                scrollTopSize: 0,
                fillHeight: 0, // 填充高度,用於最后一項低於滾動區域時使用
                leftArray: [],
                mainArray: [],
                topArr: [],
                leftIndex: 0,
                scrollInto: '',
                count: 5,
                value: 4
            };
        },
        computed: {
            /* 計算左側滾動位置定位 */
            leftIntoView() {
                return `left-${this.leftIndex > 3 ? this.leftIndex - 3 : 0}`;
            }
        },
        mounted() {
            /* 等待DOM掛載完成 */
            this.$nextTick(() => {
                /* 在非H5平台,nextTick回調后有概率獲取到錯誤的元素高度,則添加200ms的延遲來減少BUG的產生 */
                setTimeout(() => {
                    /* 等待滾動區域初始化完成 */
                    this.initScrollView().then(() => {
                        /* 獲取列表數據,你的代碼從此處開始 */
                        this.getListData();
                    });
                }, 200);
            });
        },
        methods: {
            /* 初始化滾動區域 */
            initScrollView() {
                return new Promise((resolve, reject) => {
                    let view = uni.createSelectorQuery().select('#scroll-panel');
                    view.boundingClientRect(res => {
                        this.scrollTopSize = res.top;
                        this.scrollHeight = res.height;
                        this.$nextTick(() => {
                            resolve();
                        });
                    }).exec();
                });
            },
            /* 獲取列表數據 */
            getListData() {
                // Promise 為 ES6 新增的API ,有疑問的請自行學習該方法的使用。
                new Promise((resolve, reject) => {
                    /* 因無真實數據,當前方法模擬數據。正式項目中將此處替換為 數據請求即可 */
                    uni.showLoading();
                    setTimeout(() => {
                        let [left, main] = [[], []];
    
                        for (let i = 0; i < 12; i++) {
                            left.push(`${i + 1}類商品`);
    
                            let list = [];
                            let r = Math.floor(Math.random() * 10);
                            r = r < 1 ? 3 : r;
                            for (let j = 0; j < r; j++) {
                                list.push(j);
                            }
                            main.push({
                                title: `第${i + 1}類商品標題`,
                                list
                            });
                        }
    
                        // 將請求接口返回的數據傳遞給 Promise 對象的 then 函數。
                        resolve({ left, main });
                    }, 1000);
                }).then(res => {
                    console.log('-----------請求接口返回數據示例-------------');
                    console.log(res);
    
                    uni.hideLoading();
                    this.leftArray = res.left;
                    this.mainArray = res.main;
    
                    // DOM 掛載后 再調用 getElementTop 獲取高度的方法。
                    this.$nextTick(() => {
                        this.getElementTop();
                    });
                });
            },
            /* 獲取元素頂部信息 */
            getElementTop() {
                new Promise((resolve, reject) => {
                    let view = uni.createSelectorQuery().selectAll('.main-item');
                    view.boundingClientRect(data => {
                        resolve(data);
                    }).exec();
                }).then(res => {
                    let topArr = res.map(item => {
                        return item.top - this.scrollTopSize; /* 減去滾動容器距離頂部的距離 */
                    });
                    this.topArr = topArr;
    
                    /* 獲取最后一項的高度,設置填充高度。判斷和填充時做了 +-20 的操作,是為了滾動時更好的定位 */
                    let last = res[res.length - 1].height;
                    if (last - 20 < this.scrollHeight) {
                        this.fillHeight = this.scrollHeight - last + 20;
                    }
                });
            },
            /* 主區域滾動監聽 */
            mainScroll(e) {
                let top = e.detail.scrollTop;
                let index = 0;
                /* 查找當前滾動距離 */
                for (let i = this.topArr.length - 1; i >= 0; i--) {
                    /* 在部分安卓設備上,因手機邏輯分辨率與rpx單位計算不是整數,滾動距離與有誤差,增加2px來完善該問題 */
                    if (top + 2 >= this.topArr[i]) {
                        index = i;
                        break;
                    }
                }
                this.leftIndex = index < 0 ? 0 : index;
            },
            /* 左側導航點擊 */
            leftTap(e) {
                let index = e.currentTarget.dataset.index;
                this.scrollInto = `item-${index}`;
            }
        }
    };
</script>
<style lang="scss">
    
    page,
    .container {
        width: 100vw;
        height: 100%;
    }
    /* 容器 */
    .container {
        display: flex;
        flex-direction: column;
        flex-wrap: nowrap;
        justify-content: flex-start;
        align-items: flex-start;
        align-content: flex-start;
    
        // & > view {
        //     width: 100%;
        // }
    
        .scroll-panel {
            flex-grow: 1;
            height: 0;
            overflow: hidden;
        }
    
        
    }
    .fication-search{
        width: 686rpx;
        height: 64rpx;
        margin: 12rpx auto;
        background: #F5F6F7;
        border-radius: 32rpx 32rpx 32rpx 32rpx;
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
    }
    .fication-search input{
        padding-left: 24rpx;
        font-size: 12px;
        font-family: PingFang SC-Regular, PingFang SC;
        font-weight: 400;
        color: #B5B5B5;
    }
    .fication-search input::-webkit-input-placeholder{
                color:red!important;
    }
    
    .search-icon{
        width: 24rpx;
        height: 24rpx;
        margin-right: 30rpx;
    }
    .list-box {
        display: flex;
        flex-direction: row;
        flex-wrap: nowrap;
        justify-content: flex-start;
        align-items: flex-start;
        align-content: flex-start;
        font-size: 28rpx;
    
        .left {
            width: 196rpx;
            background: #F5F6F7;
            line-height: 112rpx;
            box-sizing: border-box;
            font-size: 28rpx;
            font-family: PingFang SC-Medium, PingFang SC;
    
            .item {
                
                position: relative;
                display: flex;
                
                &:not(:first-child) {
                    margin-top: 1px;
    
                    &::after {
                        content: '';
                        display: block;
                        height: 0;
                        // border-top: #d6d6d6 solid 1px;
                        width: 620upx;
                        position: absolute;
                        top: 0px;
                        right: 0;
                        transform: scaleY(0.5); /* 1px像素 */
                    }
                }
                .item-name{
                    padding-left: 32rpx;
                }
                &.active {
                    color: #F7433D;
                    background-color: #fff;
                }
                &.active .activelink{
                    width: 8rpx;
                    height: 48rpx;
                    margin-top: 32rpx;
                    padding-left: 0;
                    background-color: #F7433D;
                    border-radius: 2px 2px 2px 2px;
                }
            }
            .fill-last {
                height: 0;
                width: 100%;
                background: none;
            }
        }

            .title {
                line-height: 64rpx;
                font-size: 16px;
                font-family: PingFang SC-Bold, PingFang SC;
                font-weight: bold;
                color: #000000;
                padding: 8rpx 0;
                background-color: #fff;
                position: sticky;
                top: 0;
                z-index: 19;
            }
    
    }
    .orderlist-list{
        width: 526rpx;
        height: 200rpx;
        background: #FFFFFF;
        margin: 0 auto;
        display: flex;
        border-bottom: 2rpx solid #F5F6F7;
        // border-radius: 8px 8px 8px 8px;
    }
    .list-left{
        margin: 24rpx;
        width: 152rpx;
        height: 152rpx;
        border-radius: 4px 4px 4px 4px;
        overflow: hidden;
    }
    .list-left image{
        width: 100%;
        height: 100%;
    }
    .list-right{
        flex: 1;
        display: flex;
        flex-direction: column;
    }
    .list-name{
        margin-top: 24rpx;
        font-size: 16px;
        font-family: PingFang SC-Medium, PingFang SC;
        font-weight: 500;
        color: #000000;
        line-height: 19px;
    }
    .list-ping{
        margin: 20rpx 0;
        display: flex;
        font-size: 12px;
        font-family: PingFang SC-Regular, PingFang SC;
        font-weight: 400;
        color: #FFB800;
        
    }
    .list-ping text{
        margin-left: 6rpx;
    }
    .list-meuns{
        margin-bottom: 28rpx;
        font-size: 12px;
        font-family: PingFang SC-Medium, PingFang SC;
        font-weight: 500;
        color: #999999;
    }
</style>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM