The gray outer package is the outermost div, give a class="tab-control" convenience, etc. to optimize the details
Since each tabItem item is only text, it is also wrapped in div first, give a class="tab-control-item", and directly use a simple span to occupy the first place
Start to modify the css: flex layout, the text is centered, set the height of this option bar: 40px, and set a background color, then each tabItem inside is flex:1
The number of texts and spans in each tabItem is determined by the parent component that uses this component, so the value is passed by the parent component. In the above, you know that tabItem is just a text display, so use an array to store and pass the value.
The parent component references and passes values:
The parent passes the titles array, which contains three elements ["recommendation", "recommendation", "greeting"], which also means that three tabItems and three spans will be created
Receipt of subcomponents
<template><divclass="tab-control"><divv-for="(item,index) in titles":key="index"class="tab-control-item"><span>{
{item}}</span></div></div></template><script>exportdefault{
name:'TabControl',
props:{
titles:{
type: Array,default(){
return[]}}},}</script>
The above can display the array passed from the parent component.
Click an option, the option is highlighted, first use the click event to get the index value of the item currently clicked, and then set a specific css style: active, let the option apply the active style
<template><divclass="tab-control"><divv-for="(item,index) in titles":key="index"class="tab-control-item":class="{active: index === currentIndex}"----动态绑定样式@click="ItemClick(index)"----点击事件获取index><span>{
{item}}</span></div></div></template>
Only when the currentIndex is the same as the current item index, the active style will be activated.
Ceiling effect settings: mainly the position attribute in the css style
.tab-control{
position: sticky;top: 44px;}
The selected line will change its position at a distance of 44px from the top to: fixed. Below 44px from the top, it is its active area, and its position is static.