vue通过style或者class改变样式的实例代码

通过style改变样式

<div :style="{ 'min-height': size + 'px' }"></div>
<div :style="[{ 'min-height': size + 'px' },{color:'red'}]"></div>
<div :style="{ 'opacity': value ? 0.5 : 1 }"></div> 

通过className改变样式

​<div class="static"
   v-bind:class="{ active: isActive, 'text-danger': hasError }">
</div>

<script>
data: {
 isActive: true,
 hasError: false
}
</script>

<style>
.active{
  ...
}
.text-danger{
  ...
}
</style>

PS:下面看下Vue的一些样式(class/style)绑定

样式有两种:class、style

class

  1、对象语法

    ①传给 :class 一个对象

    比如:

<div :class="{ active : isActive}"></div>

    在这里,isActive的真值决定active这个样式是否显示

    ②传给 :class 一个对象变量

    再比如,可以直接绑定一个对象

<div :class = "duixiang" ></div>

export default{
  data(){
   return{
    duixiang :{
     active : true
    }
   }
  }
}

    ③在②的基础上,把这个对象变量放到computed计算属性里

data: {
 isActive: true,
 error: null
},
computed: {
 classObject: function () {
  return {
   active: this.isActive && !this.error,
   'text-danger': this.error && this.error.type === 'fatal',
  }
 }
}

  2、数组语法

    传给 :class 一个数组(数组里面是变量名,然后具体变量名所指的内容写到data里)

<div :class = "[ n , i]"> </div>
export default{
 data(){
  return{
   n : ' active ',
   i : ' text-danger ',
  }
  }
}

     上面的代码在最后会宣传成为<div class = "active text-anger"></div>

style

  1、对象语法

    ①传给 :style一个对象(这个对象是个JavaScript对象)。记住!CSS属性名可以用驼峰式命名

<div :style = " { color : activeColor , fontSize : fontSize + 'px' } "></div>
//然后在data里面写明,冒号后边的这个变量的实际内容,如果是单位,像px这种就直接用字符串引着就行了
data: {
 activeColor: 'red',
 fontSize: 30
}  

    ②传给 :style一个对象变量。

<div v-bind:style="styleObject"></div>
//然后在data声明这个对象变量是指代一个怎样具体的对象
data: {
 styleObject: {
  color: 'red',
  fontSize: '13px'
 }
}

同样的,对象语法常常结合返回对象的计算属性使用。

总结

以上所述是小编给大家介绍的vue通过style或者class改变样式的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

(0)

