Uncover the magic of animation

I recently developed an image reveal animation, but have encountered a glitch during the creation process.

.container{
  overflow: hidden;
}

.image {
  background-image: url('https://images.wallpaperscraft.com/image/honda_civic_type_r_honda_type_r_honda_129270_3840x2160.jpg');
  background-size: cover;
  width: 400px;
  height: 400px;
  animation: 1s ease-out 0s 1 slideInFromLeft;
  overflow: hidden;
}

@keyframes slideInFromLeft {
  0% {
    transform: translatex(-100%);
  }
  100% {
    transform: translatex(0%);
  }
}
<div class="container">
  <div class="image">
  </div>
</div>

The animation is mostly functional except for a slight jitter or lag in the sliding effect.

What steps can be taken to ensure a smoother transition in the animation?

Answer №1

If you want the animation to trigger after the page/image has fully loaded, you can use the following javascript code snippet below. This code will add the necessary classes for the animation to occur only once.

window.onload = function() {
    var element = document.getElementById('img1');
    element.classList.add("show");
    element.classList.add("animate");
    
}
.container{
  overflow: hidden;
}

.image {
  background-image: url('https://images.wallpaperscraft.com/image/honda_civic_type_r_honda_type_r_honda_129270_3840x2160.jpg');
  background-size: cover;
  width: 400px;
  height: 400px;
  opacity: 0;
  overflow: hidden;
}

#img1.animate{
  animation: 1s ease-out 0s 1 slideInFromLeft;
}

.show {
  opacity:1;
}

@keyframes slideInFromLeft {
  0% {
    transform: translatex(-100%);
  }
  100% {
    transform: translatex(0%);
  }
}
<div class="container">
  <div id="img1" class="image" >
  </div>
</div>

Answer №2

To display images on a webpage, I recommend using the <img> element and then removing it once the image has loaded. By setting the image's src as the background of its parent element, you can achieve this effect efficiently:

let pictureDivs = document.querySelectorAll('.picture img');
for (var i = 0; i < pictureDivs.length; i++) {
  pictureDivs[i].addEventListener('load', pictureLoaded)
}

function pictureLoaded(e) {
  let divElem = e.target.closest('div');
  divElem.style.backgroundImage = 'url('+this.src+')';
  divElem.classList.add('loaded');
  divElem.removeChild(divElem.querySelector('img'));
}
.container{
  overflow: hidden;
}

.picture {
  background-size: cover;
  width: 400px;
  height: 400px;
  transform: translatex(-100%);
  overflow: hidden;
}
.picture.loaded{
  animation: slideInFromLeft 1s cubic-bezier(.5,0,.3,1) forwards;
}
.picture img {
  height: 0;
}

@keyframes slideInFromLeft {
  0% {
    transform: translatex(-100%);
  }
  100% {
    transform: translatex(0);
  }
}
<div class="container">
  <div class="picture">
    <img src="https://images.wallpaperscraft.com/image/honda_civic_type_r_honda_type_r_honda_129270_3840x2160.jpg">
  </div>
</div>

This method triggers the animation for each individual <div> containing an image as soon as that specific image loads, rather than waiting for all images on the page to load before animating (as with window.load).

It is worth noting that while this technique optimizes image loading, it is important to ensure your website does not overload with heavy resources. Image optimization and proper sizing are key steps in optimizing website performance. Consider utilizing srcset for responsive images.

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

Utilizing Bootstrap 4 cards in conjunction with a responsive next/image component

<div className="row align-items-center justify-content-center"> <div className="col-md-3 mt-3"> <div className="card bg-light"> <div className="card-img-top"> <N ...

Develop and arrange badge components using Material UI

I'm new to material ui and I want to design colored rounded squares with a letter inside placed on a card component, similar to the image below. https://i.stack.imgur.com/fmdi4.png As shown in the example, the colored square with "A" acts as a badge ...

Is there a way to create a dropdown menu that stretches across the entire page width when the burger menu is hovered over?

Is there a way to create a full-width dropdown menu that appears when the burger icon is clicked, while still maintaining the close function? (The dropdown menu and burger icon should transition smoothly when clicked) Here are the JavaScript functions for ...

"Encountered an HTTP 400 error when trying to retrieve content from a Persian URL using file

