标签:文件夹 高亮 item method 设置 control line data click
因TabControl组件在home和category页面都可以用到,所以可以在公共组件的content下创建tabControl文件夹进行封装
<template>
<div class="tab-control">
<div
v-for="(item, index) in titles"
:key="index"
class="tab-control-item"
//当索引值与currentIndex相同时,文字高亮
:class="{ active: index === currentIndex }"
@click="itemClick(index)"
>
<span>{{ item }}</span>
</div>
</div>
</template>
<script>
export default {
name: "TabControl",
props: {
titles: {
type: Array,
default() {
return [];
},
},
},
data() {
return {
currentIndex: 0,
};
},
methods: {
itemClick(index) {
this.currentIndex = index; //设置点击的span为活跃状态
},
},
};
</script>
<style>
.tab-control {
display: flex;
text-align: center;
font-size: 15px;
height: 40px;
line-height: 40px;
background-color: #fff;
}
.tab-control-item {
flex: 1;
}
.tab-control-item span {
padding: 5px;
}
.active {
color: var(--color-height-text);
}
.active span {
border-bottom: 3px solid var(--color-tint);
}
</style>
组件的导入规范
<script>
//公共组件放一起
import NavBar from "components/common/navbar/NavBar";
import TabControl from "components/content/tabControl/TabControl";
//home页面相关组件放一起
import HomeSwiper from "./childComps/HomeSwiper";
import RecommendView from "./childComps/RecommendView";
import FeatureView from "./childComps/FeatureView";
//导入方法放一起
import { getHomeMultidata } from "network/home";
export default {
name: "Home",
components: {
HomeSwiper,
RecommendView,
FeatureView,
//挂载组件规范与上述一致
NavBar,
TabControl,
},
...
TabControl停留效果实现
不要在TabControl组件里设置样式,要在Home.vue设置样式,因为只有在Home页面有这个效果
.tab-control {
display: sticky;
top: 44px;
}
supermall 项目实战 02 ->TabControl 的封装
标签:文件夹 高亮 item method 设置 control line data click
原文地址:https://www.cnblogs.com/107w/p/14835290.html