扫码关注官方订阅号
我正在尝试创建一个导航侧边栏。会有主项目,也会有子项目。
我尝试仅在单击父项目时显示子项目,并且当我单击子项目时,我希望活动子项目具有不同的颜色。我如何实现这一目标?
这是我到目前为止所尝试过的。
{{ link.text }} //I want it to show only when the parent item is clicked //trying to add the sub-active class but it's not working {{ link.text }}
尝试像下面的代码片段(您在嵌套链接中错过了另一个 ul,然后只需使用列表索引切换显示/隐藏导航):
ul
new Vue({ el: "#demo", data() { return { navLinks: [ {text: 'Contact', path: '/contact', sublinks: [{ text: 'Email', path: '/email',},], }, {text: 'About', path: '/about',}, ], belowLinks: [ {text: 'Blog', path: '/blog',}, {text: 'Portfolio', path: '/portfolio',}, ], show: null, active: null } }, methods: { toggleNav(i) { this.active = null this.show === i ? this.show = null : this.show = i }, setActive(i) { this.active === i ? this.active = null : this.active = i } } })
nav { height: 100vh; display: flex; flex-direction: column; background: #040521; justify-content: space-between; } ul { display: flex; align-items: center; margin-block-start: 0; margin-block-end: 0; padding-inline-start: 0; flex-direction: column; } a { text-decoration: none; display: flex; align-items: center; color: white; } a:hover { color: white; } li { list-style-type: none; padding: 10px 0px; width: 100%; } .page-link .active, .router-link-active { background-color: green; color: white !important; border-color: inherit !important; } .sub-active { background-color: yellow !important; color: white !important; border-color: inherit !important; } .items { padding: 10px 20px; } .sub-items { padding: 10px 0px 10px 40px; }
{{ link.text }} {{ link.text }}
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
尝试像下面的代码片段(您在嵌套链接中错过了另一个
ul,然后只需使用列表索引切换显示/隐藏导航):new Vue({ el: "#demo", data() { return { navLinks: [ {text: 'Contact', path: '/contact', sublinks: [{ text: 'Email', path: '/email',},], }, {text: 'About', path: '/about',}, ], belowLinks: [ {text: 'Blog', path: '/blog',}, {text: 'Portfolio', path: '/portfolio',}, ], show: null, active: null } }, methods: { toggleNav(i) { this.active = null this.show === i ? this.show = null : this.show = i }, setActive(i) { this.active === i ? this.active = null : this.active = i } } })nav { height: 100vh; display: flex; flex-direction: column; background: #040521; justify-content: space-between; } ul { display: flex; align-items: center; margin-block-start: 0; margin-block-end: 0; padding-inline-start: 0; flex-direction: column; } a { text-decoration: none; display: flex; align-items: center; color: white; } a:hover { color: white; } li { list-style-type: none; padding: 10px 0px; width: 100%; } .page-link .active, .router-link-active { background-color: green; color: white !important; border-color: inherit !important; } .sub-active { background-color: yellow !important; color: white !important; border-color: inherit !important; } .items { padding: 10px 20px; } .sub-items { padding: 10px 0px 10px 40px; }