Vue2动态绑定style

简介: 从基础到实战,我们一环都不要少!

动态绑定 style

写法:

:style="{fontSize: xxx}"其中xxx是动态值。

:style="[a,b]"其中a、b是样式对象。

有三种动态绑定的写法

完整代码示例

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style></style>
    <script src="./vue.js"></script>
</head>

<body>
    <div id="app">
        <div style="width: 100px;height: 100px; border: solid 1px pink"> 固定 </div>
        <!-- 1. 动态绑定style,值为一个对象 -->
        <div :style="{
      
      width:w,height:h,border:borderHave}"> 动态绑定style 1 </div>
        <!-- 2. 动态绑定style,值为一个变量 -->
        <div :style="boxStyle"> 动态绑定style 2 </div>
        <!-- 3. 数组语法可放多个对象 -->
        <div :style="[boxStyle,base]"> 动态绑定style 3 </div>
    </div>
    <script>
        let app = new Vue({
    
    
            el: "#app",
            data: {
    
    
                w: "100px",
                h: "100px",
                borderHave: "3px solid pink",
                boxStyle: {
    
    
                    width: "100px",
                    height: "100px",
                    border: "1px solid blue"
                },
                base: {
    
    
                    color: "#fff",
                    backgroundColor: "#f00"
                }
            }
        })
    </script>
</body>

</html>

效果图例

image.png

用法:


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>动态绑定内联样式示例</title>
    <style>
        .text-input {
    
    
            padding: 10px;
            font-size: 16px;
            border: 1px solid #ccc;
            width: 200px;
        }
    </style>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>

<body>
    <div id="app">
        <input type="text" class="text-input" v-model="inputText" :style="{
      
       backgroundColor: inputBackgroundColor, fontSize: inputFontSize + 'px' }">
    </div>
    <script>
        let app = new Vue({
    
    
            el: "#app",
            data: {
    
    
                inputText: '',
                inputBackgroundColor: '',
                inputFontSize: 16
            },
            watch: {
    
    
                inputText(newVal) {
    
    
                    // 根据输入文本的长度设置背景颜色和字体大小
                    if (newVal.length < 5) {
    
    
                        this.inputBackgroundColor = 'lightgreen';
                        this.inputFontSize = 16;
                    } else if (newVal.length >= 5 && newVal.length < 10) {
    
    
                        this.inputBackgroundColor = 'lightblue';
                        this.inputFontSize = 18;
                    } else {
    
    
                        this.inputBackgroundColor = 'lightpink';
                        this.inputFontSize = 20;
                    }
                }
            }
        })
    </script>
</body>

</html>

我们使用Vue框架来创建一个Vue实例,并将其绑定到id为"app"的元素上。在数据中,我们定义了inputText、inputBackgroundColor和inputFontSize这三个变量,分别控制输入文本、背景颜色和字体大小。

在输入框上使用了:style指令来动态绑定内联样式。通过绑定的对象来设置样式的属性。在watch选项中监听inputText的变化,根据输入文本的长度来动态改变背景颜色和字体大小。

目录
相关文章
|
前端开发 JavaScript UED
过渡效果的艺术:CSS transition 让网页交互更平滑(上)
过渡效果的艺术:CSS transition 让网页交互更平滑(上)
|
Windows
Electron窗口白屏解决
Electron白屏问题主要出现在两方面: 1、electron未加载完毕html,electron自身产生的白色背景; 2、electron加载html,html处于加载渲染过程中产生的短暂白屏;
3597 0
|
前端开发
如何使用 Promise 的 all 方法?
如何使用 Promise 的 all 方法?
1170 162
|
11月前
|
数据采集 监控 API
【量化基础】数据驱动决策:从零接入StockTV实时行情API
本文介绍量化交易基础,详解如何用Python对接StockTV实时行情API,获取毫秒级数据。涵盖REST与WebSocket实战代码,助你构建稳定策略,从数据获取迈向自动化交易,夯实量化基石。(238字)
|
敏捷开发 前端开发 测试技术
软件开发工作流【详解】(含公司产品研发流程图、大厂研发架构图、大厂研发流程图)
软件开发工作流【详解】(含公司产品研发流程图、大厂研发架构图、大厂研发流程图)
13406 1
|
数据采集 前端开发 数据安全/隐私保护
使用 urllib.parse 模块优雅地处理 URL
使用 urllib.parse 模块优雅地处理 URL
722 9
|
JavaScript 前端开发 索引
JS中常用的数组迭代方法(filter,forEach,map,every,some,find,findIndex)
这段代码和说明介绍了JavaScript中数组的一些常用方法。函数接收三个参数:`item`(数组项的值)、`index`(项的位置,可选)和`array`(数组本身,可选)。示例展示了如何使用`filter()`过滤非空项、`forEach()`遍历数组、`map()`处理并返回新数组、`every()`检查所有元素是否满足条件、`some()`检查是否存在满足条件的元素、`find()`获取首个符合条件的元素值以及`findIndex()`获取其索引位置。这些方法都不会修改原数组。
661 0
JS中常用的数组迭代方法(filter,forEach,map,every,some,find,findIndex)
|
存储 负载均衡 数据中心
聚合网卡和Wondershaper限速的一些问题(速度减半问题)
聚合网卡,又称为链路聚合组(LAG, Link Aggregation Group)、端口汇聚(Port Trunking)、以太通道(Ethernet Bonding)等,是一种网络技术,通过将两个或多个物理以太网接口(网卡)逻辑上捆绑在一起,形成一个单一的虚拟接口。这种技术不仅提升了网络连接的可用性和带宽,同时也增强了网络的稳定性和可靠性。以下是聚合网卡的一些关键概念、工作原理以及其优势:
850 2