As a beginner, I am facing an issue with a URL that contains Persian language characters. For instance: http://tabnak.ir/fa/news/577155/ویدیوی-درگیری-نیروهای-سیا-و-پنتاگون-در-سوریه-با-همدیگر-ویدیوهایی ...

Transform the color of links in a Sankey diagram on Google upon clicking a node

Currently, I am in the process of creating a Sankey diagram using Google charts. As of now, I have implemented the following CSS: svg path:hover { fill: red; } However, I have noticed that this code only changes the color of the links when hovering over ...

Creating a hover effect on a specific area of a map is a useful technique

Can someone please help me create a hover effect for areas in maps? Below is the code for the map. Thank you in advance! Here is the image. <img src="[CR_kraje_1_úroveň_v1] (imported)" width="770" height="443" border="0" usemap="#map" /> &l ...

Say goodbye to my HTML5 creations

I am currently working on an HTML5 grid project that involves implementing a rectangle select tool for use within the grid. Everything is going smoothly, except for the fact that when I attempt to use the rectangular select tool, the grid disappears from t ...

Handling overflow and z-index of tabs in Vuetify Drawer

Struggling to create an expandable sidebar menu using Vuetify2, following the documentation, but unable to prevent the expanded menu from overlapping other elements on the page. The current behavior causes items to be pushed away and wrap onto the next ro ...

The Boostrap card-deck appears to be see-through and malfunctioning

This problem has been puzzling me for quite some time now, and I just can't seem to find the solution! Kindly disregard the other language and code - the card is located under the jumbotron. (To access the link, remove spaces: https://code pen.io/ano ...

How can I use htaccess to forward all links within a specific folder to index.php with a GET variable attached?

Is it possible to redirect all links inside a folder to index.php while passing the file name as a GET variable? I have never worked with htaccess before and don't know how to search for it. For instance: localhost/folder/file-123 should redirect to ...

Creating a new tab within a window that is already open

I have an interesting challenge - I need to open a new tab in a window that was previously opened using window.open(). So, if the window is already open, I want to open a tab in that same window. I have attempted to reference the existing window to open a ...

Stopping an HTML5 range input at a specific number: Tips and tricks

Does anyone have suggestions on how to use angularjs to stop a range slider at 75? I attempted it but it doesn't seem like the best approach. Any guidance would be appreciated. [EDIT for clarification after max=75 answer] Just to clarify, I want to di ...

Jquery unable to render UL or LI tags in HTML document

Here's a snippet of code I've been working on: //show live preview while typing $('#copyText').keyup(function(e){ $('#copyPreview').html($('#copyText').val()); }); Users can input HTML and see a real-time previ ...

An always-visible navigation menu

After exploring various resources related to the same topic, such as sticky navigation bars and others, I noticed that all the solutions provided involved the use of this link http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js. However, inc ...

View content from an external file within an iframe

My goal is to showcase a text file within an iframe that updates automatically every second. To achieve this, I have created a simple function: function refresh() { $("#derek").attr('src', $("#derek").attr('src')); }; The automati ...

Upgrading the grayscale filter to 100% in Internet Explorer 11

Recently, I learned that the support for the grayscale(100%) filter was eliminated in Internet Explorer 9 and later versions. Can you suggest an alternative filter that I can use for .svg format images instead? ...

What is the best location to store user-uploaded files on my server?

Users visiting my website have the ability to upload their own pictures. The technology stack I am using includes tomcat, apache, hibernate, and jpa. I plan on storing these images in a specific location such as /var/ImagesUploaded on my Ubuntu server. Wh ...

Can the font family of <body> be customized in Bootstrap 5?

I am facing an issue where the Font-Family changes for my heading tags, but the body tag keeps getting cancelled out by the error "_reboot.scss:50". When I inspect my webpage and toggle off the default "enter code herefont-family: var(--bs-body-font-family ...

Utilize the float left and float right properties within a div container with a width of 100% to ensure compatibility

I need to align two divs within a container-div in IE7. The first div should float to the left, and the second to the right. Currently, both divs are floating to the left according to my CSS configuration. Here is the CSS code I'm using: .container ...

Hide the 1, 2, 3 page buttons in Datatables pagination and instead display only the Next and Previous buttons for navigation

I recently implemented datadables.net pagination for my HTML table. I am looking to customize the pagination buttons by hiding or removing the 1, 2, 3 ... buttons and only display Next and Previous Buttons. Is there a way to achieve this by setting paging ...