欢迎来到代码驿站!

vue

当前位置:首页 > 网页前端 > vue

Vue2.0点击切换类名改变样式的方法

时间:2021-04-06 10:02:30|栏目:vue|点击:

1、使用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>

2、通过改变当前值切换类名

<!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>
 
  <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>

拓展知识:vue点击改变样式的实例

data里

isActive:-1,

method里

checkedItem(index){
 this.isActive=index;
},

页面里

<div v-for="(item,index) in nameoptions" v-bind:class="{active:index==isActive}" @click="checkItem(index)>{{item.name}}</div>

上一篇:浅谈vue+webpack项目调试方法步骤

栏    目:vue

下一篇:Vue中保存数据到磁盘文件的方法

本文标题:Vue2.0点击切换类名改变样式的方法

本文地址:http://www.codeinn.net/misctech/95712.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有