vue 中样式的绑定
1、class的对象绑定
//对应的css
<style>
.active {
color: red;
}
</style> <!--html 对应的代码-->
<div class="container" id="mytest">
<div class="jumbotron">
<!--注意:如果需要添加其他静态的class,那么只要在名称上加上引号即可,否则视为变量处理-->
<div :class="{active:isactive,take:'take'}" @click="change">are you ok???</div>
</div>
</div> //javascript对应的代码
let VM = new Vue({
el: '#mytest',
//对应:class里的值
data: {isactive: false},
methods: {
change() {
this.isactive = !this.isactive;
}
}
})
2、class的数组绑定
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<link rel="stylesheet" href="./lib/bootstrap.css">
<style>
.active {
color: red;
}
</style>
<script src="./lib/vue.js"></script>
<script>
window.onload = function () {
let VM = new Vue({
el: '#mytest',
//对应:class里的值
data: {active: ''},
methods: {
change() {
this.active = this.active == '' ? 'active' : ''
}
}
})
}
</script>
</head>
<body>
<div class="container" id="mytest">
<div class="jumbotron">
<!--注意:如果需要添加其他静态的class,那么只要在名称上加上引号即可,否则视为变量处理如下面的类test-->
<div :class="[active,'test']" @click="change">are you ok???</div>
</div>
</div>
</body>
</html>
3、style的对象绑定
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<link rel="stylesheet" href="./lib/bootstrap.css">
<style>
.active {
color: red;
}
</style>
<script src="./lib/vue.js"></script>
<script>
window.onload = function () {
let VM = new Vue({
el: '#mytest',
data: {
activeObj: {
color: 'black',
'font-size': '20px'
}
},
methods: {
change() {
this.activeObj.color = this.activeObj.color == 'black' ? 'red' : 'black';
}
}
})
}
</script>
</head>
<body>
<div class="container" id="mytest">
<div class="jumbotron">
<!--用style的对象来绑定样式-->
<div :style="activeObj" @click="change">are you ok???</div>
</div>
</div>
</body>
</html>
4、style的数组绑定
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<link rel="stylesheet" href="./lib/bootstrap.css">
<style>
.active {
color: red;
}
</style>
<script src="./lib/vue.js"></script>
<script>
window.onload = function () {
let VM = new Vue({
el: '#mytest',
data: {
activeObj: {
color: 'black',
'font-size': '20px'
},
testObj:{
'text-decoration':'underline'
}
},
methods: {
change() {
this.activeObj.color = this.activeObj.color == 'black' ? 'red' : 'black';
}
}
})
}
</script>
</head>
<body>
<div class="container" id="mytest">
<div class="jumbotron">
<!--用style的数组可以容纳多个对象,可以是vue里data的数据,也可以是行内数据,注意引号-->
<div :style="[activeObj,testObj,{'font-weight':'bold'}]" @click="change">are you ok???</div>
</div>
</div>
</body>
</html>
5、通过委托事件绑定实现
通过methods里的方法,第一个传值是event,那么通过event.target 或者 event.currentTarget来操作DOM实现类的切换,可结合jquery
vue 中样式的绑定的更多相关文章
- 黑马vue---17、vue中通过属性绑定绑定style行内样式
黑马vue---17.vue中通过属性绑定绑定style行内样式 一.总结 一句话总结: 如果属性名中带有短线必须加引号,比如: h1StyleObj: { color: 'red', 'font-s ...
- 黑马vue---16、vue中通过属性绑定为元素设置class类样式
黑马vue---16.vue中通过属性绑定为元素设置class类样式 一.总结 一句话总结: 这里就是为元素绑定class样式,和后面的style样式区别一下 vue中class样式绑定方式的相对于原 ...
- vue_music:排行榜rank中top-list.vue中样式的实现:class
排行榜的歌曲列表,根据排名渲染不同的样式,同时需要考虑分辨率的2x 3x图 不同的样式--:class 考虑分辨率的2x 3x图--需要在stylu中的mixin中bgImage根据屏幕分辨率选择图片 ...
- vue中数据双向绑定的实现原理
vue中最常见的属v-model这个数据双向绑定了,很好奇它是如何实现的呢?尝试着用原生的JS去实现一下. 首先大致学习了解下Object.defineProperty()这个东东吧! * Objec ...
- vue中滚动事件绑定的函数无法调用问题
问题描述: 一个包含下拉加载的页面,刷新当前页然后滚动页面,能够正常触发滚动事件并调用回调函数,但是如果是进了某一个页面然后再进的该页面,滚动事件能够触发, 但是回调函数在滚动的时候只能被调用一次. ...
- Vue中this的绑定
之前写过一篇文章 ES6与React中this完全解惑 其实Vue也是相同的道理.在Vue的官方文档中提到: 不要在选项属性或回调上使用箭头函数,比如 created: () => consol ...
- Vue中v-for不绑定key会怎样
Vue的v-for不绑定key,默认行为和绑定key="index"是差不多的,官方没有默认这种行为的情况下,会导致所有列表DOM重新渲染.key="index" ...
- vue中的js绑定样式
添加class 对象形式添加 activated为true时p标签的class为activated false时为空 <div id="app"> <p :c ...
- 第二章 Vue快速入门-- 15 vue中通过属性绑定为元素设置class类样式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
随机推荐
- Java生成短链接
为什么要生成短链接 微博之所以要是使用短链接,主要是因为微博只允许发140 字,如果链接地址太长的话,那么发送的字数将大大减少. 那么使用短链接的作用有哪些呢?1.字数,2.统计,3.监控,4.过滤 ...
- Service Mesh
概念 A service mesh is a dedicated infrastructure layer for handling service-to-service communication. ...
- CF1100E Andrew and Taxi
题目地址:CF1100E Andrew and Taxi 二分,每次取到一个 \(mid\) ,只保留长度 \(>mid\) 的边 dfs判环,若有环,说明 \(ans>mid\) ,否则 ...
- MII与RMII接口的区别【转】
转自:https://blog.csdn.net/fun_tion/article/details/70270632 1.概述 MII即“媒体独立接口”,也叫“独立于介质的接口”.它是IEEE-802 ...
- EF 常见异常总结
问题:System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invoca ...
- GBT MBR
MBR最大支持2.2TB磁盘,它无法处理大于2.2TB容量的磁盘.MBR还只支持最多4个主分区——如果你想要更多分区,你需要创建所谓“扩展分区”,并在其中创建逻辑分区. GPT的全称是Globally ...
- mysql select in 怎么优化
2017年7月10日 22:36:54 星期一 原理: 把select where in 变换成 "where = " 或者 "where between and &qu ...
- 【原创】大数据基础之Hadoop(2)hdfs和yarn最简绿色部署
环境:3结点集群 192.168.0.1192.168.0.2192.168.0.3 1 配置root用户服务期间免密登录 参考:https://www.cnblogs.com/barneywill/ ...
- [图形]图形API的两种模式
图形API可以分为retained-mode APIs(保存模式API)和immediate-mode APIs(中间模式API). Direct2D是immediate-mode API,而Wind ...
- 小白6步搞定vue脚手架创建项目
1.安装node及npm node -v (测试node是否安装成功)npm -v(测试npm是否安装成功) 2.安装cnpm npm install -g cnpm --registry=http: ...