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

How to effectively leverage useMediaQuery in material ui?

Upon completing the web application, I have made the decision to ensure it is mobile-friendly. Utilizing the material UI framework with react, I delved into the documentation but found myself uncertain about how to effectively implement it. Let me provide ...

A guide to dynamically incorporating form fields in React.js

I am looking to dynamically add form fields. Additionally, I want the durationprice to be added dynamically on click, along with a button to dynamically add a full plan. { plan: [ { planname: "", description: "", cuisine: "", ...

Tips for successfully navigating the match with render in the Route component from react router (v4)

Setting up a route in the following manner raises an issue: In App.js <Route path="/:id" component={RunningProject} /> To access the id param in RunningProject.js, I can use constructor(props){ super(props) console.log(props.match.params. ...

Contrast between the structure of asp.net button and hyperlinks

I want to transfer the styles of a hyperlink in regular html to an Asp.net button. However, I encountered a strange background issue that is disrupting the appearance of the buttons. Hyperlink: Asp.net Button How can I remove the outline that appears o ...

With a simple click of a button, I aim to have the ability to set a variable to true

I am looking to create a button that changes to true in the Script section. This will allow for a random number to be displayed in the paragraph element. <!doctype html> <html> <button id="button"> random number </button> ...

When a function is used to set state in the constructor, React may fail to render

I am having trouble initializing the page based on the user's login status. I attempted to set the initial state using a function, but the view is not updating after the API call is completed. Currently, when the code is executed, I see Part 3. im ...

Is it possible for me to develop a unique NPM package for a custom component library using Material UI as a

I am interested in utilizing MaterialUI as the foundational package (@material-ui/core), customizing components to align with my brand and style guidelines, and creating my own component library as an NPM package. This involves customizing @material-ui/cor ...

Guidelines for pinpointing local directories

Recently, I obtained a source code for a website navigation that includes a unique set of icons known as "Typicon." The HTML in the source code contains the following: <link rel="stylesheet" type="text/css" href="css/component.css" /> Within the C ...

The text feature for the Google Places API is not displaying properly within the MUI TextField

For address auto-generation, I am currently utilizing the Google Places API. I have a regular input tag in my HTML where users can type their input and see Google Places suggestions in the dropdown - all working perfectly. However, when I switch to using m ...

Synchronizing timers among different elements

Just a little context: I'm diving into the world of React and currently working on a small app using next.js (with a template from a tutorial I followed some time ago). Lately, I've encountered a challenge where I need to synchronize a timer betw ...

Hiding HTML Labels with Jquery

I have a label that I want to hide initially and then display with the resultant values when the Submit event of the dialog box occurs. The label should not be visible at first, which is correct. However, even after clicking the submit button, it is not sh ...

I'm encountering a problem with my dynamic routing in next.js react where no information is being displayed and all I see is a 404

Currently facing challenges with dynamic routes implementation in Next.js. Here is the scenario: I have set up getStaticPaths and getStaticProps on my dynamic page. Upon clicking on an item that should direct me to the dynamic route, the browser takes me t ...

Traversing through each element using Array method

I need to create a redux function that will add a specified number n to the fourth element of an array every time a button is clicked. However, if the element is either L or M, I do not want the addition to occur. For example, starting with this initial a ...

Guide on deleting an element from an object array based on the content of a specific field (resulting in undefined mapping)

I am working on integrating a task list feature using React. I have created a state to store the id and content of each task: this.state = {tasks: [{id: 123, content: 'Walk with dog'}, {id: 2, content: 'Do groceries'}]} Adding elements ...

The password-protected HTML blog remains hidden until the correct entry is entered into the password box, causing

After successfully creating a password redirect page where entering the correct password redirects you to another URL (psswrdtest.tumblr.com with the password as correctpsswrd, redirecting to google.com*), I attempted to improve it by making the password p ...

How can I modify the text color of the Navbar-Brand class in Bootstrap NavBar?

I have been experimenting with various options to change the text color of the `navbar` and specifically the `navbar-brand` item. However, my `.navbar-brand {color: purple;}` CSS class doesn't seem to be working. Can someone please assist me in determ ...

Oops! There seems to be an issue with making a request to https://www.gstatic.com/firebasejs/releases.json when trying to initialize

I am facing an issue while trying to deploy a react website (created with npx create-react-app) on firebase. After logging in, when I run the firebase init command, it gives me an error. Here is the complete log: > ? Are you ready to proceed? Yes ? Whi ...

Issues with Jquery datatable causing column header to overlap the table header

I am facing an issue with my datatable as the table is not displaying correctly. Could someone advise me on how to troubleshoot and fix this problem? Below is the code snippet in question: $(tbName).dataTable({ "sScrollX": "100%", "sScro ...

What are the reasons behind the difficulty in deploying my Vite app on Vercel?

Currently, I am working on developing a social media app with Vite. However, I am facing challenges when trying to deploy it. I have included my vite.config.js file below for better insight into the issue at hand. The main issue is that while the ./ route ...

Why is the Material UI React select not selecting all children except for the last one?

I have a challenge with selecting specific children: const useStyles = makeStyles(theme => ({ icons: { "&>*": { backgroundColor: "red", }, }, })) This is how it looks in JSX: <Grid item> < ...