Vue 實現(xiàn)一個下拉菜單

前言

有時候我們需要去自定一個下拉菜單,這個時候就得自己擼css了

先來看demo


demo.gif

實現(xiàn)

其他的不相關(guān)的代碼,這里就刪掉了

<template>
    <div id="m-header">
        <div class="right">
            <i class="ivu-icon ivu-icon-md-menu" :style="{'font-size': iSize}" @click.stop="didClickMenus"></i>
            <ul :class="{active: isShow, none: !isShow}">
                <li class="item-top"></li>
                <li v-for="(item, index) in menus"
                    :key="index" @click="didSelectItem(index)"
                    :class="item.class">
                    {{item.name}}
                </li>
            </ul>
        </div>
    </div>
</template>

<script>
    export default {
        name: "m-header",
        mounted() {
            document.onclick = () => {
                if (this.isShow) {
                    this.isShow = false;
                }
            };
            let screenWidth = window.screen.width;
            let baseWidth = 1920;
            this.iSize = (screenWidth / baseWidth) * 100 + 'px';
        },
        data() {
            return {
                isShow: false,
                menus: [{
                    name: "MCR",
                    class: "item-normal"
                },{
                    name: "Roast",
                    class: "item-normal"
                },{
                    name: "Origins",
                    class: "item-normal"
                },{
                    name: "Store",
                    class: "item-last"
                }],
                iSize: 50
            }
        },
        methods: {
            didClickMenus() {
                this.isShow = true;
            },
            didSelectItem(index) {
                this.$emit('select-menu', index);
            }
        }
    }
</script>

<style scoped lang="less">

    #m-header {
        background-color: white;
        display: flex;
        padding: 1.08rem 1.6rem;

        .right {
            flex: 1;
            text-align: right;
            position: relative;
        }

        .none {
            display: none;
        }

        .active {
            position: absolute;
            background-color: rgba(0,0,0,0.2);
            width: 3.68rem;
            height: 4.8rem;
            right: 0;
            top: 0;
        }

        .item-top {
            height: 0.5rem;
            list-style-type: none;
        }

        .item-normal, .item-last {
            font-weight: 400;
            color: white;
            font-size: 0.45rem;
            padding-left: 0.48rem;
            padding-top: 0.2rem;
            padding-bottom: 0.2rem;
            text-align: left;
            position: relative;
            list-style-type: none;
            cursor: pointer;
        }

        .item-normal::after {
            content:"";
            width: calc(~"3.68rem - 0.48rem * 2");
            height: 1px;
            background-color: white;
            left: 0.48rem;
            position: absolute;
            bottom: 0;
        }
    }

</style>

說明

通過給屏幕點擊事件判斷是否需要隱藏下拉菜單

document.onclick = () => {
                if (this.isShow) {
                    this.isShow = false;
                }
            };

下拉菜單的點擊事件需要使用@click.stop阻止冒泡。

@click.stop="didClickMenus"

下面這個是li的下劃線

.item-normal::after
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML標準。 注意:講述HT...
    kismetajun閱讀 28,774評論 1 45
  • vue概述 在官方文檔中,有一句話對Vue的定位說的很明確:Vue.js 的核心是一個允許采用簡潔的模板語法來聲明...
    li4065閱讀 7,593評論 0 25
  • (續(xù)jQuery基礎(chǔ)(1)) 第5章 DOM節(jié)點的復(fù)制與替換 (1)DOM拷貝clone() 克隆節(jié)點是DOM的常...
    凜0_0閱讀 1,499評論 0 8
  • 阿櫻桃小丸子閱讀 443評論 0 0
  • 情竇懵懂 妝點顏容 面紅心動 情有獨鐘 諾言深重 患難與共 感情漸濃 老婆老公 某日失控 水乳交融 感情漸溶 耐性...
    夾餡鍋鍋閱讀 329評論 0 0

友情鏈接更多精彩內(nèi)容