码迷,mamicode.com
首页 > 其他好文 > 详细

vue3.x class和style绑定

时间:2021-06-20 18:21:34      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:pre   off   rip   hex   灰色   exp   imp   temp   tag   

 

注:实例环境 vue cli构建的项目

app.vue

<template>
  <Example></Example>
</template>

<script>
import Example from ‘./components/Example‘

export default {
  name: ‘App‘,
  components: {
    Example
  }
}
</script>

<style>

</style>

Example.vue

<template>
    <div>
       <div :class="className" :style="styleName">
           <div>
               <button @click="changeClass(‘class1‘)">灰色</button>
               <button @click="changeClass(‘class2‘)">黑色</button>
               <button @click="changeClass(‘class3‘)">红色</button>
               &nbsp;&nbsp;<span>|</span>&nbsp;&nbsp;
               <button @click="changeStyle(‘left‘)">居左</button>
               <button @click="changeStyle(‘center‘)">居中</button>
               <button @click="changeStyle(‘right‘)">居右</button>
           </div>
           <p>
               class 绑定
           </p>

       </div>
    </div>
</template>

<script>
    export default {
        name: "Example",
        data:function () {
            return {
                className: ‘class1‘,
                styleName: ‘left‘
            }
        },
        methods:{
            changeClass:function (className) {
                this.className = className;
            },
            changeStyle:function (styleName) {
                this.styleName ={
                    textAlign: styleName
                };
            }
        }
    }
</script>

<style scoped>
.class1{
    background-color: #ccc;
    color:#000;
}
.class2{
    background-color: #000;
    color:#fff;
}
.class3{
    background-color: red;
    color: yellow;
}
</style>

刷新浏览器

技术图片

vue3.x class和style绑定

标签:pre   off   rip   hex   灰色   exp   imp   temp   tag   

原文地址:https://www.cnblogs.com/hu308830232/p/14906649.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!