8. vue3中类的操作方式

<script setup>
// This starter template is using Vue 3 <script setup> SFCs
// Check out https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup
import HelloWorld from './components/HelloWorld.vue'
</script>

<template>
  <!--类class-->

  <div>
  <!--第一种写法,放置字符串-->
  <h1 :class="msg">hello</h1>
  <!--第二种写法,放置对象. active:是对象属性  isActive: 是变量-->
  <!--下面的作用是控制这个类是否生效-->
  <h1 :class="{active:isActive}">hello2</h1>
  <button @click="toggleActive">切换active</button>
  <!--第三种写法,放置数组-->
  <!--<h1 :class="['swiper','active']">hello3</h1>-->
  <h1 :class=arr>hello3</h1>
  <!--第四种写法,数组和对象结合-->
  <h1 :class="[classname4,{active:isActive}]">hello4</h1>
  </div>
</template>

<script>
//命令式
//document.querySelector("h1").innerHTML="helloworld"
//声明式, 先声明出变量。
export default{
  name:'App',
  data(){
    return{
      msg:"helloworld",
      isActive:true,
      arr:['swiper','active'],
      classname4:"abc"
    }
  },
  methods:{
    toggleActive(){
      this.isActive= !this.isActive,
      //测试点击后删除数组最后一个元素
      this.arr.pop()
      this.classname4="cba"
    }
  }
}
</script>

<style>
/* 表示类名 */
.active{
  background: yellowgreen;
}
</style>
<template>
  <!--style-->
  <!--第一种写法,放置字符串,背景色变成了黄色-->
  <div>
  <h1 :style="msg">hello</h1>
  <!--第二种写法,放置对象. {}表示里面是对象。background:是对象属性  purple: 是变量值-->
  <!--下面的作用是控制这个类是否生效-->
  <h1 :style="{background:'purple'}">hello2</h1>
   <!--如果有多个属性-->
  <h1 :style="styleObj">hello3</h1>
  <!--第四种写法,数组和对象的结合-->
  <h1 :style="[styleObj,{width:'500px'}]">hello4</h1>
  <!--切换样式-->
  <button @click="toggleActive">切换active</button>
  </div>
</template>

<script>
//命令式
//document.querySelector("h1").innerHTML="helloworld"
//声明式, 先声明出变量。
export default{
  name:'App',
  data(){
    return{
      msg:"background:yellow;",
      isActive:true,
      arr:['swiper','active'],
      classname4:"abc",
      styleObj:{
        // 如果遇到多个单词组合可使用引号 'background-color'
        // 也可使用驼峰的命名
        background : 'pink',
        border:'1px solid blue',
        boxSizing:"border-box"
      }
    }
  },
  methods:{
    toggleActive(){
      this.styleObj.backgroundColor = "skyblue"
    }
  }
}
</script>

<style>
/* 表示类名 */
.active{
  background: yellowgreen;
}
</style>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值