vue、uniapp中动态添加绑定style、class 9种方法实现

9种方法介绍

  1. 直接使用静态class和style属性:

    • 使用场景:当class和style属性是固定不变的时候,可以直接在模板中写死。
    • 优点:简单直接,没有额外的计算和逻辑。
    • 缺点:无法根据条件动态修改class和style。
  2. 使用v-bind动态绑定class和style属性:

    • 使用场景:当class和style属性需要根据组件的data或props属性动态变化时,可以使用v-bind来动态绑定。
    • 优点:可以根据条件动态修改class和style。
    • 缺点:需要在模板中写表达式,有一定的复杂度。
  3. 使用计算属性来动态生成class和style对象:

    • 使用场景:当class和style属性的计算逻辑比较复杂时,可以使用计算属性来生成class和style对象。
    • 优点:代码可读性好,逻辑清晰。
    • 缺点:需要定义额外的计算属性。
  4. 使用动态绑定的class和style属性:

    • 使用场景:当class和style属性的计算逻辑比较简单时,可以直接在模板中使用表达式来动态生成class和style字符串。
    • 优点:简洁明了,没有额外的计算属性。
    • 缺点:逻辑稍微复杂时,可读性会变差。
  5. 使用数组语法来动态绑定class属性:

    • 使用场景:当class属性需要根据多个条件动态变化时,可以使用数组语法来动态绑定class属性。
    • 优点:可以根据多个条件动态修改class。
    • 缺点:数组语法相对复杂,可读性较差。
  6. 使用动态绑定的style属性:

    • 使用场景:当style属性需要根据组件的data或props属性动态变化时,可以使用动态绑定的style属性。
    • 优点:可以根据条件动态修改style。
    • 缺点:需要在模板中写表达式,有一定的复杂度。
  7. 使用对象语法动态绑定class属性:

    • 使用场景:当class属性需要根据多个条件动态变化时,可以使用对象语法来动态绑定class属性。
    • 优点:可以根据多个条件动态修改class。
    • 缺点:对象语法相对复杂,可读性较差。
  8. 使用计算属性动态绑定style属性:

    • 使用场景:当style属性的计算逻辑比较复杂时,可以使用计算属性来动态绑定style属性。
    • 优点:代码可读性好,逻辑清晰。
    • 缺点:需要定义额外的计算属性。
  9. 使用动态绑定的class和style属性,通过在模板中使用数组和对象语法来动态生成class和style字符串:

    • 使用场景:当class和style属性需要根据多个条件动态变化时,可以使用数组和对象语法来动态绑定class和style属性。
    • 优点:可以根据多个条件动态修改class和style。
    • 缺点:语法相对复杂,可读性较差。

具体实现

在Uniapp和Vue中,可以使用以下9种方法来动态添加绑定style和class:

  1. 使用v-bind指令(或简写为:class:style)来动态绑定class和style属性。可以通过计算属性或直接在模板中使用三元表达式来根据条件动态设置class和style属性。
html 复制代码
<template>
  <div :class="{'active': isActive}" :style="{'color': textColor}">Hello World</div>
</template>

<script>
export default {
  data() {
    return {
      isActive: true,
      textColor: 'red'
    }
  }
}
</script>
  1. 使用v-bind指令(或简写为:class:style)来动态绑定class和style对象。可以在data中定义一个对象,根据条件动态设置class和style对象的属性。
html 复制代码
<template>
  <div :class="classObject" :style="styleObject">Hello World</div>
</template>

<script>
export default {
  data() {
    return {
      classObject: {
        active: true,
        'text-color': true
      },
      styleObject: {
        color: 'red',
        fontSize: '16px'
      }
    }
  }
}
</script>
  1. 使用计算属性来动态生成class和style对象。可以根据组件的data或props属性计算出class和style对象,并在模板中使用计算属性。
html 复制代码
<template>
  <div :class="computedClass" :style="computedStyle">Hello World</div>
</template>

<script>
export default {
  data() {
    return {
      isActive: true,
      textColor: 'red'
    }
  },
  computed: {
    computedClass() {
      return {
        active: this.isActive,
        'text-color': this.textColor === 'red'
      }
    },
    computedStyle() {
      return {
        color: this.textColor,
        fontSize: '16px'
      }
    }
  }
}
</script>
  1. 使用动态绑定的class和style属性,通过在模板中使用表达式来动态生成class和style字符串。
