header-box.vue 4.55 KB
<template>
    <div class="blk-header-wrap" ref="header" v-if="showHeader" :class="[className, classList]">
        <div class="blk-header">
            <div class="blk-header-left">
                <slot name="left">
                    <i class="icon icon-back go-back-btn" @click="goBack"></i>
                </slot>
            </div>
            <div class="blk-header-main">
                <span class="blk-header-title">
                    <slot name="title">{{yoho.title}}</slot>
                </span>
            </div>
            <div class="blk-header-right">
                <slot name="right"></slot>
            </div>
        </div>
        <div class="blk-header-gap" v-if="placeholder"></div>
    </div>
</template>
<script>
import {mapState} from 'vuex';
export default {
    name: 'HeaderBox',
    props: {
        className: [String, Object, Array],
        placeholder: {
            type: Boolean,
            default: true
        },
        autoHide: [Boolean]
    },
    computed: {
        ...mapState(['yoho'])
    },
    data() {
        return {
            showHeader: true,
            classList: {ghost: this.autoHide}
        };
    },
    methods: {
        goBack() {
            this.$yoho.goBack({}, function() {}, function() {});
        },
        toggle() {
            if (window.scrollY > 100) {
                this.classList = {};
            } else if (window.scrollY > 70) {
                this.classList = {ghost: true, ghost3: true};
            } else if (window.scrollY > 30) {
                this.classList = {ghost: true, ghost2: true};
            }
        }
    },
    mounted() {
        if (this.autoHide) {
            window.addEventListener('touchmove', () => {
                this.toggle();
            });

            window.addEventListener('scroll', () => {
                this.toggle();
            });
        }
    }
};
</script>
<style lang="scss">
    @import "~scss/variable.scss";
    .blk-header-wrap.hide {
        .blk-header {
            display: none;
        }
    }
    .blk-header-wrap.ghost {
        .blk-header {
            background-color: rgba(255, 255, 255, 0);
            transition: 0.3s all;
            border-bottom: 0;
        }

        .blk-header-title {
            opacity: 0;
        }
    }

    .blk-header-wrap.ghost-2 {
        .blk-header {
            background-color: rgba(255, 255, 255, 0.4);
            transition: 0.3s all;
            border-bottom: 0;
        }

        .blk-header-title {
            opacity: 0.4;
        }
    }

    .blk-header-wrap.ghost-3 {
        .blk-header {
            background-color: rgba(255, 255, 255, 0.7);
            transition: 0.3s all;
            border-bottom: 0;
        }

        .blk-header-title {
            opacity: 0.7;
        }
    }

    .blk-header {
        box-sizing: content-box;
        position: fixed;
        display: flex;
        top: 0;
        right: 0;
        left: 0;
        z-index: 210;
        height: $header-height;
        max-width: 750px;
        margin-left: auto;
        margin-right: auto;
        line-height: $header-height;
        font-size: $header-font-size;
        font-weight: bold;
        background-color: #fff;
        border-bottom: 1px solid #eee;
        color: #000;

        .icon,
        .blk-header-title {
            vertical-align: middle;
        }

        .blk-header-left {
            width: 100px;
            text-align: center;
            font-size: 40px;
            .icon {
                font-size: 40px;
            }
        }
        

        .blk-header-right {
            width: 100px;
            text-align: center;
            font-size: 40px;
            .icon {
                font-size: 40px;
            }
        
            .right-btn {
                font-size: 17PX;
                margin-right: 30px;
            }
        }

        .blk-header-main {
            flex: 1;
            text-align: center;
            font-size: $header-font-size;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
        }
        .icon {
            display: inline-block;
            &.icon-search {
                margin-top: -10px;
            }
        }

        .go-back-btn {
            &:active {
                background: #ccc;
                opacity: 0.5;
            }
        }
    }


    .blk-header-gap {
        height: $header-height + 2;
        background-color: transparent;
    }

    .blk-header-wrap {
        &.ghost {
            .blk-header-title {
                visibility: hidden;
            }
        }
    }

</style>