最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vuetify: Add custom hover style to v-btn - Stack Overflow

programmeradmin2浏览0评论

I'm a Beginner in Vuetify. I want to create a button on my website like this:

<v-btn class="v-btn white--text mx-1 px-6" elevation="2" x-small rounded color="#BB86FC">text</v-btn>

also am adding <v-hover> tag to my code:

<v-hover>
  <v-btn class="white--text mx-1 px-6" elevation="2" x-small rounded color="#BB86FC">text</v-btn>
</v-hover>

and adding style in my CSS:

.v-btn:hover:

but is not working.

How can I give it a different style to change the background color so when hovering over the button, the color changes to "red"?

I'm a Beginner in Vuetify. I want to create a button on my website like this:

<v-btn class="v-btn white--text mx-1 px-6" elevation="2" x-small rounded color="#BB86FC">text</v-btn>

also am adding <v-hover> tag to my code:

<v-hover>
  <v-btn class="white--text mx-1 px-6" elevation="2" x-small rounded color="#BB86FC">text</v-btn>
</v-hover>

and adding style in my CSS:

.v-btn:hover:

but is not working.

How can I give it a different style to change the background color so when hovering over the button, the color changes to "red"?

Share Improve this question edited Apr 11, 2021 at 9:13 Majed Badawi 28.4k4 gold badges30 silver badges55 bronze badges asked Apr 11, 2021 at 8:35 Miss Tahereh HZMiss Tahereh HZ 731 gold badge1 silver badge8 bronze badges 2
  • Why you are using .v-btn in the css style? – Yash Maheshwari Commented Apr 11, 2021 at 8:45
  • so sorry ..im adding v-btn class ...edid my question now – Miss Tahereh HZ Commented Apr 11, 2021 at 8:53
Add a comment  | 

3 Answers 3

Reset to default 9

You can add v-slot on v-hover and use it in the style-binding of the button as follows:

new Vue({ el:"#app", vuetify: new Vuetify() });
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.js"></script><link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/[email protected]/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet">

<v-app id="app">
   <v-hover v-slot="{ hover }">
     <v-btn 
       class="v-btn white--text mx-1 px-6"
       elevation="2" 
       x-small 
       rounded
       :style="{ 'background-color': hover ? 'red' : '#BB86FC' }"
     >text</v-btn>
   </v-hover>
</v-app>

Also for v-btn you have just directive color, so you can use code like Majed Badawi wrote but change style to :color="hover ? 'red' : 'blue'"

Let's go a little bit deeper..

From Vuetify 3:

The v-hover component provides a simple interface for handling hover states for any component. It is a renderless component that uses the default slot to provide scoped access to its internal model; as well as mouse event listeners to modify it.

From Vue.js 3:

As discussed in Render Scope, slot content does not have access to state in the child component. However, there are cases where it could be useful if a slot's content can make use of data from both the parent scope and the child scope. To achieve that, we need a way for the child to pass data to a slot when rendering it. In fact, we can do exactly that - we can pass attributes to a slot outlet just like passing props to a component.

One implementation example using Vuetify 3 and Vue.js 3 would be like so:

<v-hover v-slot="{ isHovering, props }">
   <v-btn v-bind="props" :color="isHovering ? red : blue" text="cancel" />
</v-hover>

The way v-hover works is by binding the props to the component it needs to listen to, so that it knows when to generate the hover effect. The v-slot is providing a two way binding between the child and parent component through the slot mechanism. So whenever the button is hovered (an event occurs) it communicates with v-hover through the props, making v-hover react by changing the state of isHovering and thus changing the color of the button.

发布评论

评论列表(0)

  1. 暂无评论