Vue动态样式

1.1动态class

用v-bind给标签class设置动态的值

  • 语法:

  • :class="{类名: 布尔值}"

<template>
  <div>
    <!-- 语法:
      :class="{类名: 布尔值}"
      使用场景: vue变量控制标签是否应该有类名
     -->
    <p :class="{red_str: bool}">动态class</p>
  </div>
</template>
​
<script>
export default {
  data(){
    return {
      bool: true
    }
  }
}
</script>
​
<style scoped>
  .red_str{
    color: red;
  }
</style>

就是把类名保存在vue变量中赋予给标签

代码演示2:

<template>
    <div>
     <!-- 语法:
      :class="{类名: 布尔值}"
      使用场景: vue变量控制标签是否应该有类名
     -->
     <p :class="{red_str: bool}">动态class</p>
     <p :class="{red_str: bool,helloWorld: bool}">hello和class</p>
     <!--使用对象模式添加-->
     <p :class="classObj">helloWorld</p>
​
    </div>
</template>
<script>
export default {
    data(){
        return{
            bool:true,
            classObj:{
                red_str:true,
                helloWorld:true
            }
        };
    }
};
</script>
<style scoped>
.red_str{
    color: red;
  }
.helloWorld{
    background-color: aqua;
}
</style>

1.2 动态style

给标签动态设置style的值CSS property 名可以用驼峰式 (camelCase) 或短横线分隔 (kebab-case,记得用引号括起来) 来命名

  • 语法

  • :style="{css属性: 值}"

<template>
     <div>
    <p style="color:red">基本style</p>
    <!-- 动态style语法
      :style="{css属性名: 值}"
     -->
    <p :style="{backgroundColor: colorStr}">动态style</p>
    <p :style="{color: colorStr,fontSize:fontSize,'backgroundColor':bgColor}">动态style之多个style</p>
    <!--多个样式抽取-->
     <p :style="styleObj">样式抽取后的style</p>
  </div>
</template>
<script>
export default {
    data(){
        return{
            colorStr: 'red',
            fontSize:'40px',
            bgColor:'pink',
            styleObj:{
                color: 'red',
                fontSize:'40px',
                'backgroundColor':'pink' 
            }
        }
    }
}
</script>

动态style的key都是css属性名

1.3 案例-品牌管理(铺)

数据铺设

  • 需求1: 把默认数据显示到表格上

  • 需求2: 注意资产超过100的, 都用红色字体标记出来

细节:

① 先铺设静态页面

② 此案例使用bootstrap, 需要下载, 并导入到工程main.js中

③ 用v-for配合默认数据, 把数据默认铺设到表格上显示

④ 直接在标签上, 大于100价格, 动态设置red类名

1.因为案例使用了bootstrap, 工程化开发, 模块化用npm/yarn下载引入使用

npm install bootstrap

2.在main.js - 引入bootstrap

import "bootstrap/dist/css/bootstrap.css" 
// 默认找文件夹下的index文件(但是这个不是所以需要写路径)

1.4 案例-品牌管理(增)

数据新增

  • 需求1: 实现表单数据新增进表格功能

  • 需求2: 判断用户输入是否为空给提示

  • 分析① 添加资产按钮 – 绑定点击事件② 给表单v-model绑定vue变量收集用户输入内容③ 添加数组到数组中④ 判断用户内容是否符合规定

<!-- 添加资产 -->
      <form class="form-inline">
        <div class="form-group">
          <div class="input-group">
            <input
              type="text"
              class="form-control"
              placeholder="资产名称"
              v-model="name"
            />
          </div>
        </div>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <div class="form-group">
          <div class="input-group">
            <input
              type="text"
              class="form-control"
              placeholder="价格"
              v-model.number="price"
            />
          </div>
        </div>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <!-- 4. 阻止表单提交(刷新网页数据又回去了) -->
        <button class="btn btn-primary" @click.prevent="addFn">添加资产</button>
      </form>
​
<script>
// 目标: 新增
// 1. 按钮 - 事件
// 2. 给表单v-model绑定vue变量
export default {
  // ...省略其他
  methods: {
    addFn(){
      // 5. 判断是否为空
      if (this.name.trim().length === 0 || this.price === 0) {
        alert("不能为空")
        return
      }
      // 3. 把值以对象形式-插入list
      this.list.push({
        // 当前数组最后一个对象的id+1作为新对象id值
        id: this.list[this.list.length - 1].id + 1,
        name: this.name,
        price: this.price,
        time: new Date()
      })
    }
  }
};
</script>

1.5 案例-品牌管理(删)

目标: 数据删除

  • 需求1: 点击删除的a标签, 删除数据

  • 需求2: 删除没数据了要提示暂无数据的tfoot

  • 分析① a标签绑定点击事件② 给事件方法传id③ 通过id, 找到对应数据删除④ 删除光了要让tfoot显示⑤ 删除光了再新增, 有bug(id值问题)需要修复

