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

作者:SilenceJude 时间:2024-04-09 10:45:21 

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="{'click':i==clickIndex}"
  ></li>
 </ul>
</div>
</template>

<script>
export default {
data() {
 return {
  list: [1, 2, 3, 4],
  clickIndex: -1
 };
},
methods: {}
};
</script>
<style scoped>
.item {
display: inline-block;
width: 100px;
height: 100px;
cursor: pointer;
border: 1px solid black;
}
.item:hover {
background: gray;
}
.item.click {
background: red;
}
</style>

补充:vue之v-for中给每个item动态绑定class,动态添加元素,动态删除某个元素的实现

主要解决了在v-for时,如何给每个item添加动态的样式,即是说,鼠标滑动到某一项时,可以单独改变某一项的样式,同时添加按钮等操作。以及删除某一项的操作。


<template>
<div class="hello">
  <ul>
    <li  v-for="(item, itemIndex) in test"
       :key="item.id"
       :class="{defaultClass: itemIndex === isActive}"
       @mouseenter="onMouseEnter(itemIndex)"
       @mouseleave="onMouseLeave">
      {{ itemIndex+1 }} :{{ item.title }}
      <button v-if="isActive === itemIndex" @click="deleteItem(itemIndex)">删除({{itemIndex+1}})</button>
    </li>
  </ul>
</div>
</template>

<script>
export default {
name: 'HelloWorld',
data () {
 return {
  test: [
    {  
      id: 1,
      title: 'title first'
    },
    {
      id: 2,
      title: 'title second'
    },
    {
      id: 3,
      title: 'title third'
    }
  ],
  isActive: ''
 }
},
methods: {
  onMouseEnter(index) {
    this.isActive = index
  },
  onMouseLeave() {
    this.isActive = ''
  },
  deleteItem(index) {
    this.test.splice(index, 1)
  }
},
computed: {

}
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
/* display: inline-block; */
margin:10px;
}
a {
color: #42b983;
}
.defaultClass{
 background-color: red;
}
</style>

总结

以上所述是小编给大家介绍的vue中v-for通过动态绑定class实现触发效果网站的支持!

来源:https://blog.csdn.net/SilenceJude/article/details/84847451

标签:v-for,动态绑定,class
0
投稿

猜你喜欢

  • python turtle库画一个方格和圆实例

    2021-09-25 14:06:10
  • Python装饰器与线程结合提高接口访问效率方法

    2021-07-16 11:28:32
  • MySQL基础教程之事务异常情况

    2024-01-15 06:12:20
  • Websocket通信协议在数字孪生中的应用

    2024-04-30 08:55:46
  • 教你如何通过日志文件恢复MySQL数据

    2024-01-29 07:11:07
  • Python 3.6 读取并操作文件内容的实例

    2021-11-21 16:52:14
  • Python3 Loguru输出日志工具的使用

    2021-11-18 04:28:29
  • sql字符串函数大全和使用方法示例

    2024-01-26 22:17:48
  • 使用有趣的自定义标记布局页面

    2012-07-12 01:29:03
  • php实现mysql备份恢复分卷处理的方法

    2023-11-16 20:55:33
  • 跟我学习javascript的定时器

    2024-05-11 09:32:09
  • Linux下C连接MySQL出现错误解决一例

    2008-12-29 13:17:00
  • 真正保险的“有属性”检测

    2010-03-28 13:25:00
  • OpenCV实现直线检测

    2023-08-14 01:37:35
  • 如何利用python将Xmind用例转为Excel用例

    2022-06-18 19:18:46
  • archlinux 罗技K380 F1-F12 功能键锁定(实现方法)

    2023-11-27 22:42:48
  • ASP生成静态模版技术(带参数的标签)

    2009-03-03 12:29:00
  • django 2.0更新的10条注意事项总结

    2023-05-22 10:48:35
  • 电商网站的购买按钮

    2011-07-04 12:18:59
  • 详解Python中time()方法的使用的教程

    2022-10-07 16:28:23
  • asp之家 网络编程 m.aspxhome.com