绿满眶商城微信小程序-uniapp
zxl
2025-07-17 b303b6945a139153688e86635346a621fe0c29b3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<template>
    <view class="top-bar" :style="{paddingTop: statusBarHeight + 'px'}">
        <view class="top-bar-content">
            <!-- 标题列表 -->
            <scroll-view class="title-scroll" scroll-x="true" scroll-with-animation :scroll-left="scrollLeft">
                <view class="title-container">
                    <view 
                        v-for="(item, index) in titleList" 
                        :key="index"
                        :class="{active: selectedTitleIndex === item.index, 'title-item': true}"
                        :style="{color: textColor}"
                        @click="changeTab(item)"
                    >
                        <text>{{item.title}}</text>
                        <view class="underline" :style="{backgroundColor: textColor}" v-if="selectedTitleIndex === item.index"></view>
                    </view>
                </view>
            </scroll-view>
        </view>
    </view>
</template>
 
<script>
    export default {
        name:"TopBar",
        props: {
            selectedTitleIndex: {
                type: String,
                default: 'home'
            },
            textColor: {
                type: String,
                default: 'white'
            }
        },
        data() {
            return {
                statusBarHeight: 0,
                scrollLeft: 0,
                titleList: [
                    {
                        index: 'home',
                        pagePath: '/pages/tabbar/index/home',
                        title: '推荐'
                    },
                    {
                        index: 'shop',
                        pagePath: '/pages/commodity-square/commoditySquare',
                        title: '商品广场'
                    },
                    {
                        index: 'activity',
                        pagePath: '/pages/mine/activity/reportActivity',
                        title: '活动'
                    },
                    {
                        index: 'health',
                        pagePath: '/pages/health/healthVideo',
                        title: '大健康'
                    }
                ]
            };
        },
        created() {
            // 获取状态栏高度
            const systemInfo = uni.getSystemInfoSync();
            this.statusBarHeight = systemInfo.statusBarHeight;
        },
        methods: {
            changeTab(titleObj) {
                console.log("点击顶部导航", titleObj);
                if (titleObj.index !== this.selectedTitleIndex) {
                    this.$emit("changeTab", titleObj);
                    // 计算滚动位置使当前选中项居中
                    this.$nextTick(() => {
                        const query = uni.createSelectorQuery().in(this);
                        query.select(`.title-item[data-index="${titleObj.index}"]`).boundingClientRect();
                        query.select('.title-scroll').boundingClientRect();
                        query.exec(res => {
                            if (res[0] && res[1]) {
                                const itemLeft = res[0].left;
                                const scrollWidth = res[1].width;
                                const itemWidth = res[0].width;
                                this.scrollLeft = itemLeft - (scrollWidth - itemWidth) / 2;
                            }
                        });
                    });
                }
            }
        }
    }
</script>
 
<style>
    .top-bar {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            z-index: 999;
        }
        
        .top-bar .top-bar-content {
            box-sizing: border-box;
            display: flex;
            align-items: center;
        }
        
        .top-bar .title-scroll {
            width: 100%;
            height: 40px;
            white-space: nowrap;
        }
        
        .top-bar .title-scroll .title-container {
            display: inline-flex;
            height: 100%;
            align-items: center;
        }
        
        .top-bar .title-scroll .title-container .title-item {
            position: relative;
            padding: 0 24rpx;
            font-size: 32rpx;
            /* color: white; */
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            flex-direction: column;
        }
        
        .top-bar .title-scroll .title-container .title-item.active {
            font-weight: 500;
        }
        
        .top-bar .title-scroll .title-container .title-item.active .underline {
            position: absolute;
            bottom: 4rpx;
            left: 50%;
            transform: translateX(-50%);
            width: calc(100% - 76rpx);
            height: 4rpx;
            /* background-color: white; */
            border-radius: 4rpx;
        }
</style>