相关推荐

  • vue中v-for通过动态绑定class实现触发效果

    vue动态绑定class练习. :class="{ '类名1':条件表达式,'类名2':条件表达式- }" <template> <div class="app-*"> <ul> <li v-for="(item,i) of list" :key="i" class="item" @click="clickIndex=i" :class=&quo

  • vue添加class样式实例讲解

    vue提供了一个动态添加class的v-bind:class(:class) 对象,可以使用:class进行clas动态的切换.案例中将通过使用:class设置 字体为红色. 1.新建一个html代码页面. 2.在html代码页面创建一个<div>同时添加id为app,并添加一段文本 3.引入vue.js文件.使用<scrtip>标签引入vue文件. 4.创建vue实例.新建一个<script>标签,然后使用new Vue()创建实例. 代码: <script&g

  • vue动态绑定class选中当前列表变色的方法示例

    这个小技巧在工作当中是非常实用而且经常用到的  希望小伙伴儿们能学到. 先看看效果图吧 接下来我们看看怎么实现的吧 在methods中写入一个方法 clickcategory(index){ // 这里我们传入一个当前值 this.categoryIndex = index } 然后需要在data里面注册一下 data() { return { categoryIndex: 0, //点击当前背景变成白色索引 } }, 在css中设置我们当前选中项为active的类名,并给与一个白色的背景颜色

  • vue根据值给予不同class的实例

    如下所示: <div class="chatBox-kuang" :class="addclass(skin)"> </div> data(){ return{ skin:'' } } onchooseSkin(attr){ this.skin=attr }, 方法一 addclass(i){ switch (i) { case 0: return 'skinA'; case 1: return 'skinB'; case 2: return

  • 在vue中使用v-bind:class的选项卡方法

    //vue中的选项卡的实现,数据驱动dom,因此需要使用数据,来改变class: 详细见代码实现 <style> ul{overflow: hidden;} ul li{float: left;width: 150px;height: 35px;line-height: 35px;border: 1px solid red;list-style: none;cursor: pointer;} ul li.active{background-color: yellow} </style&g

  • vue--点击当前增加class,其他删除class的方法

    如下所示: <div id="app"> <p v-for='(data,key,index) in datas' v-on:click="addClassFun(index)" v-bind:class='{class1:index==qwerqwre}'>{{data.data}}</p> </div> <script> new Vue({ el: '#app', data: { datas: { da

  • vue通过style或者class改变样式的实例代码

    通过style改变样式 <div :style="{ 'min-height': size + 'px' }"></div> <div :style="[{ 'min-height': size + 'px' },{color:'red'}]"></div> <div :style="{ 'opacity': value ? 0.5 : 1 }"></div> 通过class

  • vue滚动固定顶部及修改样式的实例代码

    滚动固定位置有多种方法 1 css3  粘性定位 position:sticky: top:20px: 2直接position:fixed:给顶部盒子设置一个margin-top刚好是需要固定的盒子的高度 3事件监听更改style中的position属性 ** 修改样式 滚动监听事件中使用this.$refs.xxx.style.color='xxxx' 这种方式会报错 Uncaught TypeError: Cannot read property 'style' of undefined 所

  • vue中选中多个选项并且改变选中的样式的实例代码

    1:HTML: <ul class="content"> <li v-for="(item,index) in touristList" @click="onStorage(item,index)" :class="{'active': rSelect.indexOf(item)!=-1}" :key="item.id"> <div>{{item.name}}</d

  • 对angularJs中ng-style动态改变样式的实例讲解

    如下所示: <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <script src="angular.min.js"></script> </head> <body> <div ng-app="module" ng-controller="

  • vue里input根据value改变背景色的实例

    1.首先定义两个不同的 .null-input .el-input__inner { background-color: rgba(255, 255, 255, 0.8); color: #525661; font-size: 16px; } .no-null-input .el-input__inner { background-color: rgba(255, 255, 255, 1); color: #524F52; font-size: 16px; } 2.根据:class 控制inpu

  • vue使用一些外部插件及样式的配置代码

    一.配置全局css及js样式 1.首先将事先写好的css文件及js文件放在src文件目录下的assets文件下 2.在main.js文件输上图右边两个红色框的代码 二.配置全局jQuery及bootstrap 安装插件步骤: >: cnpm install jquery vue/cli 3 配置jQuery:在vue.config.js中配置(没有,手动项目根目录下新建) const webpack = require("webpack"); module.exports = {

  • 使用vue制作探探滑动堆叠组件的实例代码

    效果图如下所示: 前言 嗨,说起探探想必各位程序汪都不陌生(毕竟妹子很多),能在上面丝滑的翻牌子,探探的的堆叠滑动组件起到了关键的作用,下面就来看看如何用vue写一个探探的堆叠组件 一. 功能分析 简单使用下探探会发现,堆叠滑动的功能很简单,用一张图概括就是: 简单归纳下里面包含的基本功能点: 图片的堆叠 图片第一张的滑动 条件成功后的滑出,条件失败后的回弹 滑出后下一张图片堆叠到顶部 体验优化 根据触摸点的不同,滑动时首图有不同角度偏移 偏移面积判定是否成功滑出 二. 具体实现 有了归纳好的功

  • Vue实现按钮旋转和移动位置的实例代码

    1.静态效果图 Chrom移动端浏览模式下可拖动按钮处于任意位置,并且点击可旋转按钮 2.代码 <template> <div id="app"> <div class="icon-add-50" :style="iconstyle" @click='click' @touchmove='touchmove' @touchstart='touchstart(this,$event)' @touchend='touch

  • Vue的属性、方法、生命周期实例代码详解

    实例 <!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"

  • Vue.js动态添加、删除选题的实例代码

    大家先看看页面效果吧,当当当当``````````````````````` 图中第二个选题是小颖点击了"新增选题"按钮,然后出来的,当你点击了"删除选项"或"删除选题"按钮,就会删除相应的选项和选题. html代码 <template> <div class="main-container"> <div class="form-horizontal"> <temp

随机推荐