用vue写一个京东底部导航栏,实现点击变色的样式
时间: 2023-04-11 16:01:26 浏览: 438
好的,这个问题可以回答。您可以使用Vue的v-bind:class指令来实现点击变色的样式。首先,在data中定义一个变量来存储当前选中的导航栏项的索引,然后在模板中使用v-for指令循环渲染导航栏项,并使用v-bind:class指令绑定样式类。当导航栏项被点击时,使用@click事件触发一个方法来更新当前选中的导航栏项的索引,从而改变样式类的绑定。
相关问题
用vue代码写一个京东底部导航栏,实现点击变色的样式,并给出完整代码
以下是用Vue代码实现京东底部导航栏点击变色的样式的完整代码:
<template>
<div class="nav-bar">
<div class="nav-item" :class="{active: activeIndex === 0}" @click="activeIndex = 0">
<i class="iconfont icon-home"></i>
<span>首页</span>
</div>
<div class="nav-item" :class="{active: activeIndex === 1}" @click="activeIndex = 1">
<i class="iconfont icon-category"></i>
<span>分类</span>
</div>
<div class="nav-item" :class="{active: activeIndex === 2}" @click="activeIndex = 2">
<i class="iconfont icon-cart"></i>
<span>购物车</span>
</div>
<div class="nav-item" :class="{active: activeIndex === 3}" @click="activeIndex = 3">
<i class="iconfont icon-my"></i>
<span>我的</span>
</div>
</div>
</template>
<script>
export default {
data() {
return {
activeIndex: 0
}
}
}
</script>
<style scoped>
.nav-bar {
display: flex;
justify-content: space-between;
align-items: center;
height: 50px;
background-color: #fff;
border-top: 1px solid #eee;
position: fixed;
bottom: 0;
left: 0;
right: 0;
z-index: 999;
}
.nav-item {
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-size: 12px;
color: #999;
}
.nav-item.active {
color: #f00;
}
</style>
用vue写一个手机app底部导航栏
可以使用vue-router和vue-awesome-swiper第三方库来实现一个带有底部导航栏的手机app界面。
首先,在main.js中导入vue-router和vue-awesome-swiper:
```
import Vue from 'vue'
import VueRouter from 'vue-router'
import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'swiper/dist/css/swiper.css'
Vue.use(VueRouter)
Vue.use(VueAwesomeSwiper)
```
然后,创建一个App.vue组件,包含底部导航栏和路由视图:
```
<template>
<div id="app">
<div class="main">
<router-view></router-view>
</div>
<div class="tabbar">
<router-link to="/" class="tabbar-item">
<i class="iconfont icon-home"></i>
<span>首页</span>
</router-link>
<router-link to="/category" class="tabbar-item">
<i class="iconfont icon-category"></i>
<span>分类</span>
</router-link>
<router-link to="/cart" class="tabbar-item">
<i class="iconfont icon-cart"></i>
<span>购物车</span>
</router-link>
<router-link to="/profile" class="tabbar-item">
<i class="iconfont icon-profile"></i>
<span>我的</span>
</router-link>
</div>
</div>
</template>
<script>
export default {
name: 'App'
}
</script>
<style>
.tabbar {
display: flex;
position: fixed;
bottom: 0;
left: 0;
width: 100%;
background-color: #fff;
box-shadow: 0 -2px 4px rgba(0,0,0,.15);
z-index: 99;
}
.tabbar-item {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
color: #999;
font-size: 12px;
text-decoration: none;
}
.tabbar-item.active {
color: #33a3f4;
}
.tabbar-item i {
font-size: 20px;
margin-bottom: 2px;
}
.main {
margin-bottom: 50px;
}
</style>
```
在路由配置文件中,定义每个路由对应的组件:
```
import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'
import Category from './views/Category.vue'
import Cart from './views/Cart.vue'
import Profile from './views/Profile.vue'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
component: Home
},
{
path: '/category',
component: Category
},
{
path: '/cart',
component: Cart
},
{
path: '/profile',
component: Profile
}
]
})
```
最后,在各个页面组件中,使用vue-awesome-swiper实现轮播图等效果。
以上就是一个简单的带有底部导航栏的手机app界面的实现。
阅读全文