Creating an image rollover effect when hovering between two images

I am currently working with React and trying to change 2 images by hovering over them.
Here is what I have accomplished so far:

Card.js

<div className="image-wrapper">
    <img src={sprites.front_default} className="image" alt="normal" />
    <img src={sprites.back_default} className="image-hover" alt="hover" />
  </div>

style.css

.image-wrapper {
  position: relative;
}

.image-hover {
  position: absolute;
  top: 0;
  left: 0;
  opacity: 0;
  transition: opacity 1s ease-out;
}
.image-hover:hover {
  opacity: 1;
}

The main challenge I am facing is how to reduce the front image's opacity to 0 when hovering over the div. How can I achieve this?

I am able to achieve it without a fade effect, but transitioning the opacity is important.
This is how I do it without transition

<img
        className="figure"
        src={sprites.front_default}
        onMouseOver={(e) => (e.currentTarget.src = sprites.back_default)}
        onMouseOut={(e) => (e.currentTarget.src = sprites.front_default)}
        alt={"pokemon"}
      />

Answer №1

Utilize HTML & CSS to create a dynamic image transition effect within the Card design:

<div className="image-wrapper">
  <img
    src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/1.png"
    className="pokemon-image"
    alt="normal"
  />
  <img
    src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/2.png"
    className="pokemon-image pokemon-image--back"
    alt="normal"
  />
</div>

A single class pokemon-image is used to center align two images within the image-wrapper element, whereas pokemon-image--back is displayed during hover over the image-wrapper.

CSS styling applied includes:

.image-wrapper {
  width: 200px;
  height: 150px;
  background-color: #f7f7f7;
  position: relative;
}

.pokemon-image {
  position: absolute;
  height: 100%;
  left: 50%;
  transform: translateX(-50%);
}

.pokemon-image--back {
  opacity: 0;
  transition: opacity 1s ease-out;
}

.image-wrapper:hover .pokemon-image--back {
  opacity: 1;
}

Experience it firsthand in this codesandbox environment.

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

Facing difficulties with implementing AWS Amplify Authenticator in NextJS version 13.4

I am looking to transition from CRA to Next.js, but I am encountering issues when trying to integrate AWS Amplify authentication. The amplify login form displays correctly, but when attempting to sign in, I receive the following error: Error [ERROR] 40:38 ...

Why is it that vanilla HTML/JS (including React) opts for camelCase in its styling conventions, while CSS does not follow the same pattern

Each type of technology for styling has its own set of conventions when it comes to naming properties, with camelCase and hyphenated-style being the two main options. When applying styles directly to an HTML DOM Node using JavaScript, the syntax would be ...

Tic Tac Toe is not functioning properly

Hey there! Can someone please help me figure out what's going on? I'm trying to create a Tic Tac Toe game, but instead of using "O" and "X" to mark the fields, I want to use different colors. Specifically, one player should be able to mark with b ...

Resolved plugin issue through CSS adjustments

Take a look at this template 1) After referring to the above template, I developed a fixed plugin using JavaScript. 2) Clicking the icon will trigger the opening of a card. 3) Within the card, I designed a form using mdb bootstrap. Everything seems to ...

Strange formatting in the internet explorer browser (IE)

I have encountered an issue with CSS animation elements on the home page. The animations appear correctly in Google Chrome, but there is a strange indentation problem when viewed in IE. I have tried several solutions without success. Although I am not a w ...

Design a fluid row that allows for horizontal scrolling with text alignment options on the left, center, and right side

I created a row with horizontal scrolling on smaller screens. Is there a way to have text aligned left, center, and right all on the same line? I want to achieve this with text aligned in multiple positions. Check out the demonstration on Stackblitz C ...

transfer information between different express middleware functions within a universal react application

I am working on an isomorphic react app and I am looking for a way to pass state between express middleware functions. One of my express routes handles form submission: export const createPaymentHandler = async (req: Request, res: Response, next: NextFun ...

How can I achieve unique spacing between rows in material-ui grid components?

What is the most effective method for creating spacing between specific rows in a grid using material-ui? Consider the following grid with spacing={0}: GridContainer row1 GridItem GridItem row2 GridItem GridItem row3 GridItem GridItem row4 Gr ...

Describe a Typescript Interface Value as a collection of strings or any input provided as an argument

Uncertain if the question title is accurate - currently developing react components within a library that has specific predefined values for certain properties. However, additional values may need to be added on a per usage basis. So far, this setup is fun ...

Tips for adding spacing when the sidebar collapses and expands in a React application

I am attempting to achieve a layout where the body adjusts its space when the sidebar collapses and expands. Here is how it appears when the sidebar expands: see expanded sidebar here And this is how it looks when the sidebar collapses: see collapsed s ...

Font-face src with local() is incompatible with Android devices

(I apologize for not discovering this earlier, but it seems that Chrome-Android also does not support the font I intended to use. The fallback-to-sys-default-font-trick used by Chrome-Android did fool me.) I had planned to incorporate unicode-range to add ...

Narrowing Down State Types

I am working on a functional component in NextJS with props passed from SSR. The component has a state inside it structured like this: const MyComponent = ({err, n}: {err?: ErrorType, n?: N})=>{ const [x, setX] = useState(n || null) ... if(e ...

What is the correct way to input the 'name' HTML attribute in an Ant Design select element?

I am facing an issue with the 'name' attribute in my Ant Design Select component. When trying to set the 'name' attribute, I encountered an error message that is causing issues. https://i.stack.imgur.com/Lzb4t.png Ant Design Select Co ...

What is the fastest way to emulate CSS3 box radius for IE in terms of rendering speed?

There are numerous options available in JavaScript, .htc, and jQuery to create rounded corners for IE 6,7,8. (Not compatible with IE 8) http://code.google.com/p/jquerycurvycorners/ http://code.google.com/p/curvycorners/ Which solution offers the faste ...

Trouble with loading @testing-library/jest-dom

I've been trying to integrate React Testing Library into my Create React App project, but I've run into a peculiar issue. In one of my tests, I need to utilize jest-dom, but for some reason, I can't seem to import it successfully. After add ...

Having trouble with the contact form? It seems to be experiencing issues with

Hey there! I'm encountering an issue with the following codes, where I'm getting redirected and receiving an error message stating "Undefined variable: subject in C:\wamp\www\boot\send.php on line 42." Any help would be greatl ...

The onClick event in monaco-editor/react is not triggering as expected

I've successfully integrated monaco-editor/react into my Next.js application, and all is working smoothly. Now, I am facing an issue with the onClick event of the editor not firing. Here's the code snippet: import Editor from "@monaco-editor ...

There was an issue with the NextJS axios request as it returned a status code

I'm currently in the process of developing an application with NextJS and Strapi In my project, I am fetching data from Strapi using Axios within NextJS Next: 14.0.4 Axios: ^1.6.5 Strapi: 4.17.1 Node: 18.17.0 Here is the code snippet: import axios f ...

Display a menu using Material-UI when an IconButton is clicked

While exploring the material-ui website, I am attempting to implement a feature where a menu is displayed when an IconButton is clicked. However, it seems like there is quite a bit of code required for this functionality. Can someone explain the purpose ...

"What is the best approach to adjusting the width of one div based on the width of another div using CSS Grid

As a beginner in programming, I'm trying to work with CSS Grid but facing some challenges. My goal is to create a component with two columns: the left column should have a width of minmax(570px, 720px), and the right column should be minmax(380px, 10 ...