Smoother background-images animations (prevent flickering) - html

I have three background images that I want to animate between. I do this as outlined below.
However, I find that sometimes (when first loading the page), the images flicker. So instead of a smooth transition what happens is that image 1 flickers then smoothly fades out, then image 2 flickers and then smoothly transitions in.
How can I avoid this flickering? Is there a way to wait for the images to be fully loaded before to start the animation? Or is that not the problem?
.page-light {
background-repeat: no-repeat;
background-position: center top;
}
#media (max-width: 767.98px) {
.page-light {
background-image: url('/img/hero/front-introduction-mobile.png');
}
}
#media (min-width: 768.00px) {
.page-light {
background-image: url('/img/hero/front-introduction-1.png');
-webkit-animation: animation-home-background 9000ms infinite;
-moz-animation: animation-home-background 9000ms infinite;
-o-animation: animation-home-background 9000ms infinite;
animation: animation-home-background 9000ms infinite;
-webkit-animation-delay: 1s;
/* Chrome, Safari, Opera */
animation-delay: 3s;
}
}
#-webkit-keyframes animation-home-background {
0% {
background-image: url('/img/hero/front-introduction-1.png');
}
40% {
background-image: url('/img/hero/front-introduction-2.png');
}
80% {
background-image: url('/img/hero/front-introduction-3.png');
}
100% {
background-image: url('/img/hero/front-introduction-1.png');
}
}
#keyframes animation-home-background {
0% {
background-image: url('/img/hero/front-introduction-1.png');
}
33% {
background-image: url('/img/hero/front-introduction-2.png');
}
66% {
background-image: url('/img/hero/front-introduction-3.png');
}
100% {
background-image: url('/img/hero/front-introduction-1.png');
}
}

I found this to be extremely helpful, as pointed out by #vxpin in the comments.
https://stackoverflow.com/a/52358326/4262057
Basically you preload all the images as follows:
.stylename {
background-image:
url('http://www.BungalowSoftware.com/images/silver-background.jpg'),
url('http://www.BungalowSoftware.com/images/silver-background2.jpg');
}

Related

How to make the background color of a simple website transition smoothly with CSS?

I am new to programming. Please forgive me if this is a very basic question.
I am working on a small project of designing a very simple personal website using HTML and CSS. On the website, I want the background colors of the main website to fade into one another after some time (say, to transition from light blue to light green to light yellow after 15s each). Is there a way I can do this using CSS?
body {
background:lightblue;
animation:changebg infinite 15s;
}
#keyframes changebg{
0% {background:lightblue;}
33% {background:lightgreen;}
66% {background:lightyellow;}
100% {background:lightblue;}
}
Do this instead
body{
background: lightblue;
animation: changebg 60s linear infinite alternate;
}
#keyframes changebg{
0% {background:lightblue;}
33% {background:lightgreen;}
66% {background:lightyellow;}
100% {background:lightblue;}
}
If this doesn't work, you can mess around with the timing.
You can try the following code:
body{
background: lightblue;
animation: changebg 60s linear infinite alternate;
}
#keyframes changebg {
0% { background:lightblue; }
33% { background:lightgreen; }
66% { background:lightyellow; }
100% { background:lightblue; }
}
Pretty much the website's background will start at light blue, and when it reaches 33% of 60s (for e.g.) it will change to light green, etc.
And when it reaches 100% at 60s time, the "linear infinite alternate" will make sure it repeats.
body {
background: red;
animation: changebg 5s infinite;
//adding infinte will make your animation run in a loop
}
#keyframes changebg
/* Firefox */
{
0% {
background: red;
}
50% {
background: blue;
}
100% {
background: red;
}
}
Just an addition to the previous answer
Keep on learning.

Dynamic CSS Sprite animation size

I am developing an Ionic application where one of the templates contains sprite animation. The animation works fine except that the size is fixed for any device I use. Here's the code I use:
CSS
#animation_boy {
width: 558px;
height: 1536px;
background-image: url("../img/boy_sprite.png");
-webkit-animation: play 1.2s steps(7) infinite;
animation: play 1.1s steps(7) infinite;
}
#-webkit-keyframes play {
from {
background-position: 0px
}
to {
background-position: -3906px
}
}
#keyframes play {
from {
background-position: 0px
}
to {
background-position: -3906px
}
}
HTML
<div id="animation_boy"></div>
I want to make the size depend on the screen. Thanks for the help.