<td><a href="#" @click="delFn(obj.id)">删除</a></td>
          
<script>
// 目标: 删除功能
// 1. 删除a标签-点击事件
// 2. 对应方法名
// 3. 数据id到事件方法中
// 4. 通过id, 找到这条数据在数组中的下标
// 5. splice方法删除原数组里的对应元素
// 6. 设置tfoot, 无数据给出提示
// 7. 无数据再新增, id要判断一下
export default {
  // ...其他代码
  methods: {
    // ...其他代码
    delFn(id){
      // 通过id找到这条数据在数组中下标
      let index = this.list.findIndex(obj => obj.id === id)
      this.list.splice(index, 1)
    }
  }
};
</script>
​
bug解决方式:
<script>
 // 解决bug: 无数组新增-list没有数据, id需要给一个固定值(以后id都是后台生成, 现在是模拟给一个id)
       let id = this.list.length > 0 ? this.list[this.list.length - 1].id + 1 : 100
      // 3. 把值以对象形式-插入list
      this.list.push({
        // 当前数组最后一个对象的id+1作为新对象id值
        id: id,
        name: this.name,
        price: this.price,
        time: new Date()
      })
    </script>

整体代码展示:

<template>
    <div>
      <div class="container">
        <!-- 顶部框模块 -->
        <div class="form-group">
          <div class="input-group">
            <h4>品牌管理</h4>
          </div>
        </div>
  
        <!-- 数据表格 -->
        <table class="table table-bordered table-hover mt-2">
          <thead>
            <tr>
              <th>编号</th>
              <th>资产名称</th>
              <th>价格</th>
              <th>创建时间</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="obj in list" :key="obj.id">
              <td>{{ obj.id }}</td>
              <td>{{ obj.name }}</td>
  
              <!-- 如果价格超过100,就有red这个类 -->
              <td :class="{red: obj.price > 100}">{{ obj.price }}</td>
              <td>{{ obj.time }}</td>
              <td><a href="#" @click="delFn(obj.id)">删除</a></td>
            </tr>
          </tbody>
  
          <tfoot >
            <tr>
              <td colspan="5" style="text-align: center" v-show="list.length==0?true:false">暂无其他数据</td>
              
            </tr>
          </tfoot>
           
        </table>
  
        <!-- 添加资产 -->
        <form class="form-inline">
          <div class="form-group">
            <div class="input-group">
              <input  type="text"  class="form-control"  placeholder="资产名称信息"  v-model="name"
              />
            </div>
          </div>
          &nbsp;&nbsp;&nbsp;&nbsp;
          <div class="form-group">
            <div class="input-group">
              <input
                type="text"
                class="form-control"
                placeholder="价格"
                v-model.number="price"
              />
            </div>
          </div>
          &nbsp;&nbsp;&nbsp;&nbsp;<br>
          <!-- 阻止表单提交 -->
          <button class="btn btn-primary"  @click.prevent="addFn">添加资产信息</button>
        </form>
      </div>
    </div>
  </template>
  
  <script>
  // 1. 明确需求
  // 2. 标签+样式+默认数据
  // 3. 下载bootstrap, main.js引入bootstrap.css
  // 4. 把list数组 - 铺设表格
  // 5. 修改价格颜色
  export default {
    data() {
      return {
        isshow:true,
        name: "", // 名称
        price: 0, // 价格
        list: [
   { id: 100, name: "外套", price: 199, time: new Date()},
   { id: 101, name: "裤子", price: 34, time: new Date() },
   { id: 102, name: "鞋", price: 25.4, time: new Date() },
   { id: 103, name: "头发", price: 19900, time: new Date() }
        ],
      };
    },
    methods:{
      addFn(){
           // 5. 判断是否为空
        if (this.name.trim().length === 0 || this.price === 0) {
          alert("不能为空")
          return
        }
         // 解决bug: 无数组新增-list没有数据, id需要给一个固定值(以后id都是后台生成, 现在是模拟给一个id)
         let id = this.list.length > 0 ? this.list[this.list.length - 1].id + 1 : 100
        // 3. 把值以对象形式-插入list
        this.list.push({
          // 当前数组最后一个对象的id+1作为新对象id值
          id: id,
          name: this.name,
          price: this.price,
          time: new Date()
        })
      },
      delFn(id){
           // 通过id找到这条数据在数组中下标
        let index = this.list.findIndex(obj => obj.id === id)
        this.list.splice(index, 1)
      }
    }
  
  };
  </script>
  
  <style scoped>
  .red{
    color: red;
  }
  h4{
    /* position: absolute; */
    margin-left: 260px;
    color: yellowgreen;
    background-color: white;
  }
  .input-group{
    text-align: center;
    /* background-color: aquamarine; */
    /* position: relative; */
  }
  .table{
    background-color: aqua;
  }
  .form-group{
    /* background-color: red; */
  }
  a{
    color: red;
    text-decoration: none;
  }
  </style>

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值