Guide to updating div card content when hovering in Vuejs

new Vue({
  el: '#mouse',
  data: {
    message: 'Hover Me!'
  }, 
  methods: {
    mouseover: function(){
      this.message = 'Good!'
    },    
    mouseleave: function(){
      this.message = 'Hover Me!'
    }
  }
})
body {
  
  background: #333;
  
  #mouse {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    display: block;
    width: 280px;
    height: 100px;
    margin: 0 auto;
    line-height: 50px;
    text-align: center;
    color: #fff;
    background: #007db9;

    a {
      display: block;
      width: 100%;
      height: 100%;
      cursor: pointer;
    }
  }
}
<div id="mouse">
  <a
    v-on:mouseover="mouseover"
    v-on:mouseleave="mouseleave">
    {{message}}
  </a>
</div>

How can I change the content of a div card on hover in Vue.js?

Using the code provided above, I have successfully managed to change the content on hover. However, my only issue is that initially, I want to display an image and then show a button within the same div after the user hovers over it.

Answer №1

You have the option to achieve this using CSS:

#myButton {
  visibility: hidden;
}

#mouse:hover #myButton {
  visibility: visible;
}

<div id="mouse">
  <a
    v-on:mouseover="mouseover"
    v-on:mouseleave="mouseleave">
    {{text}}
  </a>
  <button id='myButton'>Click me</button>
</div>

Answer №2

Incorporate a boolean variable like isHover to dynamically display your content based on certain conditions. Set isHover=true when hovering over an element, and isHover=false when moving the mouse away.

Answer №3

Instead of displaying all content within one div, consider implementing conditions to display specific elements at certain times. For example, you can utilize Vue.js's conditional rendering feature as shown in this example. Additionally, you can control the initial display of an image by using a boolean variable such as isFirstTime.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to dynamically include a span element rather than using a Label control in the code?

Is there a way to programmatically add a span tag in the code-behind? If so, is there an equivalent HtmlControl that can be used? Currently, I am following this method where I am constructing rows for a table within an Itemplate implementation. var header ...

Creating a combined Email and Password form field in a single row using Bootstrap

Recently, I decided to explore bootstrap. I am looking to implement email/password validation in one row on my rails app: email? | password? | OK Currently, I have a functioning code for displaying only an email field and an OK button in one row: email? ...

Is there a way to gather selected checkboxes from all child checkbox components in vue?

I have a unique setup where my table rows are generated by child components, each containing a checkbox. I am looking for a way to retrieve all the checked checkboxes at once without using two-way binding or updating an array on the parent component. Here ...

Combining Vue.js with Laravel: Issue of String Stripping in Blade File when Passed as a Prop

In my blade file, I have the following piece of code: $customerId = time() . rand(0, 100000) . '-' . $redItem->id; <Flashbar :user-ref={!! json_encode($customerId) !!} /> The problem I'm facing is that the Flashbar component ...

Creating a VUE application using UML Class and Sequence Diagrams

I'm currently in the process of modeling a portion of my VUE application using UML. I utilized a class diagram to showcase the structure and a sequence diagram to highlight a specific behavior. However, there are a few aspects that I'm uncertain ...

Overriding the !important declaration in inline styles within various CSS IDs and classes

I need to find a way to override an inline !important declaration that is nested inside multiple CSS ids and classes. Let's analyze the following situation: <div class="A"> <div class="B"> <div class="C"> < ...

setting the child div's margin in relation to its parent div

Here is my HTML: <div class='mainDiv'> <div class='subDiv'></div> </div> This is the corresponding CSS code: .mainDiv { margin-top: 200px; width: 700px; height: 800px; background-color: red ...

What steps should be taken to extract the content from the template?

Here is a Vue component to consider <template> <div class="myclass"> <div class="myotherclass">I am inside {{getElementClass()}}</div> </div> </template> <script> export def ...

Is there a way to adjust the font size and make the text bold on an asp:LinkButton?

I am working with this asp control: <asp:LinkButton Text="X" runat="server" /> Is there a way to adjust the text size and make it bold in order to have the X appear like shown in this image: https://i.sstatic.net/ilLm1.png ...

Is it possible to retrieve information from a JSON file using Vue.js 2?

Looking to retrieve data from a JSON file with the json data format. [{"id":81,"body":"There are some reason to fix the issues","created_at":"2017-11-16 11:56:47","updated_at":"2017-11-16 11:56:47"}] I have integrated vue-resource and followed the Vue sy ...

Issue with jQuery click event not activating on initial click but functioning properly on the subsequent click

I am currently implementing the jquery.ime editor in my project, which allows for multi-language editing capabilities. The library I am using can be found here. By default, the language JSON files are loaded on "change" events. However, I would like this f ...

The functionality of box-sizing: border-box does not seem to be working properly when used in

I am currently utilizing the Cleanslate tool to reset all CSS attributes on my widget container along with its children. I have also attempted to apply 'box-sizing: border-box' to all elements, but I am encountering unexpected behavior. Specific ...

Troubleshooting issue: Bootstrap 4 input group not properly showing invalid feedback text

I've been exploring the features of Bootstrap 4 - beta and I've noticed that when using .is-invalid with .input-group, it doesn't seem to be visible. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.cs ...

Is there a way to dynamically alter the text color in a tag based on its value?

Utilizing ajax technology, I aim to dynamically populate a div tag with values from a table. I believe that implementing a loop is crucial in this scenario, but the challenge lies in executing it correctly. My current setup involves xampp for backend php ...

Requesting data asynchronously using AJAX and displaying the filtered results on a webpage using JSON

When I send a request to my node.js server for a .json file containing person information (first name, last name), I want the data to be filtered based on user input. For example: When I request the .json file from the server, it gives me a list of people ...

Unable to get AngularJS ng-repeat to function properly with select dropdown options

While the http part works fine and retrieves the value without any issues, I face a problem when trying to use them in a select option. Upon inspection, all the values are shown but they do not display in the select option. $http({ method: " ...

Utilizing KnockoutJS to Apply CSS Binding Based on Dropdown Selection

Check out the live example here: http://jsfiddle.net/0gmbbv5w/ In my application, I have an object retrieved from the database that I bind to a <select> var NoticeType = function (noticeType) { this.NoticeTypeId = ko.observable(noticeType.Notic ...

Reading values from a properties file using HTML

Here's a snippet of my HTML code: First name: <input type = "text" > Last name: <input type = "text"> Instead of manually inputting the field values (First name, Last name) in the HTML, I am interested in reading them ...

Steps to align div directly to the left of the button that was clicked

I have a div in my code that is hidden by default. I want to show this div exactly left whenever the user clicks on the show button. Since there are multiple show buttons on my page, I would like to display the div as described above. <div id="me ...

The dominance of CSS specificity in favor of the flex property compared to flex-basis

Is there a specificity among CSS properties that affects how styles are applied? I've been experimenting with flexbox and encountered an interesting scenario: The second selector .flex-basis-5 is added dynamically via JavaScript based on certain con ...