当前位置:首页 » 《随便一记》 » 正文

前端Vue日常工作中--动态css类

22 人参与  2024年02月14日 15:06  分类 : 《随便一记》  评论

点击全文阅读


前端Vue日常工作中–动态css类

文章目录

前端Vue日常工作中--动态css类1.普通应用1.1动态 CSS 类1.2动态内联样式 2.组件中应用(父子组件)2.1动态绑定CSS类名在父组件中:在子组件中: 2.2动态绑定内联样式在父组件中:在子组件中:

1.普通应用

Vue.js 允许你在动态地设置 CSS 类、样式等方面非常灵活。

假设你有一个 Vue 组件,其中有一个数据属性 isStyled,你想根据这个属性动态地改变元素的样式。

1.1动态 CSS 类

<template>  <div :class="{ 'styled': isStyled }">    <p>这是一个动态样式的例子</p>  </div>  <button @click="toggleStyle">切换样式</button></template><script>export default {  data() {    return {      isStyled: false,    };  },  methods: {    toggleStyle() {      this.isStyled = !this.isStyled;    },  },};</script><style>.styled {  color: red;  font-weight: bold;}</style>

isStyledtrue 时,.styled 类将被应用,从而改变文字的颜色和粗细。点击按钮会调用 toggleStyle 方法,切换 isStyled 的值,从而动态改变样式。

1.2动态内联样式

<template>  <div :style="{ color: isStyled ? 'red' : 'black', fontWeight: isStyled ? 'bold' : 'normal' }">    <p>这是另一个动态样式的例子</p>  </div>  <button @click="toggleStyle">切换样式</button></template><script>export default {  data() {    return {      isStyled: false,    };  },  methods: {    toggleStyle() {      this.isStyled = !this.isStyled;    },  },};</script>

使用了三元运算符 isStyled ? 'red' : 'black'isStyled ? 'bold' : 'normal',根据 isStyled 的值动态地设置文字的颜色和粗细。点击按钮会调用 toggleStyle 方法,切换 isStyled 的值,从而动态改变样式。

2.组件中应用(父子组件)

在Vue.js中,你可以通过动态地绑定CSS类名或内联样式来实现动态CSS。这可以在父子组件之间进行通信,使得子组件可以根据父组件的状态或属性来更新自身的样式。

2.1动态绑定CSS类名

在父组件中:
<template>  <div :class="{ 'father-style': isStyled }">    <child-component :isStyled="isStyled"></child-component>  </div></template><script>import ChildComponent from './ChildComponent.vue';export default {  components: {    ChildComponent,  },  data() {    return {      isStyled: true, // 或者根据其他条件来动态改变    };  },};</script><style>.father-style {  color: red;  font-weight: bold;}</style>
在子组件中:
<template>  <div :class="{ 'child-style': isStyled }">    <!-- 子组件的内容 -->  </div></template><script>export default {  props: {    isStyled: Boolean,  },};</script><style scoped>.child-style {  color: red;  font-weight: bold;}</style>

父组件使用:class绑定动态的CSS类名,根据 isStyled 的值决定是否添加 father-style 类名。子组件同样使用:class绑定动态的CSS类名,根据 isStyled 的值决定是否添加 child-style 类名。

2.2动态绑定内联样式

在父组件中:
<template>  <div :style="{ backgroundColor: highlightColor }">    <child-component :highlightColor="highlightColor"></child-component>  </div></template><script>import ChildComponent from './ChildComponent.vue';export default {  components: {    ChildComponent,  },  data() {    return {      highlightColor: 'yellow', // 或者根据其他条件来动态改变    };  },};</script>
在子组件中:
<template>  <div :style="{ backgroundColor: highlightColor }">    <!-- 子组件的内容 -->  </div></template><script>export default {  props: {    highlightColor: String,  },};</script>

父组件使用:style绑定动态的内联样式,根据 highlightColor 的值来设置背景颜色。子组件同样使用:style绑定动态的内联样式,根据 highlightColor 的值来设置背景颜色。


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/68479.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 给你的第三封信是遗言沈佳芮顾温言_给你的第三封信是遗言沈佳芮顾温言列表
  • 闻砚辞阮雾梨(许是曾经有相思人面桃花长相忆+番外)_(闻砚辞阮雾梨)许是曾经有相思人面桃花长相忆+番外列表_笔趣阁(闻砚辞阮雾梨)
  • 男友让我给白月光顶罪后我杀疯了人气小说未删减节选_苏泽裴婉婷王希谣全文+后续
  • 慕晚夏顾砚廷顾砚廷
  • 给你的第三封信是遗言沈佳芮顾温言结局+番外全书+后续+结局(顾温言沈佳芮)列表_给你的第三封信是遗言沈佳芮顾温言结局+番外(顾温言沈佳芮)给你的第三封信是遗言沈佳芮顾温言结局+番外全书+后续+结局在线
  • 佛心不渡红尘劫结局+番外(云狸越渊)全书云狸越渊结局_云狸越渊+结局列表_笔趣阁(佛心不渡红尘劫结局+番外)云狸越渊彩蛋(佛心不渡红尘劫结局+番外)附加+结局(云狸越渊)全文速览
  • (余烬下的思念全书+后续+结局)分享(沈夏里顾禾州)_(余烬下的思念全书+后续+结局)列表_笔趣阁(沈夏里顾禾州)
  • 全文上岸后被闺蜜背刺,重生我杀疯了(乔知微)列表_全文上岸后被闺蜜背刺,重生我杀疯了
  • 「九转金身炸裂异界!反派竟是我分身?」小说节选免费试读_「方叶」限时免费完整章节
  • 小透明逆袭误撩后被大佬宠上天/大佬出门记得装不熟(许惟昭方肃礼)_小透明逆袭误撩后被大佬宠上天/大佬出门记得装不熟许惟昭方肃礼
  • 顾砚廷,慕晚夏_顾砚廷,慕晚夏列表
  • 阴阳车番外特辑限免解锁_[王成]小说后续在线免费阅读

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1