<template> <div class="Home"> <div class="index" id="mapTree"></div> <div class="top"> <div class="topLeft"> <div v-bind:class="{ redText: buttonStyle['Rail'], }" @click="toggleComponent('Rail')" > <a>电子围栏</a> </div> <div v-bind:class="{ redText: buttonStyle['Video'], }" @click="toggleComponent('Video')" > 视频监控 </div> <div v-bind:class="{ redText: buttonStyle['Location'], }" @click="toggleComponent('Location')" > 实时定位 </div> <div v-bind:class="{ redText: buttonStyle['Monitor'], }" @click="toggleComponent('Monitor')" > 环境检测 </div> </div> <div class="topRight"> <div>2</div> <div>2</div> <div>2</div> <div>2</div> </div> </div> <Rail v-if="currentComponent === 'Rail'" /> <Video v-if="currentComponent === 'Video'" /> <Location v-if="currentComponent === 'Location'" /> <Monitor v-if="currentComponent === 'Monitor'" /> </div> </template> <script> import Rail from "@/views/rail/rail.vue"; import Video from "@/views/video/video.vue"; import Location from "@/views/location/location.vue"; import Monitor from "@/views/monitor/monitor.vue"; let viewer; Cesium.Ion.defaultAccessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJlYzc0OGZjYi03NDY5LTRmMmQtYTc2YS05ZTY2YzZlMTRmYTQiLCJpZCI6MTM5MzQ0LCJpYXQiOjE2ODQzMTI2MjF9.nNF8IyvsjDSdeRJeea8ftf4TC1DOgSa_jue-ZZ0dZ8M"; export default { name: "MineHome", components: { Rail, Video, Location, Monitor, }, data() { return { currentComponent: "Rail", currentTab: "", // 记录当前选中的按钮 buttonStyle: { // 记录按钮样式 Rail: true, Video: false, Location: false, Monitor: false, }, }; }, mounted() { this.toggleComponent("Rail"); this.initMap(); }, methods: { // 初始化地图 initMap() { viewer = new Cesium.Viewer("mapTree", { infoBox: false, // 禁用infoBox }); // 加载倾斜模型 var tileset = new Cesium.Cesium3DTileset({ url: "http://47.104.159.54:8156/out2/tileset.json", }); viewer.scene.primitives.add(tileset); viewer.zoomTo(tileset); }, toggleComponent(componentName) { this.currentComponent = componentName; }, }, }; 根据这段代码实现 默认选中按钮字体颜色为红色 非选中为白色
时间: 2024-01-03 22:28:59 浏览: 208
可以在 `buttonStyle` 对象中设置每个按钮的默认样式,然后在模板中使用 `v-bind:class` 绑定样式类即可。可以尝试以下代码:
```html
<template>
<div class="Home">
<div class="index" id="mapTree"></div>
<div class="top">
<div class="topLeft">
<div
:class="{ redText: buttonStyle['Rail'] }"
@click="toggleComponent('Rail')"
>
<a>电子围栏</a>
</div>
<div
:class="{ redText: buttonStyle['Video'] }"
@click="toggleComponent('Video')"
>
<a>视频监控</a>
</div>
<div
:class="{ redText: buttonStyle['Location'] }"
@click="toggleComponent('Location')"
>
<a>实时定位</a>
</div>
<div
:class="{ redText: buttonStyle['Monitor'] }"
@click="toggleComponent('Monitor')"
>
<a>环境检测</a>
</div>
</div>
<div class="topRight">
<div>2</div>
<div>2</div>
<div>2</div>
<div>2</div>
</div>
</div>
<Rail v-if="currentComponent === 'Rail'" />
<Video v-if="currentComponent === 'Video'" />
<Location v-if="currentComponent === 'Location'" />
<Monitor v-if="currentComponent === 'Monitor'" />
</div>
</template>
<script>
import Rail from "@/views/rail/rail.vue";
import Video from "@/views/video/video.vue";
import Location from "@/views/location/location.vue";
import Monitor from "@/views/monitor/monitor.vue";
let viewer;
Cesium.Ion.defaultAccessToken =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJlYzc0OGZjYi03NDY5LTRmMmQtYTc2YS05ZTY2YzZlMTRmYTQiLCJpZCI6MTM5MzQ0LCJpYXQiOjE2ODQzMTI2MjF9.nNF8IyvsjDSdeRJeea8ftf4TC1DOgSa_jue-ZZ0dZ8M";
export default {
name: "MineHome",
components: {
Rail,
Video,
Location,
Monitor,
},
data() {
return {
currentComponent: "Rail",
currentTab: "", // 记录当前选中的按钮
buttonStyle: {
// 记录按钮样式
Rail: true,
Video: false,
Location: false,
Monitor: false,
},
};
},
mounted() {
this.toggleComponent("Rail");
this.initMap();
},
methods: {
// 初始化地图
initMap() {
viewer = new Cesium.Viewer("mapTree", {
infoBox: false, // 禁用infoBox
});
// 加载倾斜模型
var tileset = new Cesium.Cesium3DTileset({
url: "http://47.104.159.54:8156/out2/tileset.json",
});
viewer.scene.primitives.add(tileset);
viewer.zoomTo(tileset);
},
toggleComponent(componentName) {
this.currentComponent = componentName;
// 更新按钮样式
for (let key in this.buttonStyle) {
this.buttonStyle[key] = key === componentName;
}
},
},
};
</script>
<style>
.redText {
color: red;
}
</style>
```
这里使用了 `v-bind:class` 动态绑定样式类,当 `buttonStyle` 对象中对应的属性为真时,元素会添加 `redText` 样式类,从而显示为红色字体。在 `toggleComponent` 方法中,更新当前组件并更新按钮样式。注意还需要添加 `.redText` 样式定义才能实现红色字体。
阅读全文