溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Vue2.0點(diǎn)擊切換類名改變樣式的方法

發(fā)布時(shí)間:2020-08-28 06:08:38 來源:腳本之家 閱讀:184 作者:香蕉你個(gè)芭吶吶 欄目:web開發(fā)

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導(dǎo)航</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:'項(xiàng)目',id:'program'},
   {text:'人員',id:'person'},
   {text:'機(jī)構(gòu)',id:'organization'},
   {text:'技術(shù)',id:'tech'},
   {text:'地區(qū)',id:'location'},
   {text:'國(guó)家',id:'country'}
  ],
  nowIndex:0
  // willShow:false
  },
  methods:{
  relationClick:function(index){
   
   this.nowIndex=index;
  }
  }
 });
 </script>
</body>
</html>

2、通過改變當(dāng)前值切換類名

<!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>點(diǎn)擊切換</title>
 <style>
 .glyphicon-minus{
  color:#08f;
 }
 .glyphicon-plus{
  color:purple;
 }
 
 </style>
 <script src="js/vue.js"></script>
</head>
<body>
 
 <div id="box">
  <div  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>

拓展知識(shí):vue點(diǎn)擊改變樣式的實(shí)例

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>

以上這篇Vue2.0點(diǎn)擊切換類名改變樣式的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI