I've got this code working for changing background conteniosly in CSS and HTML:
animation: animated 10s linear infinite;
#keyframes animated {
0% { background-position: 0px bottom; }
25% { background-position: -200px bottom; }
50% { background-position: -400px bottom; }
75% { background-position: -600px bottom; }
}
Currently the background moves in an animation, but I would like to have the background switch position in one go - so it seems the image changes instead of moving.
Is that possible with only CSS?
What you need to do is, instead of
25% { background-position: -200px bottom; }
50% { background-position: -400px bottom; }
You do it like:
25.01%, 50% { background-position: -200px bottom; }
50.01%, 75% { background-position: -400px bottom; }
Thus you will almost eliminate the time needed for transition from one keyframe to another.
Demo: jsFiddle
*{ padding:0; margin:0; }
#bg{ width:100%; height:100vh;
background: url('//www.planwallpaper.com/static/images/Fall-Nature-Background-Pictures.jpg') no-repeat;
animation: slideshow 5s infinite;
}
#keyframes slideshow {
0%, 25% { background-position: 0px bottom; }
25.01%, 50% { background-position: -200px bottom; }
50.01%, 75% { background-position: -400px bottom; }
75.01%, 100% { background-position: -600px bottom; }
}
<div id="bg"></div>
Use the following:
animation: animated 10s steps(4) linear infinite;
#keyframes animated {
from {
background-position: 0px 0px; }
to {
background-position: 600px 0px; }
}
You can remove linear in your CSS
animation: animated 10s infinite;
#keyframes animated {
0% { background-position: 0px bottom; }
25% { background-position: -200px bottom; }
50% { background-position: -400px bottom; }
75% { background-position: -600px bottom; }
}
You need to use some JS with setTimeout of 10000ms to switch background-position
$(document).ready(function () {
var koef = 1;
setInterval(function () {
$('#image').css({'background-position-x': 600 * -koef});
koef = !koef;
}, 1000);
});
#image {
width: 200px;
height: 200px;
background: url(http://lorempixel.com/g/1000/200/) no-repeat 0 bottom;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="image"></div>
Related
I'd like to have a wavy background on top of my regular background but the image is not stretching all the way out. It has to be position absolute (otherwise it's interfering with my navigation and moves it down by the height of the img). JSFiddle. I already tried object-fit: fill which also didn't work. Thanks in advance
My code:
* {
margin: 0;
padding: 0;
font-family: helvetica;
}
body {
height: 5000px;
}
#background {
position: absolute;
width: 100%;
height: 100%;
object-fit: contain;
object-position: ;
opacity: 15%;
}
#navwrapper {
background: linear-gradient(250deg, #0061ff, #60efff);
background-size: 400% 400%;
-webkit-animation: AnimationName 10s ease infinite;
-moz-animation: AnimationName 10s ease infinite;
animation: AnimationName 10s ease infinite;
height: 100vh;
}
#-webkit-keyframes AnimationName {
0% {
background-position: 0% 7%
}
50% {
background-position: 100% 94%
}
100% {
background-position: 0% 7%
}
}
#-moz-keyframes AnimationName {
0% {
background-position: 0% 7%
}
50% {
background-position: 100% 94%
}
100% {
background-position: 0% 7%
}
}
#keyframes AnimationName {
0% {
background-position: 0% 7%
}
50% {
background-position: 100% 94%
}
100% {
background-position: 0% 7%
}
}
<nav id="navwrapper">
<div id="background"><img src="https://i.postimg.cc/RZzwCTQz/Zeichenfla-che-92.png"></div>
</nav>
Instead of applying css to #background apply it in #background img thats where you want to change object fit property.
replace this
#background {
position: absolute;
width: 100%;
height: 100%;
object-fit: cover;
object-position: ;
opacity: 15%;
}
with this
#background img{
position: absolute;
width: 100%;
height: 100%;
object-fit: cover;
opacity: 15%;
}
You can check the output below
* {
margin: 0;
padding: 0;
font-family: helvetica;
}
body {
height: 5000px;
}
#background img {
position: absolute;
width: 100%;
height: 100%;
object-fit: cover;
object-position: ;
opacity: 15%;
}
#navwrapper {
background: linear-gradient(250deg, #0061ff, #60efff);
background-size: 400% 400%;
-webkit-animation: AnimationName 10s ease infinite;
-moz-animation: AnimationName 10s ease infinite;
animation: AnimationName 10s ease infinite;
height: 100vh;
}
#-webkit-keyframes AnimationName {
0% {
background-position: 0% 7%
}
50% {
background-position: 100% 94%
}
100% {
background-position: 0% 7%
}
}
#-moz-keyframes AnimationName {
0% {
background-position: 0% 7%
}
50% {
background-position: 100% 94%
}
100% {
background-position: 0% 7%
}
}
#keyframes AnimationName {
0% {
background-position: 0% 7%
}
50% {
background-position: 100% 94%
}
100% {
background-position: 0% 7%
}
}
<nav id="navwrapper">
<div id="background"><img src="https://i.postimg.cc/RZzwCTQz/Zeichenfla-che-92.png"></div>
</nav>
Use this property
#background{
background:url(...);
background-position: center !important;
background-size: cover !important;
background-repeat: no-repeat;
height: 500px;
width:100%
}
You can just set width and height to 100% for an image that it should all space of container:
#background img {
width: 100%;
height: 100%;
}
I have created a div which has a gradient background, and I want to change this gradient. I applied a keyframes animation which changed background color instantly. How can I make this change smooth?
div {
width: 100px;
height: 100px;
background:linear-gradient(red, yellow);
animation-name: colorchange;
animation-duration: 5s;
-webkit-animation-name: colorchange;
animation-iteration-count: 5;
-webkit-animation-duration: 5s;
text-align: center;
}
#keyframes colorchange {
0% {background:linear-gradient(red, yellow) }
35% {background:linear-gradient(yellow, green) }
70% {background:linear-gradient(green, red) }
100%{background:linear-gradient(red, yellow)}
}
<div>
Gradient Background
</div>
Try this
div {
text-align: center;
width: 100px;
height: 90px;
color: #fff;
background: linear-gradient(0deg, red, yellow, green);
background-size: 400% 400%;
-webkit-animation: Gradient 15s ease infinite;
-moz-animation: Gradient 15s ease infinite;
animation: Gradient 15s ease infinite;
}
#-webkit-keyframes Gradient {
0% {
background-position: 50% 0%
}
50% {
background-position: 50% 100%
}
100% {
background-position: 50% 0%
}
}
#-moz-keyframes Gradient {
0% {
background-position: 50% 0%
}
50% {
background-position: 50% 100%
}
100% {
background-position: 50% 0%
}
}
#keyframes Gradient {
0% {
background-position: 50% 0%
}
50% {
background-position: 50% 100%
}
100% {
background-position: 50% 0%
}
}
<div> Text </div>
I might be wrong, but gradients don't support transitions.
There's a workaround I found in other related question:
https://medium.com/#dave_lunny/animating-css-gradients-using-only-css-d2fd7671e759
As far as I'm concerned, the smooth transition doesn't work with gradient backgrounds, only with straight colors.
You can create a large gradient background with many colors though, and use the transition to move it. This creates the illusion of the colors changing.
body {
width: 100wh;
height: 90vh;
color: #fff;
background: linear-gradient(-45deg, #EE7752, #E73C7E, #23A6D5, #23D5AB);
background-size: 400% 400%;
-webkit-animation: Gradient 15s ease infinite;
-moz-animation: Gradient 15s ease infinite;
animation: Gradient 15s ease infinite;
}
#-webkit-keyframes Gradient {
0% {
background-position: 0% 50%
}
50% {
background-position: 100% 50%
}
100% {
background-position: 0% 50%
}
}
#-moz-keyframes Gradient {
0% {
background-position: 0% 50%
}
50% {
background-position: 100% 50%
}
100% {
background-position: 0% 50%
}
}
#keyframes Gradient {
0% {
background-position: 0% 50%
}
50% {
background-position: 100% 50%
}
100% {
background-position: 0% 50%
}
}
h1,
h6 {
font-family: 'Open Sans';
font-weight: 300;
text-align: center;
position: absolute;
top: 45%;
right: 0;
left: 0;
}
<h1>Hello World!</h1>
I'm trying to do animation with my background image and I manage to do it. But I my animation is not running smoothly. If I reduce the animation from 30s to 5s it running smooth but the animation is too fast.
My CSS :
body {
height: 100%;
width: 100%;
background: url('https://static.pexels.com/photos/3768/sky-sunny-clouds-cloudy.jpg') center center no-repeat;
background-size: cover;
background-position-y: 0;
animation: grow 30s linear infinite;
}
#keyframes grow {
0% { background-size: 100% auto; }
50% { background-size: 140% auto; }
100% { background-size: 100% auto; }
}
Check the result here https://jsfiddle.net/6q3obw82/
Try changing the keyframes for the code with adding more breakpoints
#keyframes grow {
0% { background-size: 100% auto; }
25% { background-size: 150% auto; }
50% { background-size: 200% auto; }
75% { background-size: 150% auto; }
100% { background-size: 100% auto; }
}
Jsfiddle link: https://jsfiddle.net/xgjmesrr/
I've got this sample sprite grid sheet that I need to run through and animate. I am able to reach a certain point but struggling to make it perfect. The animation is not that smooth and additionally, the image is not aligned properly. During the animation, you can see image elements not centered with other elements in the view. Here is my HTML and CSS3 code so far.
.hi {
width: 910px;
height: 340px;
background-image: url("https://simba-heroku.imgix.net/animation-homepage-tablet-retina.jpg?auto=format,compress");
position: relative;
-webkit-animation: playv 12s steps(6) infinite, playh 2s steps(4) infinite;
}
#-webkit-keyframes playv {
0% { background-position-y: 0px; }
100% { background-position-y: 100%; }
}
#-webkit-keyframes playh {
0% { background-position-x: 0px; }
100% { background-position-x: 100%; }
}
<div class="hi">
</div>
Fiddle: http://jsfiddle.net/bf5ckdv9/
I have added a background dimension style, and rearranged some of your properties
the result is almost ok; but your sprite grid seems to be out of order
.hi {
width: 910px;
height: 340px;
background-image: url("https://simba-heroku.imgix.net/animation-homepage-tablet-retina.jpg?auto=format,compress");
position: relative;
animation: playh 2s steps(5) infinite, playv 10s steps(5) infinite;
border: solid 1px blue;
background-size: 500% 500%;
background-repeat: no-repeat;
}
#keyframes playv {
0% { background-position-y: 0px; }
100% { background-position-y: 125%; }
}
#keyframes playh {
0% { background-position-x: 0%; }
100% { background-position-x: 125%; }
}
<div class="hi">
</div>
All I want to have a background scrolling effect from bottom to top ,but I dont know how to do so. I have tried it using CSS ,but the problem arises here is it scroll in both direction i.e bottom to top and top to bottom.
* {
margin: 0;
padding: 0;
}
div {
width: 100vw;
height: 100vh;
animation: change 2s infinite ease-in both;
background-image: url(http://hdwallpaperbackgrounds.net/wp-content/uploads/2016/07/background-pictures-2.jpg);
background-size: cover;
background-repeat: no-repeat;
}
#keyframes change {
0%, 100% {
background-position: 0% 5%;
}
5% {
background-position: 0% 10%
}
10% {
background-position: 0% 15%
}
15% {
background-position: 0% 20%
}
20% {
background-position: 0% 25%
}
25% {
background-position: 0% 30%;
}
30% {
background-position: 0% 35%
}
35% {
background-position: 0% 40%
}
40% {
background-position: 0% 45%
}
45% {
background-position: 0% 50%
}
}
<div>
<h1> This is animating background ...</h1>
</div>
You can achieve that by just using the to and from attributes for the keyframes change. The effect is kind of weird, because the background top and bottom do not fit together...
* {
margin: 0;
padding: 0;
}
div {
width: 100vw;
height: 100vh;
animation: change 3s linear infinite;
background-image: url(http://hdwallpaperbackgrounds.net/wp-content/uploads/2016/07/background-pictures-2.jpg);
background-size: cover;
background-repeat: repeat-y;
background-position: 0 0;
}
#keyframes change {
from {
background-position: 0 100vh;
}
to {
background-position: 0 0;
}
}
<div>
<h1> This is animating background ...</h1>
</div>