Vue2.0点击切换类名改变样式
2017-01-05 16:51
316 查看
Vue2.0点击切换类名改变样式
使用index
通过改变当前值切换类名
使用index
<!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>vue导航</title> <style media="screen"> *{ margin:0; padding: 0; } ul li{ list-style: none; } .navul{ margin:100px auto 20px; overflow: hidden; } .navul li{ background-color: #5597b4; padding:18px 30px; float:left; color: #fff; font-size: 18px; cursor: pointer; } .active{ background-color: #5597b4; } .home .home,.new .new,.contact .contact,.service .service{ background-color: skyblue; } .checked{ background: #eff4f7; } </style> </head> <body> <div id="nav"> <ul> <li v-for="(relation,index) in relations" v-bind:id="relation.id" v-bind:id="relation.id" v-bind:class="{checked:index==nowIndex}" v-on:click="relationClick(index)"> <i></i> <span class="">{{relation.text}}</span> </li> </ul> </div> <script src="js/vue.js" charset="utf-8"></script> <script type="text/javascript"> var nav = new Vue({ el:'#nav', data:{ relations: [ {text:'项目',id:'program'}, {text:'人员',id:'person'}, {text:'机构',id:'organization'}, {text:'技术',id:'tech'}, {text:'地区',id:'location'}, {text:'国家',id:'country'} ], nowIndex:0 // willShow:false }, methods:{ relationClick:function(index){ this.nowIndex=index; } } }); </script> </body> </html>
通过改变当前值切换类名
<!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>点击切换</title> <style> .glyphicon-minus{ color:#08f; } .glyphicon-plus{ color:purple; } </style> <script src="js/vue.js"></script> </head> <body> <div id="box"> <div style="width:100px; height:100px; background: #6bacdf;" v-show="isDisplay"></div> 4000 <div class="form-group"> <button class="btn" :class="[isShow?'btn-primary':'']" @mouseover="toggleShow()" @mouseout="toggleShow()" v-text="btnStyle"></button> </div> <div class=""> <button class="btn" :class="[isTrue?'glyphicon-minus':'glyphicon-plus']" @click="toggleBar()">切换类名按钮</button> </div> </div> <script type="text/javascript"> new Vue({ el:'#box', data:{ isDisplay:false, isShow: false, isTrue:true, btnStyle: 'Default' }, methods:{ toggleShow: function() { this.isShow = !this.isShow; }, toggleBar:function(){ this.isTrue = !this.isTrue; } }, watch: { isShow: function(newVal) { // console.log(newVal); newVal ? this.btnStyle = 'Primary' : this.btnStyle = 'Default' } }, }); </script> </body> </html>
相关文章推荐
- Vue2.0点击切换类名改变样式的方法
- vue2.0 实现click点击当前li,动态切换class
- 点击按钮样式的改变
- 关于使用JavaScript实现图片点击切换(附带改变导航图片 方案二)
- 视图切换(改变视图的样式)
- 点击切换样式
- web前端,jquery实现列表点击条目改变样式并拿到属性值总结1index()
- jq实现点击容器实现2张图片切换(改变src路径)
- jQuerydiv点击添加或者移除样式实现导航切换效果
- 改变当前点击的a标签的样式,将其他a标签样式还原
- 每点击同一个标签,切换两个不同的样式
- JSP页面中JQ实现---当点击按钮的时候,改变原来样式,内容也发生改变,再次点击变成原来的样子
- web前端,jquery实现列表点击条目改变样式并拿到属性值
- 关于a标签,点击后,所有a标签样式改变的问题
- web前端,jquery实现列表点击条目改变样式并拿到属性值总结2attr()
- 利用 Siblings一步实现多个同级div,只改变当前点击的div样式
- angularjs下ng-repeat点击元素改变样式的实现方法
- js控制点击按钮样式切换!
- 原生JS编写图片切换效果和点击按钮的样式变化
- 小程序之点击改变图标样式