Vue:class与style绑定

Vue:class与style绑定
2.6.1 class绑定
1、绑定字符串

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

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Class绑定之字符串形式title>
    <script src="../js/vue.js">script>
    <style>
      .static {
        border: 1px solid black;
        background-color: aquamarine;
      }
      .big {
        width: 200px;
        height: 200px;
      }
      .small {
        width: 100px;
        height: 100px;
      }
      .red-border {
        border-color: red;
      }
    style>
  head>
  <body>
    <div id="app">
      <h1>{{msg}}h1>
      
      <div class="static small">{{msg}}div>
      <hr />
      
      
      <div class="static" :class="c1">{{msg}}div>
      <button @click="changeBig">变大button>
      <button @click="changeSmall">变小button>
      <button @click="addRedBorder">添加红色边框button>
    div>
    <script>
      const vm = new Vue({
        el: "#app",
        data: {
          msg: "Class绑定之字符串形式",
          c1: "small",
          hasRedBorder: false
        },
        methods: {
          changeBig() {
            this.c1 = "big";
          },
          changeSmall() {
            this.c1 = "small";
          },
          addRedBorder() {
            if (this.hasRedBorder) {
              this.c1 = this.c1.replace(' red-border', '');
            } else {
              this.c1 += ' red-border';
            }
            this.hasRedBorder = !this.hasRedBorder;
          }
        },
      });
    script>
  body>
html>
2、绑定数组

适用于绑定的样式名字不确定,并且个数也不确定。

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Class绑定之数组形式title>
    <script src="../js/vue.js">script>
    <style>
      .static {
        border: 1px solid black;
        width: 100px;
        height: 100px;
      }
      .active {
        background-color: green;
      }
      .text-danger {
        color: red;
      }
      .rounded {
        border-radius: 10px;
      }
    style>
  head>
  <body>
    <div id="app">
      <h1>{{msg}}h1>
      
      <div class="static active text-danger">{{msg}}div>
      <br />
      
      <div class="static" :class="['active','text-danger']">{{msg}}div>
      <br />
      <div class="static" :class="[c1, c2]">{{msg}}div>
      <br />
      
      <div class="static" :class="classArray">{{msg}}div>
      <button @click="toggleRounded">切换圆角button>
    div>
    <script>
      const vm = new Vue({
        el: "#app",
        data: {
          msg: "Class绑定之数组形式",
          c1: "active",
          c2: "text-danger",
          classArray: ["active", "text-danger"],
          hasRounded: false
        },
        methods: {
          toggleRounded() {
            if (this.hasRounded) {
              this.classArray = this.classArray.filter(item => item!== 'rounded');
            } else {
              this.classArray.push('rounded');
            }
            this.hasRounded = !this.hasRounded;
          }
        }
      });
    script>
  body>
html>
3、绑定对象

适用于样式名字和个数都确定,但是要动态决定用或者不用。

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Class绑定之对象形式title>
    <script src="../js/vue.js">script>
    <style>
      .static {
        border: 1px solid black;
        width: 100px;
        height: 100px;
      }
      .active {
        background-color: green;
      }
      .text-danger {
        color: red;
      }
      .shadow {
        box-shadow: 5px 5px 5px gray;
      }
    style>
  head>
  <body>
    <div id="app">
      <h1>{{msg}}h1>
      
      
      <div class="static" :class="classObj">{{msg}}div>
      <br />
      <div class="static" :class="{active:true,'text-danger':false}">{{msg}}div>
      <button @click="toggleShadow">切换阴影button>
    div>
    <script>
      const vm = new Vue({
        el: "#app",
        data: {
          msg: "Class绑定之对象形式",
          classObj: {
            // 该对象中属性的名字必须和css中样式名一致。
            active: false,
            "text-danger": true,
            shadow: false
          }
        },
        methods: {
          toggleShadow() {
            this.classObj.shadow = !this.classObj.shadow;
          }
        }
      });
    script>
  body>
html>
2.6.2 style绑定
DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Style绑定title>
    <script src="../js/vue.js">script>
    <style>
      .static {
        border: 1px solid black;
        width: 100px;
        height: 100px;
      }
    style>
  head>
  <body>
    <div id="app">
      <h1>{{msg}}h1>
      
      <div class="static" style="background-color: green">静态写法div>
      <br />
      
      <div class="static" :style="myStyle">动态写法:字符串形式div>
      <br />
      
      <div class="static" :style="{'background-color': 'gray'}">动态写法1:对象形式div>
      <br />
      <div class="static" :style="styleObj1">动态写法2:对象形式div>
      <br />
      
      <div class="static" :style="styleArray">动态写法:数组形式div>
      <button @click="changeFontSize">改变字体大小button>
    div>
    <script>
      const vm = new Vue({
        el: "#app",
        data: {
          msg: "Style绑定",
          myStyle: "background-color: gray;",
          styleObj1: {
            backgroundColor: "green",
            fontSize: '16px'
          },
          styleArray: [{ backgroundColor: "green" }, { color: "red" }],
          currentFontSize: 16
        },
        methods: {
          changeFontSize() {
            this.currentFontSize += 2;
            this.styleObj1.fontSize = `${this.currentFontSize}px`;
          }
        }
      });
    script>
  body>
html>

你可能感兴趣的:(Vue2,vue.js)