html 复制代码
<template>
  <div :class="'active ' + (isActive ? 'active' : '')" :style="'color: ' + textColor">Hello World</div>
</template>

<script>
export default {
  data() {
    return {
      isActive: true,
      textColor: 'red'
    }
  }
}
</script>
  1. 使用数组语法来动态绑定class属性。可以在data中定义一个数组,根据条件动态设置class数组的元素。
html 复制代码
<template>
  <div :class="classArray">Hello World</div>
</template>

<script>
export default {
  data() {
    return {
      isActive: true,
      isBold: true
    }
  },
  computed: {
    classArray() {
      return ['active', {'bold': this.isBold}]
    }
  }
}
</script>
  1. 使用动态绑定的style属性,通过在模板中使用对象语法来动态生成style字符串。
html 复制代码
<template>
  <div :style="{ color: textColor, fontSize: fontSize + 'px' }">Hello World</div>
</template>

<script>
export default {
  data() {
    return {
      textColor: 'red',
      fontSize: 16
    }
  }
}
</script>
  1. 使用对象语法动态绑定class属性。可以在data中定义一个对象,根据条件动态设置class对象的属性。
html 复制代码
<template>
  <div :class="classObject">Hello World</div>
</template>

<script>
export default {
  data() {
    return {
      isActive: true,
      isBold: true
    }
  },
  computed: {
    classObject() {
      return {
        active: this.isActive,
        bold: this.isBold
      }
    }
  }
}
</script>
  1. 使用计算属性动态绑定style属性。可以根据组件的data或props属性计算出style字符串,并在模板中使用计算属性。
html 复制代码
<template>
  <div :style="computedStyle">Hello World</div>
</template>

<script>
export default {
  data() {
    return {
      textColor: 'red',
      fontSize: 16
    }
  },
  computed: {
    computedStyle() {
      return `color: ${this.textColor}; font-size: ${this.fontSize}px;`;
    }
  }
}
</script>
  1. 使用动态绑定的class和style属性,通过在模板中使用数组和对象语法来动态生成class和style字符串。
html 复制代码
<template>
  <div :class="['active', { 'bold': isBold }]">Hello World</div>
  <div :style="[styleObject, { 'font-size': fontSize + 'px' }]">Hello World</div>
</template>

<script>
export default {
  data() {
    return {
      isBold: true,
      styleObject: {
        color: 'red'
      },
      fontSize: 16
    }
  }
}
</script>
相关推荐
devincob4 小时前
js原生、vue导出、react导出、axios ( post请求方式)跨平台导出下载四种方式的demo
javascript·vue.js·react.js
木头没有瓜6 小时前
在 Windows 中清理依赖node_modules并重新安装
vue.js
2501_915106326 小时前
App HTTPS 抓包 工程化排查与工具组合实战
网络协议·ios·小程序·https·uni-app·php·iphone
不吃香菜的猪7 小时前
el-upload实现文件上传预览
前端·javascript·vue.js
dcloud_jibinbin8 小时前
【uniapp】小程序体积优化,分包异步化
前端·vue.js·webpack·性能优化·微信小程序·uni-app
qq_427506088 小时前
基于Vue 3和Element Plus实现简单的钩子函数管理各类弹窗操作
前端·javascript·vue.js
IT教程资源C8 小时前
(N_151)基于微信小程序校园学生活动管理平台
mysql·vue·前后端分离·校园活动小程序·springboot校园活动
2501_916008898 小时前
金融类 App 加密加固方法,多工具组合的工程化实践(金融级别/IPA 加固/无源码落地/Ipa Guard + 流水线)
android·ios·金融·小程序·uni-app·iphone·webview
2501_915921439 小时前
Fastlane 结合 开心上架(Appuploader)命令行版本实现跨平台上传发布 iOS App 免 Mac 自动化上架实战全解析
android·macos·ios·小程序·uni-app·自动化·iphone
游戏开发爱好者810 小时前
iOS 上架要求全解析,App Store 审核标准、开发者准备事项与开心上架(Appuploader)跨平台免 Mac 实战指南
android·macos·ios·小程序·uni-app·iphone·webview