Vue.js 动态导航栏和内容区域
<template>
<view>
<!-- 顶部导航栏 -->
<view class='af-head'>
<span class='af-head-msg' style='font-size: 60rpx;'><</span>
<span class='af-head-msg af-head-fuc'>全部功能</span>
</view>
<!-- 左侧导航栏 -->
<view class='sidebar'>
<view v-for='item in menuItems' :key='item.id' @click='selectMenuItem(item.id)'
:class='{ active: activeMenuItem === item.id }'>
{{ item.name }}
</view>
</view>
<!-- 显示内容 -->
<view class='content' v-if='activeMenuItem !== null'>
<view v-for='feature in menuItems[activeMenuItem - 1].features' :key='feature.id'>
<image :src='feature.icon' class='feature-icon' />
<span>{{ feature.name }}</span>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
activeMenuItem: null, // 当前选中的菜单项ID
menuItems: [
{
id: 1,
name: '菜单项1',
features: [
{ id: 1, name: '功能1', icon: 'icon1.png' },
{ id: 2, name: '功能2', icon: 'icon2.png' }
]
},
{
id: 2,
name: '菜单项2',
features: [
{ id: 3, name: '功能3', icon: 'icon3.png' },
{ id: 4, name: '功能4', icon: 'icon4.png' }
]
},
{
id: 3,
name: '菜单项3',
features: [
{ id: 5, name: '功能5', icon: 'icon5.png' },
{ id: 6, name: '功能6', icon: 'icon6.png' }
]
}
]
};
},
methods: {
selectMenuItem(itemID) {
this.activeMenuItem = itemID;
}
}
};
</script>
<style>
.af-head {
position: relative;
background-color: #629ffc;
height: 100rpx;
display: flex;
justify-content: center;
font-size: 44rpx;
font-weight: 500;
font-family: sans-serif;
}
.af-head-msg {
position: absolute;
bottom: 16rpx;
color: white;
left: 38rpx;
}
.af-head-fuc {
text-align: center;
left: 300rpx;
}
.sidebar {
width: 100px;
height: 100vh;
padding-top: 20rpx;
}
.sidebar view {
padding: 10rpx;
color: black;
list-style-type: none;
display: block;
margin-top: 10px;
cursor: pointer;
transition: background-color 0.3s;
}
.sidebar view.active {
border-left: 4px solid #629ffc;
background-color: white;
}
.content {
margin-left: 120px; /* 导航栏宽度加上间距 */
}
.feature-icon {
width: 20px;
height: 20px;
margin-right: 5px;
}
</style>
原文地址: https://www.cveoy.top/t/topic/RXQ 著作权归作者所有。请勿转载和采集!