一、行内样式
(1):class=“{}”
<div
v-for="(item, index) in dataList"
:key="item.name + index"
class="sitem"
:class="{
'is-active': curName === item.name,
}"
></div>
(2):class=“[]”
<div
v-for="(item, index) in dataList"
:key="item.name + index"
:class="[
'sitem',
curName === item.name ? 'is-active' : '',
]"
></div>
(3):style=“{}”
<div
v-for="(item, index) in dataList"
:key="item.name + index"
:style="{
color: item.mColor,
'margin-top': '10px'
}"
></div>
二、js写法
(1)class
<div
v-for="(item, index) in dataList"
:key="item.name + index"
class="sitem"
:id="'div_' + item.station_code"
@click="clickPort(item)"
></div>
clickPort(sitem) {
let div = document.querySelector('#div_' + sitem.station_code); //id
if (div.classList.contains('isactive')) {
div.classList.remove('isactive');
//
} else {
div.classList.add('isactive');
//
}
}
(2)style
中间带 ” - ” 的用小驼峰命名就可以了。
clickBlock(i, j) {
let div = document.getElementById('block'+i+'-'+j);
if (div.style.backgroundColor==='rgb(159, 236, 231)') {
div.style.backgroundColor='rgb(159, 236, 197)';
} else {
div.style.backgroundColor='rgb(159, 236, 231)';
}
}
// setProperty()
// removeProperty() 自行百度
三、改变style标签内样式
<script>
export default {
props: {
zIndex: {
default: 999,
type: Number,
},
},
mounted() {
this.$refs.UI.style.setProperty('--zIndex', this.zIndex);
},
watch: {
zIndex(val) {
//, old) {
this.$el.style.setProperty('--zIndex', val);
},
},
}
</script>
<style lang="less" scoped>
.dialog1 {
width: 81.8rem;
height: 32rem;
position: relative;
z-index: var(--zIndex);
}
</style>
没必要,用 一(3)就好了。