vue实现导航栏下拉菜单
作者:凡小多 发布时间:2024-05-09 15:17:56
标签:vue,导航栏,下拉菜单
本文实例为大家分享了vue实现导航栏下拉菜单的具体代码,供大家参考,具体内容如下
先看效果:
下拉菜单铺满全屏
<div class="nav">...</div>
<div class="dropdown-content">...</div>
.nav {
?? ?position: relative;
}
.dropdown-content {
?? ?position: absolute;
?? ?width: 100%; ?// 拉满
}
下拉动画
方法一:鼠标移入移出事件
使用的是vue的 transition
组件以及鼠标事件@mouseenter
和 @mouseleave
.dropdown-enter-active {
? animation: expand-contract 1s ease;
}
.dropdown-leave-active {
? animation: expand-contract 1s ease reverse;
}
@keyframes expand-contract {
? 0% {
? ? overflow: hidden;
? ? opacity: 0;
? ? max-height: 0;
? }
? 100% {
? ? max-height: 300px; ?// 大于等于下拉菜单的高度
? ? opacity: 1;
? }
}
优点:
1、结构层次清楚
2、多个导航需要下拉菜单,且结构相似内容不同,只需要重新渲染数据即可。
缺点:
1、使用了事件处理相对复杂
案例代码
<template>
? <div class="app-container">
? ? <!-- 导航栏 -->
? ? <div class="nav" ref="navRef">
? ? ? <div class="nav-item" @mouseenter="isShow = false">导航栏1</div>
? ? ? <div class="nav-item" @mouseenter="showDropDown('2')">导航栏2</div>
? ? ? <div class="nav-item" @mouseenter="showDropDown('3')">导航栏3</div>
? ? ? <div class="nav-item" @mouseenter="isShow = false">导航栏4</div>
? ? ? <div class="nav-item" @mouseenter="isShow = false">导航栏5</div>
? ? </div>
? ? <!-- 下拉菜单 -->
? ? <transition name="dropdown">
? ? ? <div v-show="isShow" class="dropdown-content" @mouseleave="hideDropDown">
? ? ? ? <div class="dropdown-menu">
? ? ? ? ? <div class="menuItem" v-for="(item, index) in analog" :key="index">
? ? ? ? ? ? {{ item }}
? ? ? ? ? </div>
? ? ? ? </div>
? ? ? </div>
? ? </transition>
? </div>
</template>
<script>
export default {
? data() {
? ? return {
? ? ? isShow: false,
? ? ? navTop: 0,
? ? ? // 模拟下拉菜单内容
? ? ? analog: [],
? ? };
? },
? mounted() {
? ? // 导航栏距页面高度 = 元素顶部距页面距离 + 元素本身高度
? ? this.navTop =
? ? ? this.$refs.navRef.getBoundingClientRect().top +
? ? ? this.$refs.navRef.offsetHeight;
? },
? methods: {
? ? showDropDown(val) {
? ? ? if (!this.isShow) this.isShow = true;
? ? ? if (val === "2") {
? ? ? ? this.analog = ["菜单1", "菜单1", "菜单1", "菜单1", "菜单1"];
? ? ? } else {
? ? ? ? this.analog = ["菜单22", "菜单22", "菜单22", "菜单22", "菜单22"];
? ? ? }
? ? },
? ? hideDropDown(e) {
? ? ? // e.pageY:鼠标指针相对页面的偏移量
? ? ? if (this.isShow && e.pageY >= this.navTop) this.isShow = false;
? ? },
? },
};
</script>
<style lang="scss" scoped>
// 下拉菜单收缩展开
@keyframes expand-contract {
? 0% {
? ? opacity: 0;
? ? height: 0;
? ? // max-height: 0;
? }
? 100% {
? ? opacity: 1;
? ? height: 300px;
? ? // max-height: 300px; ?// 大于等于下拉菜单的高度
? }
}
.dropdown-enter-active {
? animation: expand-contract 0.6s;
}
.dropdown-leave-active {
? animation: expand-contract 0.6s reverse;
}
// 内容变化
@keyframes menu {
? 0% {
? ? opacity: 0;
? }
? 100% {
? ? opacity: 1;
? }
}
// 导航栏
.nav {
? position: relative;
? display: flex;
? width: 100%;
? height: 80px;
? line-height: 80px;
? background-color: #eee;
? border-bottom: 1px solid #ccc;
? .nav-item {
? ? position: relative;
? ? margin: 0 20px;
? ? cursor: pointer;
? ? transition: all 0.3s linear;
? ? &::before {
? ? ? content: "";
? ? ? position: absolute;
? ? ? bottom: 0;
? ? ? left: 0;
? ? ? height: 2px;
? ? ? width: 100%;
? ? ? background-color: #1678e9;
? ? ? transform: scale(0);
? ? ? transition: all 0.4s linear;
? ? }
? ? &:hover {
? ? ? color: #1678e9;
? ? ? &::before {
? ? ? ? transform: scale(1);
? ? ? }
? ? }
? }
}
.dropdown-content {
? position: absolute;
? width: 100%; // 拉满
? overflow: hidden;
? .dropdown-menu {
? ? padding: 10px 8px 15px;
? ? color: white;
? ? background-color: rgba($color: #ccc, $alpha: 0.5);
? ? border-radius: 4px;
? ? /* animation: menu 0.6s; */
? ? .menuItem {
? ? ? width: 100%;
? ? ? white-space: nowrap;
? ? ? padding: 10px 16px;
? ? ? font-size: 16px;
? ? ? color: #000;
? ? ? cursor: pointer;
? ? ? transition: all 0.3s;
? ? ? border-radius: 4px;
? ? ? &:hover {
? ? ? ? background-color: #ccc;
? ? ? }
? ? }
? }
}
</style>
方法二:hover
将下拉菜单需要下拉的导航栏下一级下,使用hover
控制元素,nav-item
不要设置相对定位,以免定位时下拉菜单宽度不能100%铺满导航栏宽度。
将菜单初始高度设为0
优点:
1、简单明了,不需要事件,js等操作
缺点:
1、每个下拉菜单独立,也就是说切换导航栏,下拉菜单显示隐藏也会动画堆叠
2、每个导航标题都需要单独写下拉菜单,结构层次变多
案例代码
<template>
? <div class="app-container">
? ? <!-- 导航栏 -->
? ? <div class="nav">
? ? ? <div class="nav-item"><span class="nav-item-title">导航栏1</span></div>
? ? ? <div class="nav-item">
? ? ? ? <span class="nav-item-title">导航栏2</span>
? ? ? ? <!-- 下拉菜单 -->
? ? ? ? <div class="dropdown-content">
? ? ? ? ? <div class="dropdown-menu">
? ? ? ? ? ? <div class="menuItem">菜单1</div>
? ? ? ? ? ? <div class="menuItem">菜单菜单1</div>
? ? ? ? ? ? <div class="menuItem">菜单2</div>
? ? ? ? ? ? <div class="menuItem">菜单菜单菜单1</div>
? ? ? ? ? ? <div class="menuItem">菜单3</div>
? ? ? ? ? </div>
? ? ? ? </div>
? ? ? </div>
? ? ? <div class="nav-item"><span class="nav-item-title">导航栏3</span></div>
? ? ? <div class="nav-item">
? ? ? ? <span class="nav-item-title">导航栏4</span>
? ? ? ? <!-- 下拉菜单 -->
? ? ? ? <div class="dropdown-content">
? ? ? ? ? <div class="dropdown-menu">
? ? ? ? ? ? <div class="menuItem">菜单1</div>
? ? ? ? ? ? <div class="menuItem">菜单菜单1</div>
? ? ? ? ? ? <div class="menuItem">菜单2</div>
? ? ? ? ? ? <div class="menuItem">菜单菜单菜单1</div>
? ? ? ? ? ? <div class="menuItem">菜单3</div>
? ? ? ? ? </div>
? ? ? ? </div>
? ? ? </div>
? ? ? <div class="nav-item"><span class="nav-item-title">导航栏5</span></div>
? ? </div>
? </div>
</template>
<script>
export default {
? data() {
? ? return {
? ? ? isShow: false,
? ? };
? },
? mounted() {},
? methods: {},
};
</script>
<style lang="scss" scoped>
.nav {
? position: relative;
? display: flex;
? width: 100%;
? height: 80px;
? line-height: 80px;
? background-color: #eee;
? border-bottom: 1px solid #ccc;
? .nav-item {
? ? // position: relative;
? ? margin: 0 20px;
? ? cursor: pointer;
? ? transition: all 0.3s linear;
? ? .nav-item-title {
? ? ? position: relative;
? ? ? display: block;
? ? ? height: inherit;
? ? ? width: inherit;
? ? ? &::before {
? ? ? ? content: "";
? ? ? ? position: absolute;
? ? ? ? bottom: 0;
? ? ? ? left: 0;
? ? ? ? height: 2px;
? ? ? ? width: 100%;
? ? ? ? background-color: #1678e9;
? ? ? ? transform: scale(0);
? ? ? ? transition: all 0.4s linear;
? ? ? }
? ? ? &:hover {
? ? ? ? color: #1678e9;
? ? ? ? &::before {
? ? ? ? ? transform: scale(1);
? ? ? ? }
? ? ? }
? ? }
? ? &:hover .dropdown-content {
? ? ? height: 300px;
? ? }
? }
? // 下拉菜单
? .dropdown-content {
? ? position: absolute;
? ? top: 80px; // 为导航栏高度
? ? left: 0; // 设置为0, 不然会直接定位到父元素下方
? ? width: 100%;
? ? height: 0; // 下拉初始高度
? ? overflow: hidden;
? ? transition: 0.6s;
? ? .dropdown-menu {
? ? ? padding: 10px 8px 15px;
? ? ? color: white;
? ? ? background-color: rgba($color: #ccc, $alpha: 0.5);
? ? ? border-radius: 4px;
? ? ? .menuItem {
? ? ? ? width: 100%;
? ? ? ? height: 42px;
? ? ? ? white-space: nowrap;
? ? ? ? padding: 0 16px;
? ? ? ? font-size: 16px;
? ? ? ? line-height: 42px;
? ? ? ? color: #000;
? ? ? ? cursor: pointer;
? ? ? ? transition: all 0.3s ease-in-out;
? ? ? ? border-radius: 4px;
? ? ? ? &:hover {
? ? ? ? ? background-color: #ccc;
? ? ? ? }
? ? ? }
? ? }
? }
}
</style>
来源:https://blog.csdn.net/wgh4318/article/details/126426602


猜你喜欢
- 将数据库中的数据保存在excel文件中有很多种方法,这里主要介绍pyExcelerator的使用。一、前期准备(不详细介绍MySQL)pyt
- <?php /** +------------------------------------------------ * 通用的树型
- PHP5.4才支持JSON_UNESCAPED_UNICODE这个参数,此参数是让中文字符在json_encode的时候不用转义,减少数据传
- 在XHTML标签中有一些标签的作用是相似的,当然这里的相似是指语义相似,以至于很多人都不清楚这些相似的标签如何使用,那么今天的主题就是分解相
- 线程实现Python中线程有两种方式:函数或者用类来包装线程对象。threading模块中包含了丰富的多线程支持功能:threading.c
- python的时间模块生成时间戳的方法是非常简单的,因为最近频繁用到了时间戳功能,这里简单总结了一下日常使用最为频繁的两个时间模块各自生成当
- 在调用后端接口时,由于后端接口的不规范统一,接口最外层在没有数据时返回的是空数组(其实更想要的是空json对象),而在有数据时返回的是jso
- 在当今用户的显示器越来越大的今天,之前的1024*768固宽布局有点越来越不合时宜,对大屏幕的用户而言,两侧空空的留白给人第一眼的印象是严重
- 本文为大家分享了Win中安装mysql的详细步骤,供大家参考,具体内容如下mysql下载目录选择免安装版“Windows (x86, 64-
- Numpy是高性能科学计算和数据分析的基础包,里面包含了许多对数组进行快速运算的标准数学函数,掌握这些方法,能摆脱数据处理时的循环。1.首先
- 近些年随着Python语言越来越流行,越来越多的人选择Python语言作为自己的职业方向。如何在心仪公司的面试中获得好成绩,并最终成功获得o
- 1.1 简介开启慢查询日志,可以让MySQL记录下查询超过指定时间的语句,通过定位分析性能的瓶颈,才能更好的优化数据库系统的性能。1.2 登
- 偶然将想到的一个如何判断鼠标从哪个方向进入一个容器的问题。首先想到的是给容器的四个边添加几块,然后看鼠标进入的时候哪个块先监听到鼠标事件。不
- 本文实例讲述了Go语言中的range用法。分享给大家供大家参考。具体如下:for 循环的 range 格式可以对 slice 或者 map
- 几个方式(本文不作介绍),要将Session保存到SQL Server中,需要有以下几个步骤:1.首先要创建用于保存Session数据的数据
- 如何发送一个XMLHttpRequest的检索的特定部分HTML标题数据。<html> <head> <tit
- 一、网络请求在uni中可以调用uni.request方法进行请求网络请求需要注意的是:在小程序中网络相关的 API 在使用前需要配置域名白名
- 源码: 代码如下: <% '隐藏并修改文件的最后修改时间的aspshell '原理:通过FSO可以修改文件的属性,比
- 我们熟悉了对象和类的基本概念。我们将进一步拓展,以便能实际运用对象和类。调用类的其它信息上一讲中提到,在定义方法时,必须有self这一参数。
- numpy多维数组的创建多维数组(矩阵ndarray)ndarray的基本属性shape维度的大小ndim维度的个数dtype数据类型1.1