React text hover image functionality

Just diving into the world of React and attempting to create a cool image popup effect when you hover over text in my project. I could really use some guidance on how to make it function within React, as the logic seems a bit different from plain HTML. Any help or insights would be greatly appreciated! Here's the code snippet for reference:

Check out this video for an example using plain HTML

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    vertical-align: middle;
}

:root {
    /* COLORS */
    --background1: #5a5d59;
    --background2: #323432;
    --font_color:  #cececd;
}

/* CUSTOM CURSOR */
.hasCursor {
    cursor: -webkit-image-set(url(mailto),pointer;
}

.hasCursorORANGE {
    cursor: -webkit-image-set(url(mailto),pointer;
}

/* BODY */

.main-body {
    background: linear-gradient(to top left, var(--background1), var(--background2));
    height: 100vh;
    padding: 0 7rem;
    padding-top: 45vh;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    /* COUNTER*/
    counter-reset: section;
}

/* TEXT */

.text-tag > p {
    position: relative;
    z-index: 2;
    font-size: 60px;
    letter-spacing: 1px;
    color: var(--font_color);
}

/* COUNTER*/

.text-tag > p::after {
    position: relative;
    top: -45px;
    padding-left: 15px;
    font-size: 18px;
    counter-increment: section;
    content: counter(section,decimal-leading-zero);
}

/* IMAGE POSITIONING */

.img-container {
    position: relative;
    z-index: 0;
    top: -230px;
    left: 15px;
    opacity: 0;
    visibility: hidden;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>#CodingTrends nº2</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>

    <div class="main-body hasCursor">
        
        <div class="text-tag">
            <p onmouseover="imageIn(getImage1)" onmouseout="imageOut(getImage1)" class="hasCursorORANGE">Ceramics</p>
            <div class="img-container">
                <img src="/img_1.png" alt="Ceramic Vase">
            </div>
        </div>  

        <div class="text-tag">
            <p onmouseover="imageIn(getImage2)" onmouseout="imageOut(getImage2)" class="hasCursorORANGE">Oil Painting</p>
            <div class="img-container">
                <img src="/img_2.jpg" alt="Classic painting - The Girl with a Pearl Earing">
            </div>
        </div> 

        <div class="text-tag">
            <p onmouseover="imageIn(getImage3)" onmouseout="imageOut(getImage3)" class="hasCursorORANGE">Sculpture</p>
            <div class="img-container">
                <img src="/img_3.png" alt="Classic Greek Sculpute of a bust">
            </div>
        </div>

    </div> 


    <script>
        var getImage1 = document.getElementsByClassName("img-container") [0];
        var getImage2 = document.getElementsByClassName("img-container") [1];
        var getImage3 = document.getElementsByClassName("img-container") [2];

        function imageIn(img) {
            img.style.opacity = "1";
            img.style.visibility = "visible";
            img.style.transition = "opacity .5s";
        }

        function imageOut(img) {
            img.style.opacity = "0";
            img.style.visibility = "hidden";
            img.style.transition = "opacity .5s";
        }

    </script>


</body>
</html>

This is the React Code but I really don't know if its the correct approach. Please try to see the video to understand the issue.

class Index extends React.Component {
  state = {
    img: "assets/test/img_1.png",
    img2: "assets/test/img_2.jpg",
    img3: "assets/test/img_3.png",
    imageChange: "nothing"
  }

  getImage1 = document.getElementsByClassName("img-container") [0];
  getImage2 = document.getElementsByClassName("img-container") [1];
  getImage3 = document.getElementsByClassName("img-container") [2];

  imageAppear(img) {
    console.log(img.className);
    img.style.opacity = "1";
    img.style.visibility = "visible";
    img.style.transition = "opacity .5s";
  }

  imageDissappear(img) {
    img.style.opacity = "0";
    img.style.visibility = "hidden";
    img.style.transition = "opacity .5s";
  }
  


  render() {
    var getImage1 = document.getElementsByClassName("img-container") [0];
    var getImage2 = document.getElementsByClassName("img-container") [1];
    var getImage3 = document.getElementsByClassName("img-container") [2];
    
    return (
    <>
    <section className="pb-16 bg-blueGray-200 relative pt-32">
      <div
        className="-mt-20 top-0 bottom-auto left-0 right-0 w-full absolute h-20"
        style={{ transform: "translateZ(0)" }}
      >
        <svg
          className="absolute bottom-0 overflow-hidden"
          xmlns="http://www.w3.org/2000/svg"
          preserveAspectRatio="none"
          version="1.1"
          viewBox="0 0 2560 100"
          x="0"
          y="0"
        >
          <polygon
            className="text-blueGray-200 fill-current"
            points="2560 0 2560 100 0 100"
          ></polygon>
        </svg>
      </div>
            
            <div class="main-body hasCursor">
              <div class="text-tag">
                <p class="hasCursorORANGE"
                onMouseEnter={() => {this.imageAppear(getImage1)}} 
                onMouseOut={() => {this.imageDissappear(getImage1)}} >Ceramics</p>
                
                <div class="img-container" >
                  <img src={this.state.img} alt="Ceramic Vase"></img>
                </div>
              </div>

              <div class="text-tag">
                <p class="hasCursorORANGE" >Oil Painting</p>
                <div class="img-container">
                  <img src={this.state.img2} alt="Classic Painting"></img>
                </div>
              </div>

              <div class="text-tag">
                <p class="hasCursorORANGE" >Sculpture</p>
                <div class="img-container">
                  <img src={this.state.img3} alt="Sculpture"></img>
                </div>
              </div>

            </div>
            
    </section>
  </>

    )
  }
}

export default Index; //

Answer №1

The solution involved utilizing Css hover. Here is the resolution for the provided example.

.img-container {
    position: relative;
    visibility: hidden;
    opacity: 0;
    transition:opacity .5s;
    z-index: 0;
    top: -60px;
    left: 0px;
}

.text-tag  {
    font-family: Nexa;
    font-weight:bolder;
    display: flex;
    z-index: 2;
    font-size: 1.8rem;
    line-height: 2rem;
    color: var(--font_color);
}

.text-tag:hover .img-container{
    opacity: 1;
    visibility:visible;
    transition:opacity .8s;

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

Fill the second dropdown menu options based on the selection made in the first dropdown menu

I need assistance with dynamically populating my second drop-down menu based on the selection made in the first drop-down. Here are the steps I've taken so far: form.php - Utilizing javascript, I have set up a function to call getgeneral.php. The se ...

Execute JavaScript function only if it is the final invocation

I'm currently experiencing an issue with a Javascript function that updates a bootstrap spinner. The function works well in general, but the problem arises when I click multiple times. I utilize ajax to update the quantity of selected products in the ...

Fill a form with jQuery and ajax data after it has been submitted

I'm working on a simple HTML form and I want to use Ajax to perform a lookup using a PHP file after entering data into the first field. The goal is to fetch information from an external source for the two remaining fields. <form method="post" acti ...

The process of extracting a value from an array of objects encountered an error due to the undefined object

I am looking to extract the value from an array within an object while also implementing error checking. The code I currently have checks if a specific key exists in the object and if the value associated with that key is of type array. If both condition ...

Utilizing JQuery to extract data from a <select> dropdown menu

Is there a way to retrieve the current value of a SELECT tag using JavaScript or jQuery? I have tried using $('select').val(), but it only returns the default value and does not update when changed. Any suggestions on how to solve this issue? $( ...

Steps for selectively extracting objects from an array containing nested objectsNeed a way to isolate specific objects

Currently, I am working on a project in JavaScript and have created an array called folders that holds multiple objects: folders = [folder1, folder2, folder3...] Each object within the array has various properties, one of which is docs that is an array o ...

Images and videos are not appearing on GithubPages when viewed online, but they are visible

Check out my GitHub repository: https://github.com/Snipervuk/ChristianMilicevic I'm encountering an issue where my HTML images or videos are not displaying on my GitHub pages online, but they do display locally. I've attempted several solutions ...

What is the most efficient way to transfer a value from the main application file to a router file using the express framework

Currently, I am developing an Express application with multiple routes. To ensure modularity, each route will have its own file stored in a dedicated routes folder. One challenge I encountered is sharing a common value across all routes. Specifically, I n ...

What is the most efficient way to utilize a single connection to interact with MongoDB?

I have a series of functions that are responsible for running various queries. Here is the code I am working with: var MongoClient = require('mongodb').MongoClient; async function createDatabase() { MongoClient.connect(urlMongoDB, function(er ...

“Unable to update value using Controller component in react-hook-form”

Currently, I am utilizing the react-hook-form in combination with MUI components. In order to incorporate the Select component, I have enclosed it within the Controller since direct registration is not possible. Although everything seems to be functioning ...

Is there a way to modify the text color within the thumb-label of the Vuetify v-slider component?

Lately, I've been facing some challenges and my objective is to change the color of the thumb label on my v-slider to a custom one that is defined in the component's design. Can anyone provide guidance on how to achieve this? Regards, Joost ...

Enhancing Angular 4 classes with dependency injection techniques

Currently utilizing angular 4 and angular cli for my project development. I have created some classes that serve as the base for my components! However, as the constructors of these classes grow during development, I find myself in a phase where I need to ...

What mysterious occurrence is affecting my DOM element?

As I delve into learning react-redux and work on developing a simple store app, I've encountered a problem that I'd like to outline in a concise manner. Currently, my store has some states set up along with a Navbar featuring a Cart button. Upon ...

Issue with Bootstrap rows overlapping on smaller screens

Basically, the issue I'm facing is that when I reduce the size of my screen, the columns drop below and start overlapping with the row beneath them. I've been trying to figure out if there's a simple solution that I'm overlooking. But ...

Tips on applying the "activate" class to a Bootstrap navbar in Angular 2 when implementing page anchor navigation

As I work on my single-page website using Angular 2 and Bootstrap 4, I have successfully implemented a fixed navbar component that remains at the top of the page. Utilizing anchor navigation (#id), the navbar allows smooth scrolling to different sections o ...

Replacing text in the main navigation bar with sIFR

Could this menu layout be improved? <div class="navigation"> <ul id="nav-menu"> <li class="active"> <div class="sifr-r active"><a href="#" title="home" class="top-link"><span class="blue-small">01.</span& ...

Getting the expanded row columns values in a RadGrid when using the onHierarchyExpanded event

Here is the code for my RadGrid: <telerik:RadGrid ID="ProductRanges_Grd" ShowHeaderWhenEmpty="true" runat="server" AutoGenerateColumns="false" Width="100%" Height="250px" ShowHeader="true" Visible="false" ...

Div's HTML Classes are not triggering the Click Event

I'm attempting to create a custom function that will display notifications in the top right corner of my website, similar to how OS X notifications appear. Instead of relying on an external library, I am using jQuery as a resource. To house my notifi ...

Angular - Error: Cannot read property 'publishLast' of undefined

My goal is to prevent multiple requests from being created when using the async pipe. I am facing an issue with a request to fetch a user from the API: getUser() { this._user = this.http.get<User>(environment.baseAPIUrl + 'user') ...

scrolling through horizontal bootstrap thumbnails

Hey there, I'm currently working on making the bootstrap thumbnails fit into a Div that has a horizontal scroll. However, I've run into an issue where they are collapsing beneath it instead of being offset properly as shown in the image. Does any ...