当前位置: 首页 > news >正文

Vue中v-bind对样式控制的增强—(详解v-bind操作class以及操作style属性,附有案例+代码)

文章目录

  • v-bind对样式控制的增强
    • 2.1 操作class
      • 2.1.1 语法
      • 2.1.2 对象语法
      • 2.1.3 数组语法
      • 2.1.4 使用
      • 2.1.5 Test
    • 2.2 操作style
      • 2.2.1 语法
      • 2.2.2 使用
      • 2.2.3 Test

v-bind对样式控制的增强

2.1 操作class

2.1.1 语法

<div> :class = "对象/数组">这是一个div</div>

2.1.2 对象语法

当class动态绑定的是对象时,键就是类名,值就是布尔值,如果值是true,就有这个类,否则没有这个类

<div class="box" :class="{ 类名1: 布尔值, 类名2: 布尔值 }"></div>

适用场景:一个类名,来回切换

2.1.3 数组语法

当class动态绑定的是数组时 → 数组中所有的类,都会添加到盒子上,本质就是一个 class 列表

<div class="box" :class="[ 类名1, 类名2, 类名3 ]"></div><div class="box" :class="[ '类名1', '类名2', '类名3' ]"></div>

使用场景:批量添加或删除类

2.1.4 使用

<style>.box {width: 50px;height: 50px;border: 1px solid #000;margin-top: 5px;}.red {background-color: red;}.big {width: 100px; height: 100px;}</style>
</head>
<body><div id="app"><div class="box" :class="{ big: true, red: true }">你好Java</div><div class="box" :class="['red', 'big']">你好Java</div></div><script src="js/vue.js"></script><script>const app = new Vue({el: '#app',data: {}})</script>
</body>

2.1.5 Test

<style>* {margin: 0;padding: 0;}ul {display: flex;border-bottom: 2px solid #1e3c9f;padding: 0 10px;}li {width: 100px;height: 50px;line-height: 50px;list-style: none;text-align: center;}li a {display: block;text-decoration: none;font-weight: bold;color: #333333;}li a.active {background-color: #12e06f;color: #fff;}</style>
</head>
<body><div id="app"><ul><li v-for="(item ,index) in list" :key="item.id" @click="activeIndex = index"><a href="#" :class="{active: index === activeIndex}">{{item.name}}</a></li></ul></div><script src="js/vue.js"></script><script>const app = new Vue({el: '#app',data: {activeIndex: 0, // 记录高亮list: [{id: 1,name: '商品秒杀'}, {id: 2,name: '特价处理'}, {id: 3,name: '品牌优惠'}]}})</script>
</body>

2.2 操作style

2.2.1 语法

:style中的css属性会覆盖 class="box"的css样式

<div class="box" :style="{ CSS属性名1: CSS属性值, CSS属性名2: CSS属性值 }"></div>

2.2.2 使用

<style>.box {width: 50px;height: 50px;background-color: red;}</style>
</head><body><div id="app"><div class="box" :style="{ width: '100px', height: '100px', backgroundColor: 'green' }"></div></div><script src="js/vue.js"></script><script>const app = new Vue({el: '#app',data: {}})</script>
</body>

2.2.3 Test

 <style>.progress {height: 25px;width: 400px;border-radius: 15px;background-color: #272425;border: 3px solid #272425;box-sizing: border-box;margin-bottom: 30px;}.inner {width: 50%;height: 20px;border-radius: 10px;text-align: right;position: relative;background-color: #409eff;background-size: 20px 20px;box-sizing: border-box;transition: all 1s;}.inner span {position: absolute;right: -20px;bottom: -25px;}</style>
</head><body><div id="app"><!-- 外层盒子底色 (黑色) --><div class="progress"><!-- 内层盒子 - 进度(蓝色) --><div class="inner" :style="{ width: percent+'%' }"><span>{{ percent }}%</span></div></div><button @click="percent = 25">设置25%</button><button @click="percent = 50">设置50%</button><button @click="percent = 75">设置75%</button><button @click="percent = 100">设置100%</button></div><script src="js/vue.js"></script><script>const app = new Vue({el: '#app',data: {percent: 30}})</script>
</body>
http://www.lryc.cn/news/463959.html

相关文章:

  • 【分布式微服务云原生】《ZooKeeper 深度探秘:分布式协调的强大利器》
  • 打造高性能在线电子表格:WebGL 渲染引擎 Kola2d 自研之路
  • 深入理解WPF中的命令机制
  • 基础算法(6)——模拟
  • 2025年广西高考报名流程图解(手机端)
  • 十、结构型(外观模式)
  • 10.12Python数学基础-矩阵(上)
  • 重学SpringBoot3-安装Spring Boot CLI
  • 代码复现(五):GCPANet
  • 联邦学习实验复现—MNISIT IID实验 pytorch
  • 2015年-2017年 计算机技术专业 程序设计题(算法题)实战_c语言程序设计数据结构程序设计分析
  • 个人用计算理论导引笔记(待补充)
  • 2024年诺贝尔物理学奖揭晓:AI背后的“造梦者”是谁?
  • 2024年AI 制作PPT新宠儿,3款神器集锦,让你的演示与众不同
  • CLion和Qt 联合开发环境配置教程(Windows和Linux版)
  • Qt记录使用QtAwesome
  • ES6新增promise(异步编程新解决方案)如何封装ajax?
  • Kubernetes--深入理解Service与CoreDNS
  • AI大模型:开启智能革命新纪元
  • 快速上手C语言【下】(非常详细!!!)
  • 红黑树的理解与实现(详解)
  • 从一到无穷大 #37 Databricks Photon:打响 Spark Native Engine 第一枪
  • Java 字符串占位格式化
  • 基于netty实现简易版rpc服务-理论分析
  • Elasticsearch高级搜索技术-全文搜索
  • 案例分享—国外优秀UI卡片设计作品赏析
  • Go语言基础学习(Go安装配置、基础语法)
  • STM32—FLASH闪存
  • AP上线的那些事儿(1)capwap建立过程、设备初始化以及二层上线
  • 10 django管理系统 - 管理员管理 - 新建管理员(通过模态框和ajax实现)