vue 點擊選中效果


1、創建

<!--
 * @Description: 菜單DEMO
 * @Author: PengShuai
 * @Date: 2022-02-17 09:16:19
 * @LastEditors: PengShuai
 * @LastEditTime: 2022-02-17 10:27:49
-->
<template>
  <div class="menuDemo">
    <ul>
      <li
        v-for="(item, index) in menuDemoList"
        :key="index"
        :class="activeIndex === index ? 'active' : ' '"
        @click="onClick(index)"
      >
        {{ item.value }}
      </li>
    </ul>
  </div>
</template>

2、數據

  data() {
    return {
      // 數據
      menuDemoList: [
        {
          key: '01',
          value: '測試1',
        },
        {
          key: '02',
          value: '測試2',
        },
        {
          key: '03',
          value: '測試3',
        },
        {
          key: '04',
          value: '測試4',
        },
        {
          key: '05',
          value: '測試5',
        },
      ],
      // 下角標
      activeIndex: null,
    }
  },

3、事件

  methods: {
    // 點擊事件
    onClick(index) {
      this.activeIndex = index
    },
  },

4、樣式表

li {
  list-style-type: none;
}
.menuDemo {
  width: 200px;
  height: 100%;
  font-size: 18px;
  ul {
    li {
      border: 1px solid #ddd;
      text-align: center;
      padding: 20px;
      cursor: pointer;
    }
  }
  // 選中樣式
  .active {
    background-color: red;
    color: #fff;
    position: relative;
    &::after {
      content: '';
      width: 0;
      height: 0;
      border-top: 10px solid transparent;
      border-right: 20px solid #fff;
      border-bottom: 10px solid transparent;
      position: absolute;
      right: 0;
      top: calc(50% - 10px);
    }
  }
}

5、例


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM