One way to showcase content when hovering over a specific card in vue.js

In my DisplayBooks component, I have successfully implemented the functionality to fetch and display books from the backend. However, I now face an issue where I want to show the description of a book when hovering over its card. The backend response includes the necessary information for this. Can someone please guide me on how to achieve this?

DisplayBooks.vue

<template>
<div class="carddisplay-section">
    <div v-for="book in books" :key="book.id" class="card book">
        <div class="image-section">
            <div class="image-container">
                <img v-bind:src="book.file" />
            </div>
        </div>
        <div class="title-section">
            {{book.name}}
        </div>
        <div class="author-section">
            by {{book.author}}
        </div>
        <div class="price-section">
            Rs. {{book.price}}<label class="default">(2000)</label>
        </div>
        <div class="description">
            {{book.description}}
        </div>
        <div class="buttons">
            <div class="button-groups"  v-if="!addedBooks.includes(book.id)">
                <button type="submit" @click="handleCart(book.id);toggle(book.id);addedBooks.push(book.id)" class="AddBag">Add to Bag</button>
                <button class="wishlist">wishlist</button>
            </div>
            ...
            
        </div>
    </div>
</div>
</template>

Answer №1

To have an additional flag within the `book` object, you can create a property called `hover`.

After receiving data from the backend, you would need to assign this property to each book. One way to do this is by using the `map` function:

mounted() {
    service.userDisplayBooks().then(response => { 
        let data = response.data;
        data.map(function(obj) {
            obj.hover = false;
            return obj;
        });

        this.books.push(...data); 
        return response; 
    })
},

Alternatively, you can achieve this more concisely with `Object.assign`:

mounted() {
    service.userDisplayBooks().then(response => { 
        this.books.push(Object.assign({}, ...response.data, {hover:false}));
        return response; 
    })
},

To track if someone hovers over a book, you can use `@mouseover` and `@mouseleave` events like so:

<div v-for="book in books" :key="book.id" 
     class="card book" 
     @mouseover="book.hover = true"
     @mouseleave="book.hover = false">

Another approach is to set the `hover` property dynamically without prior initialization:

<div v-for="book in books" :key="book.id" 
     class="card book" 
     @mouseover="$set(book, 'hover', true)"
     @mouseleave="$delete(book, 'hover')">

Furthermore, you can control the visibility of a description using `v-if` based on the `hover` status:

<div class="description" v-if="book.hover">

This will ensure that the description is displayed only when a user hovers over a particular book.

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

Ascending row placement

I'm currently working on a JavaScript timeline project, where clicking on an item in the left column reveals details in the right column. While there may be simpler methods to achieve this, the HTML code below appears to be quite elegant. Typically, ...

The Bootstrap grid feature is malfunctioning on both Codeply and devtools. The dimensions of the <div class="col-lg-3 col-md-4 col-sm-6"> element remain constant despite attempts to adjust them

After experimenting with the viewport meta tag and enclosing it in a div class="container", I found that the issue persists on Codeply and Chrome DevTools. However, when I adjust my browser size, the problem seems to disappear. I also tried using various v ...

Tips on creating a stationary sidebar that ends at the container's edge

I currently have a container set up with two columns, where one column acts as a sidebar. JSFiddle The sidebar is positioned as fixed, and I've utilized jQuery to adjust the bottom property so that it roughly stays at the bottom of the container as ...

Utilizing mixins with async components in VueJS

Currently, I am utilizing Webpack 2 to import components using a special syntax with require. Among the over 100 components available, only around 5-10 are used at any given time. These components share some common functionality such as props and lifecycl ...

Problem with rendering Ionic v2 HTML in Angular 2

Issue at Hand: Currently, I am developing a hybrid app using Ionic v2 and everything seems to be functioning correctly. When I use ionic serve, the app works as expected in the browser with no issues. However, when I try running the app on an Android devi ...

Executing a JavaScript function from one page on a separate webpage

I am trying to link three pages together: dashboard.html, documents.jsp, and documents.js In the dashboard.html file, I have a hyperlink that should take the user to documents.jsp and also trigger a function in the documents.js file, which is included in ...

Using VueJS to access the user's full name as a property in the computed method

Looking to calculate the user.fullName using computed() in VueJS 2. The computed property should be within the user object. computed: { 'user.fullName': () => { return `John Doe` } } However, it's not functioning as expected... ...

having difficulty retrieving website content using the ui.router

This is my HTML code: <!doctype Html> <html lang="en"> <head > <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/l ...

What are the steps to resolving the '1' issue in my restaurant.dtd.xml document?

Having trouble with XML Can anyone explain why this specific file is causing an error? file: restaurant.xml <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/css" href="restaurant.css"?&g ...

Utilizing media queries for responsive design in CSS

I've been working on aligning the page content for different browser sizes using CSS. However, I'm having an issue with the first @media statement - no matter what changes I make in there, it doesn't affect the layout. I've been using ...

Using Google-Prettify with AngularJS

I have been attempting to implement Google Prettify in my AngularJS project. It seems to be functioning correctly on the main page, but once I navigate to another route using Angular (after ng-view), it stops working properly. You can check out the plunker ...

I'm having trouble getting the second controller to function properly in my AngularJS application

I've been looking everywhere for a solution on how to implement two controllers, but I can't seem to get it working. Could there be something wrong with my HTML or could the issue lie in my script? Here is the JavaScript code: <script> v ...

What is the best way to retain the background image in a fixed position?

Is there a way to fix the size of the picture in my code? The current size is 5834*3886, but when I minimize the browser, part of it disappears. Here is the CSS code I have written: .contact{ position: relative; min-height: 100vh; padding: 50 ...

Unable to link JavaScript to HTML file through script tag

Upon testing out a responsive nav bar, I encountered an issue with the JavaScript not connecting. Despite placing the script tag at the bottom of the body as recommended, it still fails to function. index.html <html lang="en"> <head> ...

Tutorial on implementing a _variables.scss file for Vue components with sass-resource-loader on Vue CLI 3.04

In my VueJs project created with the Vue CLI 3.0.4, I am aiming to utilize SCSS variables across all components without the need to import _variables.scss into each one individually. After some research, I found that I can achieve this using sass-resource- ...

Having trouble loading a JavaScript file in Nuxt? Experiencing some unexpected behavior?

I have a template with HTML/CSS/JS files that I want to make dynamic using Nuxt.js. I started by copying the index.html to the Nuxt project and transferring all the necessary data to nuxt.config.js, including CSS and JS files. The page renders without erro ...

Obtaining Google AAID Using a Mobile Browser (JavaScript)

I've been looking for information online without much success regarding this topic. I am interested in retrieving a user's Google Advertising ID (AAID) through my website when they visit using a mobile browser. I assume it could be done with som ...

Scroll effortlessly to the middle, bottom, and top of the page in vue.js using arrow keys for a seamless

I'm interested in implementing a feature that allows users to scroll smoothly up and down the page using the arrow keys. When the first down key is pressed, I'd like to smoothly scroll to the middle (50%) of the page, and when the second down ...

How can ngValue be leveraged with Angular Material's autocomplete feature?

I need to pass an object as my value and use [ngValue] instead of [value]. This is how my HTML is structured: <mat-input-container fxFlex="18" fxFlexOffset="1"> <input matInput placeholder="Example" [matAutocomplete]= ...

The W3C validator does not recognize any modifications made after jQuery and JavaScript have been applied

My goal is to ensure that my website passes all errors on the W3C Validator. I have encountered a few errors related to the alt text of images. In an attempt to address this issue, I wrote and added the following code to the <head> <script type=" ...