Vue教程(五):样式绑定——class和style

1、样式代码准备

样式提前准备

stylus 复制代码
<style>
        .basic{
            width: 400px;
            height: 100px;
            border: 1px solid black;
        }

        .happy{
            border: 4px solid red;
            background-color: rgba(255, 255, 0, 0.644);
            background: linear-gradient(30deg, yellow, pink, orange, yellow);
        }

        .sad{
            border: 4px dashed rgb(1, 197, 2);
            background-color: gray;
        }

        .normal{
            background-color: skyblue;
        }

        .nobug1{
            background-color: yellowgreen;
        }

        .nobug2{
            font-size: 30px;
            text-shadow: 2px 2px 10px red;
        }

        .nobug3{
            border-radius: 5px;
        }
    </style>

2、绑定class样式

绑定class样式------字符串写法

适用于:样式的类名不确定,需要动态指定

html:

html 复制代码
<!--    绑定class样式------字符串写法,适用于:样式的类名不确定,需要动态指定-->
    <div class="basic" :class="mood" @click="changeMood">
        {{label}}
    </div>

js:

js 复制代码
mood: 'normal',
methods:{
            changeMood(){
                const arr = ['normal', 'happy', 'sad'];
                // Math.random()产生的数值介于 0,1之间,可以等于0,但是不会等于1;
                let index = Math.floor(Math.random() * 3);
                this.mood = arr[index];
            }
        }

效果:

绑定class样式------数组写法

适用于:要绑定的样式个数不确定,名字也不确定

html:

html 复制代码
<div class="basic" :class="classArr">
        {{label}}
    </div>

js

js 复制代码
classArr: ['nobug1', 'nobug2', 'nobug3'],

效果:

绑定class样式------对象写法

适用于:要绑定的样式个数确定,名字也确定,但需要动态决定用不用

html

html 复制代码
<div class="basic" :class="classObj">
        {{label}}
    </div>

js

js 复制代码
classObj: {
                nobug1: false,
                nobug2: true,
            },

效果:

3、绑定style样式

绑定style样式------对象写法

html

html 复制代码
<!--    绑定style样式------对象写法-->
    <div class="basic" :style="styleObj">
        {{label}}
    </div>

js

js 复制代码
styleObj:{
                fontSize: '40px',
            },

效果:

绑定style样式------数组写法

html

html 复制代码
<!--    绑定style样式------数组写法-->
    <div class="basic" :style="styleArr">
        {{label}}
    </div>

js

js 复制代码
styleArr: [
                {
                    fontSize: '40px',
                },
                {
                    color: 'red'
                }
            ]

效果:

完整代码

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript" src="../js/vue.js"></script>
    <style>
        .basic{
            width: 400px;
            height: 100px;
            border: 1px solid black;
        }

        .happy{
            border: 4px solid red;
            background-color: rgba(255, 255, 0, 0.644);
            background: linear-gradient(30deg, yellow, pink, orange, yellow);
        }

        .sad{
            border: 4px dashed rgb(1, 197, 2);
            background-color: gray;
        }

        .normal{
            background-color: skyblue;
        }

        .nobug1{
            background-color: yellowgreen;
        }

        .nobug2{
            font-size: 30px;
            text-shadow: 2px 2px 10px red;
        }

        .nobug3{
            border-radius: 5px;
        }
    </style>
</head>
<body>
<div id="root">
    <!--    绑定class样式------字符串写法,适用于:样式的类名不确定,需要动态指定-->
    <div class="basic" :class="mood" @click="changeMood">
        {{label}}
    </div>
    <br/>
    <br/>
    <!--    绑定class样式------数组写法,适用于:要绑定的样式个数不确定,名字也不确定-->
    <div class="basic" :class="classArr">
        {{label}}
    </div>
    <br/>
    <br/>
    <!--    绑定class样式------对象写法,适用于:要绑定的样式个数确定,名字也确定,但需要动态决定用不用-->
    <div class="basic" :class="classObj">
        {{label}}
    </div>
    <br/>
    <br/>
    <!--    绑定style样式------对象写法-->
    <div class="basic" :style="styleObj">
        {{label}}
    </div>
    <br/>
    <br/>
    <!--    绑定style样式------数组写法-->
    <div class="basic" :style="styleArr">
        {{label}}
    </div>
    <br/>
    <br/>
</div>
<script>
    // 设置为 false 以阻止 vue 在启动时生成生产提示。
    Vue.config.productionTip = false;

    // 创建Vue实例
    new Vue({
        // el用于指定当前Vue实例为哪个容器服务,值通常为css选择器字符串
        el: '#root',
        data:{
            label: '不写八个',
            mood: 'normal',
            classArr: ['nobug1', 'nobug2', 'nobug3'],
            classObj: {
                nobug1: false,
                nobug2: true,
            },
            styleObj:{
                fontSize: '40px',
            },
            styleArr: [
                {
                    fontSize: '40px',
                },
                {
                    color: 'red'
                }
            ]
        },
        methods:{
            changeMood(){
                const arr = ['normal', 'happy', 'sad'];
                // Math.random()产生的数值介于 0,1之间,可以等于0,但是不会等于1;
                let index = Math.floor(Math.random() * 3);
                this.mood = arr[index];
            }
        }
    })
</script>
</body>
</html>

完整实现效果

相关推荐
沉登c12 分钟前
Javascript客户端时间与服务器时间
服务器·javascript
持久的棒棒君15 分钟前
ElementUI 2.x 输入框回车后在调用接口进行远程搜索功能
前端·javascript·elementui
一 乐30 分钟前
租拼车平台|小区租拼车管理|基于java的小区租拼车管理信息系统小程序设计与实现(源码+数据库+文档)
java·数据库·vue.js·微信·notepad++·拼车
小程xy3 小时前
react 知识点汇总(非常全面)
前端·javascript·react.js
寻找09之夏4 小时前
【Vue3实战】:用导航守卫拦截未保存的编辑,提升用户体验
前端·vue.js
非著名架构师4 小时前
js混淆的方式方法
开发语言·javascript·ecmascript
多多米10055 小时前
初学Vue(2)
前端·javascript·vue.js
敏编程5 小时前
网页前端开发之Javascript入门篇(5/9):函数
开发语言·javascript
看到请催我学习5 小时前
内存缓存和硬盘缓存
开发语言·前端·javascript·vue.js·缓存·ecmascript
blaizeer6 小时前
深入理解 CSS 浮动(Float):详尽指南
前端·css