Adding style attributes to image called from another style

This is to add a spinning/loading icon for images as they load.
The existing code I'm using calls up an animated .gif image as a background image "behind" an image thumbnail, so the loading icon is visible until the thumbnail loads on top. But I want to replace the .gif with a higher quality .png and add CSS to make it rotate. It's a much cleaner look, but I don't know how or if I can add CSS style to background: url(img/loading.png)
Here's the original HTML code:
<div style="position: absolute; display: block; background: url(img/loading.png) no-repeat center center; top: 0px; left: 0px; width: 25%; height:25%;">
I want to add this CSS code to the .png to make it rotate:
.loading {
-webkit-animation:spin 2s linear infinite;
-moz-animation:spin 2s linear infinite;
animation:spin 2s linear infinite;
}
#-moz-keyframes spin { 100% {
-moz-transform:rotate(360deg);
}
}
#-webkit-keyframes spin { 100% {
-webkit-transform:rotate(360deg);
}
}
#keyframes spin { 100% {
-webkit-transform:rotate(360deg);
transform:rotate(360deg);
}
}
What's the best way to combine these to make my background .png image rotate?
You can animate the div with the background, you just need to add the loading class to it and with a separate class to add the other styles to it like the background url, width, height, position etc...
.load-style {
height: 64px;
width: 64px;
background: url(http://www.jasonkenison.com/uploads/blog/loading.png) no-repeat center center;
background-size: 100%;
position: absolute;
}
.loading {
-webkit-animation: spin 2s linear infinite;
-moz-animation: spin 2s linear infinite;
animation: spin 2s linear infinite;
}
#-moz-keyframes spin {
100% {
-moz-transform: rotate(360deg);
}
}
#-webkit-keyframes spin {
100% {
-webkit-transform: rotate(360deg);
}
}
#keyframes spin {
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
<div class="loading load-style"></div>
You didn't add the class which your animating to the HTML. In your CSS you have a class called "loading" but the HTML doesn't know what to animate. In your div before the style="" tag add class="loading" and it will work, other than that your CSS works.

Auto scroll overflow text with CSS3

I have a limited area to show a long text. It should be wrapped with ellipsis. When you mouse over it, it should be scrolled until end of it so I came up with this but I have some problems.
It doesn't work on Opera Browser.
It uses static width property.
div.content:hover {
width:742px;
}
Even text is shorter; it scrolls in a static duration.
How can I solve this?
EDIT: I solved all these problems with JavaScript but a pure CSS3 solution will be still great because I hate injecting CSS rules inside of my JavaScript Code. http://fiddle.jshell.net/tU43F/18/
test with this : (u can modify the right attributes in animation same as u want)
div.content:hover {
-webkit-animation: slide 5.0s linear;
-moz-animation: slide 5.0s linear;
-o-animation: slide 5.0s linear;
animation: slide 5.0s linear;
width:742px;
right:0px;
text-overflow: clip;
}
#-webkit-keyframes slide {
0% { right:-665px;}
50% { right:-340px;}
100% { right:-0px; }
}
#-moz-keyframes slide {
0% { right:-665px;}
50% { right:-340px;}
100% { right:-0px; }
}
#-o-keyframes slide {
0% { right:-665px;}
50% { right:-340px;}
100% { right:-0px; }
}
#-khtml-keyframes slide {
0% { right:-665px;}
50% { right:-340px;}
100% { right:-0px; }
}
#keyframes slide {
0% { right:-665px;}
50% { right:-340px;}
100% { right:-0px; }
}

Keyframes animation working only on chrome/chromium

