style
- 为什么要绑定样式呢?
- A: 样式有几种使用形式
- style双标签嵌入样式(内联)
- 行内样式
- 外部引用
- @import(’./csss/…’)
- 数据 --控制–样式—》 功能 效果
- 样式 要和 数据 绑定–》 v-bind
- 样式的绑定形式
- A: 对象
<div :style = "{width:'50px'}"></div>
<div :style = "style"></div>
第二种将数据放在data中
- B: 数组
<div
:style = "[{width:'50px',height:'50px',background:'yellow'}]"
></div>
<div
:style = "[style]"
></div>
<div
:style = "[styleFlag?style:'']"
></div>
举例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="../basic-source/vue.js"></script>
<style>
</style>
</head>
<body>
<div id="app">
<h3>样式</h3>
<div style="width:100px;height:100px;background: red"></div>
<hr>
<h3>样式绑定-对象形式</h3>
<div :style="{width:'100px',height:'100px',background:'red'}"></div>
<div :style="style"></div>
<hr>
<h3>样式绑定-数组形式</h3>
<div :style="[{width:'100px',height:'100px',background:'red'}]"></div>
<div :style="[style]"></div>
<div :style="[styleFlage?style:'']"></div>
</div>
</body>
<script>
new Vue({
el: '#app',
data: {
styleFlage: true,
style: {
width: '100px',
height: '100px',
background: 'red',
margin: '5px'
}
}
})
</script>
</html>