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

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

8 人参与  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