Vue样式绑定

1. 绑定 HTML class
①通过class名称的bool值判断样式是否被启用

<template>
  <!--通过样式名称是否显示控制样式-->
  <div :class="{ haveBorder: p.isBorder, 'haveBackground-color': p.isBackgroundcolor }">此处是样式展示区域</div>
  <br />
  <button @click="addBorder">增加边框</button>
  <br />
  <button @click="addBackgroundcolor">增加背景颜色</button>
</template>
<script setup>
//从vue中获取ref方法
import {
      reactive } from "vue";

name: "App";
//利用v-bind的bool值控制class调用的样式名称是否显示
let p = reactive({
     
  isBorder: false,
  isBackgroundcolor: false,
});
function addBorder() {
     
  p.isBorder = true;
}
function addBackgroundcolor() {
     
  p.isBackgroundcolor = true;
}
</script>

<style scoped>
.haveBorder {
     
  border: 1px solid #000000;
}
.haveBackground-color {
     
  background-color: aqua;
}
</style>

在这里插入图片描述
②样式名称在对象中,html中调用定义的对象

<template>
  <!--通过样式名称是否显示控制样式-->
  <div :class="classObject ">此处是样式展示区域</div>
  <br />
  <button @click="addBorder">增加边框</button>
</template>
<script setup>
//从vue中获取ref方法
import {
      reactive } from "vue";

name: "App";
//利用bool值控制class调用的样式名称是否显示(样式设置成对象)
let classObject  = reactive({
     
  haveBorder: false,
});
function addBorder() {
     
  classObject.haveBorder = true;
}
</script>

<style scoped>
.haveBorder {
     
  border: 1px solid #000000;
}
</style>

在这里插入图片描述
③通过数组绑定

<template>
  <!--通过样式名称是否显示控制样式-->
  <div :class="[arrayBorder,arrayBackgroundColor]">此处是样式展示区域</div>
</template>
<script setup>
//从vue中获取ref方法
import {
      ref } from "vue";

name: "App";
//利用数组绑定样式
let arrayBorder=ref('haveBorder')
let arrayBackgroundColor=ref('haveBackground-color')
</script>

<style scoped>
.haveBorder {
     
  border: 1px solid #000000;
}
.haveBackground-color {
     
  background-color: aqua;
}
</style>

在这里插入图片描述
④在组件上使用
父组件

<template>
  <!--通过样式名称是否显示控制样式-->
  <classtest :class="[arrayBorder, arrayBackgroundColor]" />
</template>
<script >
//从vue中获取ref方法
import {
      ref } from "vue";
import classtest from "./components/classtest.vue";

export default {
     
  name: "App",
  components:{
     
    classtest
  },
  //利用数组绑定样式
  setup() {
     
    let arrayBorder = ref("haveBorder");
    let arrayBackgroundColor = ref("haveBackground-color");

    return{
     
      arrayBorder,
      arrayBackgroundColor
    }
  },
};
</script>

<style scoped>

</style>

子组件

<template>
  <!--因为有多个根元素所以使用$attrs属性实现指定接受父组件样式-->
  <!--多个根元素情况使用父组件传入的样式名称,需在子组件定义样式-->
  <div :class="$attrs.class">此处是样式展示区域</div>
  <div>此处不接受父组件传过来的class</div>
</template>

<script>
export default {
     
 name:'classtest'
}
</script>

<style>
.haveBorder {
     
  border: 1px solid #000000;
}
.haveBackground-color {
     
  background-color: aqua;
}
</style>

在这里插入图片描述

2. 绑定内联样式

<template>
  <div :style="{ border: borderStyle, 'background-color': backgroundcolorStyle }">此处是样式展示区域</div>
  <br>
  <div :style="{styleObject , 'font-size':fontSize + 'px'}">此处是样式展示区域</div>
  <br>
  <!--你可以对一个样式属性提供多个 (不同前缀的) 值-->
  <div :style="{ display: ['-webkit-box', '-ms-flexbox', 'flex'] }"></div>
</template>
<script setup>
//从vue中获取ref方法
import {
      ref,reactive } from "vue";

name: "App";
//利用数组绑定样式
let borderStyle=ref('1px solid #000000')
let backgroundcolorStyle=ref('aqua')

//定义对象形式style
let styleObject =reactive({
     
  border:'1px solid #000000'
})

//定义字体大小
let fontSize=ref(30)
</script>

<style scoped>

</style>

在这里插入图片描述

相关推荐

  1. Vue class样式

    2024-02-23 13:41:17       15 阅读
  2. vue--样式--样式切换方法

    2024-02-23 13:41:17       11 阅读
  3. Vuevue中动态样式

    2024-02-23 13:41:17       13 阅读
  4. vue样式动态写法

    2024-02-23 13:41:17       29 阅读
  5. vue3-类与样式

    2024-02-23 13:41:17       28 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-23 13:41:17       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-23 13:41:17       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-23 13:41:17       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-23 13:41:17       18 阅读

热门阅读

  1. 力扣热题100_子串_560_和为 K 的子数组

    2024-02-23 13:41:17       30 阅读
  2. 爬虫基本库的使用(httpx库的详细解析)

    2024-02-23 13:41:17       29 阅读
  3. CAN Linux C应用编程

    2024-02-23 13:41:17       18 阅读
  4. 2024.2.22

    2024-02-23 13:41:17       21 阅读
  5. idea打开项目白屏

    2024-02-23 13:41:17       34 阅读