Vue 实现一个下拉菜单

前言

有时候我们需要去自定一个下拉菜单,这个时候就得自己撸css了

先来看demo


demo.gif

实现

其他的不相关的代码,这里就删掉了

<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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,935评论 1 45
  • vue概述 在官方文档中,有一句话对Vue的定位说的很明确:Vue.js 的核心是一个允许采用简洁的模板语法来声明...
    li4065阅读 7,297评论 0 25
  • (续jQuery基础(1)) 第5章 DOM节点的复制与替换 (1)DOM拷贝clone() 克隆节点是DOM的常...
    凛0_0阅读 1,411评论 0 8
  • 阿樱桃小丸子阅读 435评论 0 0
  • 情窦懵懂 妆点颜容 面红心动 情有独钟 诺言深重 患难与共 感情渐浓 老婆老公 某日失控 水乳交融 感情渐溶 耐性...
    夹馅锅锅阅读 283评论 0 0