tab.vue 2.96 KB
<template>
    <div v-if="channel.length" class="channel-tab">
        <div class="channel ellipsis" v-for="(index, item) in channel" :class="{focus: index === current}" 
        @click="changeChannel(index)" :style="{width: (1 / channel.length) * 100 + '%'}">
            <span class="name">{{item.name | uppercase}}</span>
        </div>
    </div>
</template>

<script>
    const $ = require('jquery');
    const cookie = require('yoho-cookie');
    const qs = require('yoho-qs');
    const bus = require('common/vue-bus');

    const channelMap = {
        301: 'men',
        302: 'women',
        303: 'lifestyle'
    };

    module.exports = {
        props: {
            current: {
                type: Number,
                default: +cookie.get('_ChannelIndex') || 0
            },
            page: {
                type: String,
                default: 'channel'
            },
        },
        data() {
            return {
                channel: []
            };
        },
        created() {
            $.ajax({
                url: '/channel/channel.json'
            }).then(res => {
                if (res.data && res.data.length) {
                    const channel = [];

                    res.data.forEach((c, index) => {
                        channel.push({
                            name: c.channel_name,
                            channel: channelMap[c.channel_id] || ''
                        });

                        if (qs.channel === channelMap[c.channel_id]) {
                            this.current = index;
                            bus.$emit('channel.change', this.page, qs.channel);
                        }
                    });

                    this.channel = channel;
                }
            });
        },
        methods: {
            changeChannel(index) {
                const objY = {};

                objY[this.page + '_' + this.channel[this.current].channel] = window.scrollY;

                this.current = index;
                bus.$emit('channel.change', this.page, this.channel[index].channel, objY);
                cookie.set('_Channel', this.channel[index].channel);
                cookie.set('_ChannelIndex', index);
            }
        }
    };
</script>

<style>
    .channel-tab {
        position: fixed;
        top: 0;
        left: 50%;
        z-index: 1;
        width: 100%;
        max-width: 750px;
        height: 80px;
        font-size: 26px;
        text-align: center;
        background: #fff;
        transform: translate(-50%, 0);
        border-bottom: 1px solid #eee;
        font-family: "BrownStd", "PingFang SC", Helvetica, Roboto, "Heiti SC", "黑体", Arial;

        .channel {
            display: inline-block;
            line-height: 80px;
            color: #b0b0b0;

            &.focus {
                color: #000;
            }
        }

        .name {
            padding: 12px 0;

            &.focus {
                border-bottom: 4px solid #000;
            }
        }
    }
</style>