Learn how to create a dynamic animation that makes an image disappear and seamlessly transitions to displaying a gif before both elements fade

Display an image shrinking and a gif in one second, then make them disappear together

  1. I start with an image.
  2. When the user clicks on this image, it triggers a disappearing animation (0.5s)
  3. Once the animation is complete, a gif appears in the background (0.5s)
  4. Lastly, both the image and gif disappear together

Just like that!

Answer №1

Below is a sample HTML structure:

<div id="myDiv">
    <img id="myImg" src="image.jpg" alt="">
</div>

To add animation effects, use the following CSS property:

#myImg {
    transition: all 0.5s ease;
}

You can create an animation when clicking on the image using jQuery:

$('#myImg').on('click', function() {
  $('#myImg').css('width', '50%')
  setTimeout(function() {
    $('#myDiv').css('background-image', 'url("cat.gif")')
    setTimeout(function() {
      $('#myImg').css('display', 'none');
      $('#myDiv').css('display', 'none')
    }, 500)
  }, 500)
})

Upon clicking the image, it will shrink in width and you can customize the width or height accordingly. After 0.5s, a GIF will replace the background, followed by hiding both elements after another 0.5s. I hope this explanation helps!

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

Field validation with optional mask restriction

I'm currently using Yup to validate form fields, and there is a specific field with a custom mask that needs validation only if it's filled; if empty, it should not be required. My challenge involves two fields: customerId, which is already work ...

Tips on obtaining the element selector when a div is being dynamically loaded during an AJAX call

When running two ajax calls, I encounter an issue where the second call loads some HTML onto the page that is needed for processing a specific div in the first call. However, since the div is not present until after the second call is completed, I receiv ...

I'm struggling to find the correct media query to fix the problem with resizing my window

Looking to create a layout where two large icons are displayed side by side horizontally, but then switch to a vertical layout when the window size is reduced (such as on a mobile phone). I know that media queries are necessary for this, but I am unsure of ...

Integrating variable interpolation within media queries using Compass/Sass

Is there a method to achieve the desired result in Compass/Sass? $padding: 3em div { padding: $padding; } @media screen and (max-width: 780px) { $padding: 2em; } /* Result: div { padding: 3em; } @media screen and (max-width: 780px) { ...

Creating a dynamic table with CSS: implementing two unique hover colors

Currently, I am working with a table that serves as a check list for a collection. My goal is to find a way to change the hover color of the table to GREEN if it's an item I own, and to red if it's an item I don't own. Since I am a beginne ...

React JS displayed the string of /static/media/~ instead of rendering its markdown content

When I looked at the material UI blog template, I created my own blog app. I imported a markdown file using import post1 from './blog-posts/blog-post.1.md'; Next, I passed these properties to this component like so: <Markdown className=" ...

"Google Maps Integration: Customize Your Map with Grey Color Scheme and User-F

After reviewing the other questions and answers related to my problem, none of them seemed to address it. The issue is that when loading and placing a marker, the map seems to "freeze," turns grey, but still displays controls. The marker ends up in the up ...

Prevent elements from collapsing within the Bootstrap 5 grid system

Hey there, happy Friday everyone! I've been working on rebuilding my company's website to learn HTML and CSS. I'm currently creating a replica of the original site and it's going really well so far. However, I've run into some is ...

Error: Attempting to access the 'name' property of an undefined variable is resulting in a TypeError in Material UI

When attempting to display the retrieved data on MUI Autocomplete, I encountered an error that I cannot seem to figure out. The data is fetched from MongoDB, and I simply want to showcase the category names as selectable options. https://i.sstatic.net/cxT ...

Handling state in Material UI components with React

Is there a way to incorporate conditional styles in Material UI while working with React? <div className={clsx(selectedCount > 0 classes.red : classes.table)} > </div> ...

Identify Horizontal Swipe Gestures on Page-level

I am currently focused on ensuring accessibility for users utilizing voiceover technology. While navigating their phone, these individuals rely on right and left swipes to interact with elements on a page. I am seeking to implement swipe detection at the ...

Retrieving HTML content from source code and populating it in a webview

I'm attempting to display a web page in my Android webview by copying the HTML source code and pasting it into an HTML file within the 'assets' directory. However, when I use webview.loadUrl(myUrl);, the page appears mostly black and white w ...

Personalizing Material-UI Components using Styled-Components

My attempt to modify the props of a material-ui Grid component using styled-components was not successful. I initially tried: const NavGrid = styled(Grid)` direction: 'row'; ` Unfortunately, this approach did not yield the desired result. T ...

Switching to User-Friendly Time Display in React-Native

I've implemented a timer in one of my react-native components, and everything seems to be working smoothly with the code below: const visitItemCounter = (value) => { const [duration, setCounter] = useState(moment.duration(value).asMilliseconds()) ...

Is there a way to showcase the present weather conditions using simpleweather.js in plain text format?

I have integrated the weather.js library into my HTML file by adding the necessary imports: <script src="js/current.js"></script> <script src="js/sugar.min.js"></script> <script src="js/weather.js"></script> <script ...

What might be causing the appearance of NaN in my React component?

<body> <div id="root"> </div> <script type="text/babel"> class Count extends React.Component { constructor (props) { super(props); this.state = {count: 0}; ...

Steps to fix: "Rule '@typescript-eslint/consistent-type-assertions' is missing a definition"

My React app is failing to compile because it can't find the rule definition for '@typescript-eslint/consistent-type-assertions'. I'm feeling quite lost at the moment. I can't seem to locate any current rule definitions within the ...

Unexpected problem discovered with Firefox and JqueryUI - content is misaligned within the dialog box

I seem to be having an issue with a dialog that I recently set up. I've been working with jqueryUi for quite a while so I'm not sure why I can't figure this out. When using Firefox, I noticed that the dialog opens and closes when clicking " ...

The state and useState do not change when the onchange event is triggered by selecting an item from a dropdown list for the first time

When I select a product from the dropdown, I am unable to get the curentProduct to change to the target value. The change only occurs when I select a different product and then the first product populates. I want the curentProduct to change immediately whe ...

Encountering an issue with React and Material-UI where the MuiDialog-root appears to become unresponsive

While using React with MaterialUI, I'm encountering an issue where a div element remains stuck on the screen after closing a MUI dialog: <div class="MuiBackdrop-root" aria-hidden="true" style="opacity: 0; transition: opacity 195ms cubic-bezier(0.4 ...