Vue.js & CSS: 创建带悬停下拉菜单的网站导航栏
<template>
<div class='navbar'>
<!-- 左侧区域 -->
<div class='logo'>
<img src='./logo.png' alt='logo'>
</div>
<!-- 右侧区域 -->
<div class='nav-list'>
<div class='nav-item' @mouseover='showBox('intro')' @mouseleave='hideBox'>
<span>简介</span>
<div class='box' v-show='showIntro'>
<p>这是简介内容</p>
<p>这是简介内容</p>
<p>这是简介内容</p>
<p>这是简介内容</p>
</div>
</div>
<div class='nav-item' @mouseover='showBox('news')' @mouseleave='hideBox'>
<span>新闻中心</span>
<div class='box' v-show='showNews'>
<p>这是新闻中心内容</p>
<p>这是新闻中心内容</p>
<p>这是新闻中心内容</p>
<p>这是新闻中心内容</p>
</div>
</div>
<div class='nav-item' @mouseover='showBox('rules')' @mouseleave='hideBox'>
<span>制度公告</span>
<div class='box' v-show='showRules'>
<p>这是制度公告内容</p>
<p>这是制度公告内容</p>
<p>这是制度公告内容</p>
<p>这是制度公告内容</p>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
showIntro: false, // 是否显示简介内容
showNews: false, // 是否显示新闻中心内容
showRules: false // 是否显示制度公告内容
};
},
methods: {
// 显示对应的盒子
showBox(type) {
switch (type) {
case 'intro':
this.showIntro = true;
break;
case 'news':
this.showNews = true;
break;
case 'rules':
this.showRules = true;
break;
}
},
// 隐藏所有盒子
hideBox() {
this.showIntro = false;
this.showNews = false;
this.showRules = false;
}
}
};
</script>
<style>
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
background-color: #fff;
height: 60px;
padding: 0 20px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12);
}
.logo {
width: 40%;
}
.logo img {
width: 100%;
}
.nav-list {
width: 60%;
display: flex;
justify-content: space-between;
}
.nav-item {
position: relative;
cursor: pointer;
}
.nav-item span {
font-size: 16px;
}
.box {
position: absolute;
top: 100%;
left: 0;
width: 200px;
background-color: #fff;
padding: 10px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12);
display: none;
}
.box p {
margin: 0;
font-size: 14px;
line-height: 1.5;
}
.box p + p {
margin-top: 10px;
}
.box:before {
content: "";
position: absolute;
top: -10px;
right: 10px;
border: 5px solid transparent;
border-bottom-color: #fff;
}
</style>
原文地址: https://www.cveoy.top/t/topic/lNl1 著作权归作者所有。请勿转载和采集!