I have done a simple three image transition animation code. The code can be found here:
http://jsfiddle.net/harshithjv/AF3Jj/
This code works only on chrome and chromium browsers. It does not work on Apple's Safari browser also. Also it does not work on any other browsers(I tested on Firefox and IE9, not tried Opera).
I guess that I am missing something on animation shorthand property. Please help me out.
Edit:
I am updating with the code for some clarity, which I should have done in first place.
HTML Code:
<div class="animated_star"></div>
CSS3 Code:
#-moz-keyframes shining_star {
from {
background-image: url('http://findicons.com/icon/download/162253/star_grey/16/ico');
}
50% {
background-image: url('http://findicons.com/icon/download/181769/star_half/16/ico');
}
to {
background-image: url('http://findicons.com/icon/download/159919/star/16/ico');
}
}
#-webkit-keyframes shining_star {
from {
background-image: url('http://findicons.com/icon/download/162253/star_grey/16/ico');
}
50% {
background-image: url('http://findicons.com/icon/download/181769/star_half/16/ico');
}
100% {
background-image: url('http://findicons.com/icon/download/159919/star/16/ico');
}
}
#keyframes shining_star {
from{
background-image: url('http://findicons.com/icon/download/162253/star_grey/16/ico');
}
50% {
background-image: url('http://findicons.com/icon/download/181769/star_half/16/ico');
}
to {
background-image: url('http://findicons.com/icon/download/159919/star/16/ico');
}
}
.animated_star{
height: 16px;
width: 16px;
float: left;
-webkit-animation: shining_star 1s infinite; /* works only for Chrome/Chromium */
-moz-animation: shining_star 1s infinite;
animation: shining_star 1s infinite;
}
Background image isn't a property that can be animated - you can't tween the property.
Instead, try laying out all the images on top of each other using position:absolute, then animate the opacity of all of them to 0 except the one you want repeatedly.
also
It works in Chrome 19!
So at some point in the future, keyframes could really be... frames!
You are living in the future ;)
After some research on this, I figured that background-image CSS property is not supported inside keyframes in most browsers. It must be because of loading too many images dynamically can lead to performance issues if larger images are loaded.
Thanks to #Morpheus for another stackoverflow link(http://stackoverflow.com/questions/7318462/changing-background-image-with-css3-animations), through which I decided to resolve the issue through image sprites and reposition(using CSS property - background-position) within that sprite to select the image as I want it. The problem with background-position CSS property is that when it applied for CSS animation through keyframes, the reposition shows the movement within image sprite. But I wanted to show 3 stars transition quickly without movement in three frames. To make that possible, I had to use 6 keyframes where first star's position will be set in 0% and 33%, second star's position will be set in 34% and 66% and the third star will be set in 67% and 100%.
I have created a jsFiddle which does not have image sprites of same stars. I could not locate sprite for same stars online and so I used alternate stars. Its not a perfect example since it has sloppy animation, but I have created a smaller sprite image (48px x 16px) on my system, and animation looks good enough.
HTML Code:
<div class="animated_star"></div>
CSS Code:
#-moz-keyframes shining_star {
0% { background-position: -135px 0px; }
33% { background-position: -135px 0px; }
34% { background-position: -135px -260px; }
66% { background-position: -135px -260px; }
67% { background-position: -270px -260px; }
100% { background-position: -270px -260px; }
}
#-webkit-keyframes shining_star {
0% { background-position: -135px 0px; }
33% { background-position: -135px 0px; }
34% { background-position: -135px -260px; }
66% { background-position: -135px -260px; }
67% { background-position: -270px -260px; }
100% { background-position: -270px -260px; }
}
#keyframes shining_star {
0% { background-position: -135px 0px; }
33% { background-position: -135px 0px; }
34% { background-position: -135px -260px; }
66% { background-position: -135px -260px; }
67% { background-position: -270px -260px; }
100% { background-position: -270px -260px; }
}
.animated_star{
height: 130px;
width: 135px;
float: left;
background: transparent url('http://azmind.com/wp-content/uploads/2011/11/social-star-icons.png') no-repeat fixed;
background-position: 0px -390px;
-webkit-animation: shining_star .5s infinite linear;
-moz-animation: shining_star .5s infinite linear;
animation: shining_star .5s infinite linear;
}
The jsFiddle link: http://jsfiddle.net/harshithjv/7QvSP/2/