Vuetify - List menu activator visibility toggle

两盒软妹~` 提交于 2020-01-25 08:01:05

问题


I have 3 cards when mouse hover on card title it shows a arrow_down icon & i can click on it , it a drop down menu with Delete function when i hover over the v-card-title it shows the icon but after i click on it, mouse on drop-down menu the arrow_down icon disappeared, how to properly implement it ?

https://codepen.io/sharon-the-encoder/pen/WLWyoG?editors=0010

const cards = [
  {
    title: "Gooey PBJ Brownies",
    author: "John Walibur",
    image: "https://placeimg.com/640/480/nature"
  },
  {
    title: "Crisp Spanish Tortilla Matzo Brei",
    author: "Colman Andrews",
    image: "https://placeimg.com/640/480/animals"
  },
  {
    title: "Grilled Shrimp with Lemon and Garlic",
    author: "Celeste Mills",
    image: "https://placeimg.com/640/480/arch"
  }
];

new Vue({
  el: "#app",
  data: {
    cards: cards,
    selectedCard: -1
  },
  methods: {
    hoverCard(selectedIndex) {
      this.selectedCard = selectedIndex
    },

    isSelected(cardIndex) {
      return this.selectedCard === cardIndex
    },
    
    passmsgid(index) {
      alert(index)
    }
  }
});
body {
  background-color: #e1e7e7;
}
.grey--text.selected {
  color: red !important;
}
.iconkey {
  display: none;
}
.iconkey.selected {
  display: block;
  color: blue !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.18/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vuetify/1.4.2/vuetify.min.js">
</script>
<link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/vuetify/dist/vuetify.min.css'>
<link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons'>

<div id="app">
  <v-app>
    <v-content>
      <v-container>Hello world</v-container>

      <v-container fluid>
        <v-layout row wrap>
          <v-flex xs12 sm4 md4 lg4 v-for="(card, index) in cards" :key="index">
            <v-card class="ml-3 mt-3">
              <v-img :src="card.image" height="200px">
              </v-img>

              <v-card-title primary-title @mouseover="hoverCard(index)" @mouseout="hoverCard(-1)">
                <div>
                  <div class="headline">{{card.title}}</div>
                  <span class="grey--text" :class="{'selected': isSelected(index)}">{{card.author}} - 
                        </span>
                  <v-expand-transition>
                    <v-menu bottom transition="scale-transition" nudge-bottom="40">
                      <v-btn icon slot="activator" styole="margin-bottom: -1em;">
                        <v-icon class="iconkey" :class="{'selected': isSelected(index)}">keyboard_arrow_down</v-icon>
                      </v-btn>
                      <v-list>
                        <v-list-tile>
                          <v-list-tile-title @click="passmsgid(index)">Delete</v-list-tile-title>
                        </v-list-tile>
                      </v-list>
                    </v-menu>
                  </v-expand-transition>
                </div>
              </v-card-title>

              <v-card-actions>
                <v-btn flat>Share</v-btn>
                <v-btn flat color="purple">Explore</v-btn>
                <v-spacer></v-spacer>
              </v-card-actions>

            </v-card>
          </v-flex>
        </v-layout>

      </v-container>
    </v-content>
  </v-app>
</div>

回答1:


Updated pen using more CSS, and without v-hover: Codepen

(Originally I used v-hover but for this case not needed) Codepen


We will control visibility by using the following CSS class:

.hidden {
  visibility: hidden;
}

menu-button is hidden unless:
a) we are hovering over its parent-tile or
b) when the corresponding menu is opened.

So we need to set up custom item (tile) component:

Set menu visibility control:

data: () => ({
  menu: false,  // control button-menu state (opened / closed)
})

And our template starts with v-hover component so we can detect when we hover over it and react to that event:

<template>
  <v-hover>
    <v-list-tile slot-scope="{ hover }" @click="">
      // ...
      <v-menu v-model="menu" offset-y left >
        <v-btn slot="activator"
               :class="{hidden: !hover && !menu}"
        >

:class="{hidden: !hover && !menu}" - we set hidden class on button when we are not hovering over the parent-tile and when corresponding menu is closed.



来源:https://stackoverflow.com/questions/54224919/vuetify-list-menu-activator-visibility-toggle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!