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

Vue 类与样式

数据绑定的一个常见需求场景是操纵元素的 CSS class 列表和内联样式。因为 class 和 style 都是 attribute,我们可以和其他 attribute 一样使用 v-bind 将它们和动态的字符串绑定。但是,在处理比较复杂的绑定时,通过拼接生成字符串是麻烦且易出错的。因此,Vue 专门为 class 和 style 的 v-bind 用法提供了特殊的功能增强。除了字符串外,表达式的值也可以是对象或数组。

一、绑定Class

    <div class="item" :class="{ active: activeIndex == index }" v-for="(item, index) in students" @click="itemClick(index)">
        学生编号:{{ item.id }}
        学生姓名:{{ item.name }}
    </div>

css

<style>
.item {
    background-color: rgb(180, 180, 180);
    padding: 20px;
    border-bottom: 1px solid red;
    transition: all ease 1s;
}

.item.active {
    background-color: red;
    color: white;
    font-size: 20px;
}
</style>

js

<script>
export default {
    data() {
        return {
            students: [
                { id: 1, name: '张三' },
                { id: 2, name: '李四' },
                { id: 3, name: '王五' },
                { id: 4, name: '赵六' },
            ],
            activeIndex: -1
        }
    },
    methods: {
        itemClick(e) {
            console.info(e);

            this.activeIndex = e;
        }
    }
}
</script>

二、绑定内联样式,绑定style

<div class="item" :style="{ color: activeIndex == index?'red':'blue' }" v-for="(item, index) in students" @click="itemClick(index)">
        学生编号:{{ item.id }}
        学生姓名:{{ item.name }}
    </div> 
<div :style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>

更多:

Vue 表单输入绑定,双向绑定
Vue生命周期_Vue生命周期钩子
Vue 概念、历史、发展和Vue简介
http://www.dtcms.com/a/98653.html

相关文章:

  • 【数学建模】(启发式算法)模拟退火算法:原理、实现与应用
  • [c++项目]基于微服务的聊天室服务端测试
  • 基于ssm的课程辅助教学平台(全套)
  • 直流电机类型及其控制技术
  • 免费下载 | 2025年网络安全报告
  • libimobiledevice项目中各个库的作用
  • 【数据结构】二叉树的递归
  • 前端音频和视频上传预览功能的探索与总结
  • Linux 基本使用和 web 程序部署
  • 人生感悟8
  • 【测试】每日3道面试题 3/29
  • Advanced Renamer:批量文件重命名工具
  • Vue3组件事件用户信息卡练习
  • SOA、ESB与微服务:架构演进与对比分析
  • 【大前端系列20】JavaScript核心:项目实战从零构建任务管理系统
  • 深入解析 Vue Router 与钩子函数:从核心原理到最佳实践
  • ChemBioServer: 一个在线“药物发现/再利用”的平台
  • 人工智能安全:从技术防御到全球治理的多层次应对策略
  • Error in torch with streamlit
  • JavaWeb——案例(1/20)-准备工作(案例目标、环境搭建、三层架构搭建、规范要求)附带SQL脚本
  • c++ vs和g++下的string结构
  • 某大麦手机端-抢票
  • Mybatis_Plus中常用的IService方法
  • 图解AUTOSAR_SWS_SPIHandlerDriver
  • PyQt6实例_批量下载pdf工具_主线程启用线程池
  • 语音波形编码与参量编码 LPC 的性能分析
  • 开源项目 vue-element-admin本地启动教程
  • 求职笔试题
  • 信号与系统(郑君里)第一章-绪论 1-21 课后习题解答
  • java面向对象从入门到入土