What are some effective ways to transition a logo into a hamburger icon when hovered over?

In my React project, I am looking to enhance the user experience by adding an interactive element to the collapsed sidebar menu. My goal is to have the logo at the top of the sidebar transform into a basic hamburger menu when hovered over, and then revert back to the original logo when the mouse leaves the sidebar.

Although I have access to a designer who could create an animated asset like a GIF, I am unsure of how to implement it effectively in this context.

My experience with web animation is limited, but I believe that using CSS animations would be the most suitable approach for creating the desired transformation. However, I lack the necessary skills to do this myself, and it may be out of the designer's scope as well.

I envision the logo transitioning smoothly from its original form: https://i.sstatic.net/Bsqx9.png To a simple hamburger icon: https://i.sstatic.net/ivRYk.png

  • Is it feasible to achieve this effect in React using a GIF or other animation formats?
  • If CSS animations are the preferred method, what steps should I take to convert the logo to CSS?

Edit: To clarify, I am seeking a seamless transition between the two states, which is why I am particularly interested in utilizing CSS animations.

Answer №1

Instead of using a div, try implementing this code with an Icon. I will provide the codesandbox link for you to test it out.

Revised Answer

The following code includes hover effects for smooth transitions. You can customize the CSS to make the Icon fade in.

export default function App() {
  const [isHovered, setIsHovered] = useState(false);
  return (
    <Icon className="App">
      <Icon
        className={`${isHovered ? "show" : "hide"}`}
        onMouseOver={() => setIsHovered(false)}
      >
        Hover over me.
      </Icon>
      <Icon
        className={`${isHovered ? "hide" : "show"}`}
        onMouseOut={() => setIsHovered(true)}
      >
        I am shown when someone hovers over the previous Icon.
      </Icon>
    </Icon>
  );
}

CSS:

.hide {
  display: none;
}
.show {
  display: block;
  background-color: red;
  opacity: 0.6;
  transition: 0.3s;
}

.show:hover {
  transition: 0.3s;
  opacity: 1;
  -webkit-transition: all 500ms ease-in;
  -moz-transition: all 500ms ease-in;
  -ms-transition: all 500ms ease-in;
  -o-transition: all 500ms ease-in;
  transition: all 500ms ease-in;
}

Updated sandbox https://codesandbox.io/s/blazing-night-yii1zx?file=/src/styles.css:58-410

Previous Answer

export default function App() {
  const [isHovered, setIsHovered] = useState(false);
  return (
    <div>
      {isHovered ? (
        <h1 onMouseOut={() => setIsHovered(false)}>Hover true</h1>
      ) : (
        <h1 onMouseOver={() => setIsHovered(true)}>Hover false</h1>
      )}
    </div>
  );
}

Previous sandbox The codesandbox: https://codesandbox.io/s/dank-water-mbtwfw?file=/src/App.js:176-579

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

Issue with React Router - page displays content before state is updated with the response from the API call

I've encountered a strange issue with my React application. Currently, I'm working on adding a "previous orders" feature to my mock vinyl shop. The problem arises when I try to fetch the previous orders using a GET request with a token in a useEf ...

Positioning of CSS elements: Distributing and arranging 4 divs evenly within a single parent div

Is there a more efficient method? div { border: 2px solid black; } #main { width: 107px; height: 107px; padding: 0; text-align: center; font-size: 10px; } #tl, #tr, #bl, #br { position: relative; width: 45px; height: 45px; } #tl { t ...

Collaborating on code between a Typescript React application and a Typescript Express application

Struggling to find a smart way to share code between two interconnected projects? Look no further! I've got a React web app encompassing client code and an Express app serving as both the API and the React web app host. Since I use Typescript in both ...

What to do when JavaScript fireEvent isn't working as expected?

I've hit a roadblock while working on my JavaScript rendition of the classic game Battleship for a school project. I'm currently stuck on devising an AI for the opponent player. I have set up an event listener for when a cell is clicked on the pl ...

