当前位置:网站首页>v-bind用法:类动态绑定对象 数组 style样式 及函数方法
v-bind用法:类动态绑定对象 数组 style样式 及函数方法
2022-08-02 03:24:00 【yorup】
目录
1.动态绑定对象:class='{active:isactive}'
对象语法 :style='{fontSize:fontsize}'
数组语法(包括函数使用) :style='[baseStyle]'
正常的类写法
<style>
.active{
color: red;
}
</style> <div id="app">
<h2 class="active">一段文字</h2>
</div>但是这样可能会不够方便,所以有时候要动态绑定类。
1.动态绑定对象:class='{active:isactive}'
isactive的值为true或false,当true时css样式能实现,false不能实现
<div id="app">
<h2 :class='{active:isactive}'>{
{msg}}</h2>
</div>
<script>
const app = new Vue({
el: '#app',
data(){
return {
isactive: true,
msg: '一行文字'
}
}
})
</script>
对象换成函数 :class='Getactive()'
使得Getactive()返回和{active:isactive}一样才能实现,但函数在类里面,所以Getactive()要返回{active:this.isactive}
<div id="app">
<h2 :class='Getactive()' @click='change'>{
{msg}}</h2>
</div>
<script>
const app = new Vue({
el: '#app',
data(){
return {
isactive: false,
msg: '一行文字'
}
},
methods: {
change() {
this.isactive = !this.isactive
},
Getactive(){
return {active:this.isactive}
}
},
})
</script>2.动态绑定数组 :class="[act1,act2]"
数组里面的变量不要加引号,加了就是字符串
<div id="app">
<!-- 类名为"act1 act2" -->
<h2 :class="['act1','act2']">学习中……</h2>
<!-- 类名为"aaa bbb" -->
<h2 :class="[act1,act2]">学习中……</h2>
</div>
<script>
const app = new Vue({
el: '#app',
data(){
return {
act1:'aaa',
act2:'bbb'
}
}
})
</script>
数组换成对象 :class='getAct()'
<div id="app">
<h2 :class="[act1,act2]">学习中……</h2>
<h2 :class='getAct()'>学习中</h2>
</div>
<script>
const app = new Vue({
el: '#app',
data(){
return {
act1:'aaa',
act2:'bbb'
}
},
methods:{
getAct(){
return [this.act1,this.act2]
}
}
})
</script>3.动态绑定style(改变字体大小)
对象语法 :style='{fontSize:fontsize}'
<body>
<div id="app">
<!-- 不用动态变量 -->
<h2 :style='{fontSize:"50px"}'>123</h2>
<h2 :style='{fontSize:fontsize}'>567</h2>
</div>
<script>
const app = new Vue({
el: '#app',
data(){
return {
fontsize:50+'px'
}
}
})
</script>
</body>对象语法使用函数 :style='getFont()'
<div id="app">
<h2 :style='getFont()'>890</h2>
</div>
<script>
const app = new Vue({
el: '#app',
data(){
return {
fontsize:50+'px'
}
},
methods:{
getFont(){
return {fontSize:this.fontsize}
}
}
})
</script>数组语法(包括函数使用) :style='[baseStyle]'
<div id="app">
<!-- 不用动态变量 没有类名-->
<h2 :style='["baseStyle"]'>123</h2>
<h2 :style='[baseStyle]'>567</h2>
<!-- 函数 -->
<h2 :style='getFont()'>890</h2>
</div>
<script>
const app = new Vue({
el: '#app',
data(){
return {
baseStyle:{background:'#f00'}
}
},
methods:{
getFont(){
return this.baseStyle
}
}
})
</script>4.添加点击事件@click
点击事件使点击一次文字变色,再次点击恢复。在methods()里面添加函数
<div id="app">
<h2 :class='{active:isactive}' @click='change'>{
{msg}}</h2>
</div>
<script>
const app = new Vue({
el: '#app',
data(){
return {
isactive: false,
msg: '一行文字'
}
},
methods: {
change() {
this.isactive = !this.isactive
}
},
})
</script>边栏推荐
猜你喜欢

我的小笔记 =》原生微信小程序

Living to detect the Adaptive Normalized Representation Learning for GeneralizableFace Anti - Spoofing reading notes

require模块化语法

新工程加载YOLOV6的预训练权重问题

Phospholipid-Polyethylene Glycol-Aldehyde DSPE-PEG-Aldehyde DSPE-PEG-CHO MW: 5000

最新,每天填坑,Jeston TX1 精卫填坑,第一步:刷机

canvas--饼状图

正则笔记(2)- 正则表达式位置匹配攻略

1.uview form校验位置可以改变 2.时间区间

4.14到新公司的一天
随机推荐
我的小笔记 =》原生微信小程序
np.unique() function
---静态页面---
np.isnan ()
猴子选大王(约瑟环问题)
L1-020 帅到没朋友 (20分)
Error in render: “TypeError: Cannot read properties of null (reading ‘0‘)“ 报错解决方案
js 取字符串中某位置某特征的值,如华为(Huawei)=>华为
ThunderBirde无法登录问题、pycharm调试一直收集数据、RuntimeError: CUDA error: device-side assert triggered等疑难杂症解决
正则笔记(2)- 正则表达式位置匹配攻略
Cut out web icons through PS 2021
猴子选大王
解决MySQL创建子视图并查看的时候,字符集报错问题
【 application 】 life many years of operations, what turned scored 12 k + annual bonus salary?
Relative and absolute paths
How to check whether a table is locked in mysql
L1-043 阅览室 (20分)
Living to detect the Adaptive Normalized Representation Learning for GeneralizableFace Anti - Spoofing reading notes
--fs module--
环形链表---------约瑟夫问题