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 ...
随机推荐
- Javascript - ExtJs - 事件
事件(ExtJs Event) Ext.Util.observable类 Ext.Util.observable是一个接口,为Ext组件的事件提供了支持,组件的事件不同于传统事件,所以需要有这么一套 ...
- 配置中文分词器 IK-Analyzer-Solr7
先下载solr7版本的ik分词器,下载地址:http://search.maven.org/#search%7Cga%7C1%7Ccom.github.magese分词器GitHub源码地址:http ...
- python小练习,密码等级问题
. # 密码安全性检查代码 . # . # 低级密码要求: . # . 密码由单纯的数字或字母组成 . # . 密码长度小于等于8位 . # . # 中级密码要求: . # . 密码必须由数字.字母或 ...
- python第五天,两个知识点三目运算符和assert抛异常处理。
在python 2.5x之前是没有三目运算符的,但随着语言的发展,在2.5之后就加入了三目运算符 ''' 这里主要将三目运算符 其中也可以通过 x,y=4,5这种方式进行快速的声明变量和对变量赋值. ...
- mybatis-plus调用自身的 selectById 方法报错:org.apache.ibatis.binding.BindingException:
mybatis-plus的版本号是 2.0.1,在调用自身的insert(T)的时候没有报错,但是执行update报错,调用selectById.deleteById的时候也报错.也就是涉及到需要主键 ...
- MyBatis-进阶1
接入门的实例,我们知道MyBatis可以使用注解和配置文件实现接口和sql语句的绑定. 那么一个接口方法同时使用注解和xml配置会怎么样. @Select("select * from us ...
- MongoDB 和 NoSQL简介
MongoDB 是一个基于分布式文件存储的数据库( https://www.mongodb.com/ ).由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案.MongoDB ...
- Fusebox 类似WEBPACK 的工具,React Studio
Fusebox 类似WEBPACK 的工具, http://fuse-box.org/ React Studio: https://hackernoon.com/@reactstudio
- CentOS7利用systemctl添加自定义系统服务【转】
systemctl enable name.service 设置开机启 systemctl disable name.service 删除开机启动指令 systemctl list-units --t ...
- vc++基础班[23]---文件夹的基本操作
①.文件夹的创建:CreateDirectory ※※※ 注意:此函数只能创建一层目录,比如想在 C 盘下的 Temp 目录下创建新目录为:123 那么前提是 Temp 这个目录存在才可以! ...