Using jQuery to manipulate the image within a specific div element

I'm facing an issue with locating the img src within a div. I've written a function to find all the divs with specific ids: function identifyDiv(){ divArray = $("div[id^='your']"); divArray = _.shuffle(divArray); } This is the ...

How to update an object property in React using checkboxes

I am currently navigating the world of react and have encountered a challenging issue. I am in the process of developing an ordering application where users can customize their orders by selecting different ingredients. My approach involves using checkboxe ...

Tips for dynamically adjusting the color of link text within jQuery Mobile?

When using jQuery Mobile, links are styled according to the theme stylesheet. If you want to change this styling, you can use !important as follows: .glossaryLink{ color: white !important; background-color: #CC8DCC; } I am looking to dynamically cha ...

Ways to create an array with only unique object keys

Is there a way to create an array unique by object key in JavaScript? I am working with dynamic checkboxes and storing their ids in the state. The format used is [checkBoxId]: e.target.value I am attempting to generate a unique array from the checked che ...

Align elements horizontally

Currently, I am developing a blog application using React, Material UI, and Tailwindcss. One issue I am facing is that each postcard in the grid has a different height, and I want all cards to have the same height while keeping all children aligned in the ...

The height of a DIV element can vary based on

Looking at this div structure: DIV3 has a fixed height. The nested DIV5 will be receiving content from Ajax, causing its height to change. Additionally, there are some DHTML elements inside it that also affect the height. DIV5 has a fixed min-height set. ...

Retrieve the latest data from the server or refresh the Redux state

Picture a scenario where you have a React application that displays a list of your friends in a sidebar. The application relies on Redux to manage the state of these friends. Whenever a user adds a new friend to their list, a POST request is sent to the ba ...

Crafting a dynamic CSS 'trail' effect when hovering

I am attempting to create a visually appealing CSS menu using primarily CSS, with just a touch of jQuery as well: Here is the concept: +------------------------+ | | | | | +---+ | | | ...

CSS changes triggered by JQuery are ineffective

Having trouble modifying the CSS of a class in my HTML file. I've been struggling with this for quite some time and can't figure out what I'm doing wrong. (I've already attempted multiple versions, but nothing seems to work) Here' ...

Form fields in Bootstrap 4 will consistently expand downward in the select dropdown menu

When using bootstrap 4, I want my field to always expand downwards and never upwards, even when half the screen is taken up by the form's select element. How can I achieve this effect? I have tried using data-dropup-auto="false" but it didn't wo ...

Can Keyboarddatepicker automatically format the date?

We are utilizing material ui Keyboarddatepicker in our React application and have specified the date format using props as format=DD/MMM/YYYY, resulting in dates being displayed like "10/12/2020". The date picker functions perfectly when selecting a date. ...

The componentDidMount event of the child component is throwing an error due to the parent state being

I'm experiencing an issue where the value passed from the parent component to the child component via props is showing as undefined when logging it in the componentDidMount event of the child component. I need this value to be properly updated and acc ...

The functionality of JQuery's .hover() is disabled once an HTML onClick() event is activated

I am currently working on a webpage and attempting to incorporate JQuery into it for the first time. However, I seem to be encountering some issues that I believe might have simple solutions. Background Information My JQuery code only contains one event l ...

Out of nowhere, Div became completely unresponsive

As part of my lesson plan on jQuery, I wanted to demonstrate a sliding effect using a div element. Initially, the sliding worked fine when clicked, but then I tried adding code for hiding another element and encountered issues. Even after removing the new ...

Utilizing the Parent's Props in ReactJs: A Comprehensive Guide

I am currently utilizing reactjs version 16. My objective is to create a wizard that can be reused anywhere on my website, providing flexibility and convenience. Take a look at the code I have implemented: export default class Website extends Component ...

Unable to locate the identifier 'componentDidUpdate' for a React component using Typescript

Is it possible to utilize componentDidUpdate or any other component with Typescript? The function does not seem to be recognized in my program. However, useEffect() works perfectly fine. ...