I have two backgrounds:
body {
background-image: url(img/nemo.png),url(img/ocean.png);
}
How do I make nemo.png background move infinitely from left-right but not affecting ocean.png background?
EDIT: When he reaches the right most edge (and the image is no longer visible), he will appear from the left edge again and start doing the drifting from left-to-right.
This can be done with pure CSS 3, e.g keyframed animations:
Demo: http://jsfiddle.net/dghsV/112
body {
background-image: url("http://www.animaatjes.nl/disney-plaatjes/disney-plaatjes/finding-nemo/nemo11.gif"), url("http://images2.layoutsparks.com/1/200022/ocean-dreams-blue-waves.jpg");
background-repeat: no-repeat;
background-position: 50% 0%, 0;
-moz-animation: swim 2s linear 0s infinite;
-webkit-animation: swim 2s linear 0s infinite;
animation: swim 2s linear 0s infinite;
}
#-moz-keyframes swim {
from { background-position: 200% 0, 0 0; }
to { background-position: -100% 0, 0 0; }
}
#-webkit-keyframes swim {
from { background-position: 200% 0, 0 0; }
to { background-position: -100% 0, 0 0; }
}
#keyframes swim {
from { background-position: 200% 0, 0 0; }
to { background-position: -100% 0, 0 0; }
}
Syntax
animation : animation-name animation-duration animation-timing-function animation-delay animation-iteration-count animation-direction;
The feature is experimental, so vendor-prefixes (eg -webkit-) have to be added (see also Can I use CSS3 Animation for compatibility notes). In my demo, I've used all properties, except for the last one.
Heres an option:
Create an animated GIF from the nemo.png which is a simple animation of the image moving from left to right.
Then create the layered backgrounds by setting ocean.png to the background of the body of your page.
Then create a div which with the following css:
width:100%;
height:100%;
background-position:center center;
background-image: url(img/moving-nemo.gif);
The div will be an all-encompassing container for all of your content which will give you a layered background effect.
make only ocean the background and create a div with the nemo as background:
<div id="nemo"></div>
#nemo {
background: url(nemo.png) no-repeat;
width: 100px;
height: 100px;
position:absolute;
left: 0px;
top: 500px;
z-index:-10;
}
than you can animate this div with javascript (jQuery):
<script type="text/javascript">
$(document).ready(function() {
SwimRight();
});
//duration is in ms
function SwimRight() {
$('#nemo').css({positionLeft: 0});
$('#nemo').animate(
{ left: $(document).width() },
{ duration: 5000,
easing: normal,
queue: true,
complete: SwimRight}
);
</script>
Related
I have created a rotating linear-gradient background. Unfortunately, as it rotates, in the corners you can see white screen. I am trying to get it so the color fills the viewport with nothing visible but the rotating gradient.
Here is my code so far (done on codepen.io):
HTML
<div class="fade"></div>
CSS
body {
margin: 0;
}
.fade {
background-image: linear-gradient(0deg, red, blue, red);
height: 100vh;
width: 100%;
animation: revolve 1s infinite linear;
}
#keyframes revolve {
from {
transform: scale3d(1,1,1) rotateZ(0deg);
}
to {
transform: scale3d(1,1,1) rotateZ(360deg);
}
}
I originally had the scale3d as (2,2,1). Changing it to (1,1,1) didn't solve anything. I have also tried changing the height and width to greater than 100wv and setting a background-position of center center, but neither of those worked.
Here is the codepen.
Use the vmax unit like below to create a big overflowing square:
body {
margin: 0;
overflow:hidden;
}
.fade {
background-image: linear-gradient(0deg, red, blue, red);
position:absolute;
height: 200vmax;
width: 200vmax;
left:50%;
top:50%;
transform:translate(-50%,-50%);
animation: revolve 1s infinite linear;
}
#keyframes revolve {
to {
transform:translate(-50%,-50%) rotate(360deg);
}
}
<div class="fade"></div>
You can simplify with a pseudo element:
html::before {
content:"";
background-image: linear-gradient(0deg, red, blue, red);
position:fixed;
top:-50vmax;
bottom:-50vmax;
left:-50vmax;
right:-50vmax;
animation: revolve 1s infinite linear;
}
#keyframes revolve {
to {
transform: rotate(360deg);
}
}
This question already has answers here:
background-position-y doesn't work in Firefox (via CSS)?
(9 answers)
Closed 3 years ago.
I know this has been answered somewhere already, but can't figure out what's wrong.
I have used repeating-linear-gradient for background and #-webkit-keyframes and #keyframes for animating it. It does work in google chrome but not in firefox.
HTML
<div class="menu_block"></div>
CSS
.menu_block {
height:100px; width:500px;
background: repeating-linear-gradient(45deg,#000,#000 20px,#fff 20px,#fff 40px);
background-size:56px 56px;
background-position-x:0%;
-webkit-animation:'slide' 30s infinite linear forwards;
}
#-webkit-keyframes 'slide' {
0%{background-position-x:0%;}
100% { background-position-x:100%;}
}
#keyframes 'slide' {
0%{ background-position-x:0%; }
100% { background-position-x:100%;}
}
jsfiddle is here
https://jsfiddle.net/mathews8881/0cj3L6wu/
Could somebody please help.
Try animating both params background-position: 0% 0;. Also missing non-prefixed animation rule.
.menu_block {
height: 100px;
width: 500px;
background: repeating-linear-gradient(45deg, #000, #000 20px, #fff 20px, #fff 40px);
background-size: 56px 56px;
background-position: 0 0;
-webkit-animation: slide 30s infinite linear forwards;
animation: slide 30s infinite linear forwards;
}
#keyframes slide {
0% {
background-position: 0 0;
}
100% {
background-position: 100% 0;
}
}
#-webkit-keyframes slide {
0% {
background-position: 0 0;
}
100% {
background-position: 100% 0;
}
}
<div class="menu_block">
</div>
Remove the single quotes on the animation/keyframes property values, the -webkit- prefix on animation and use background-position: 0%; instead of background-position-x:0%; (as mentioned in a comment, background-position-x is not supported across browser).
Note, since your prefixed properties weren't consistent applied I removed all of them, so you need to add them back to cover older browser versions needing them. Also, in your case, no need to use forwards in your animation when using infinite
.menu_block {
height:100px; width:500px;
background: repeating-linear-gradient(45deg,#000,#000 20px,#fff 20px,#fff 40px);
background-size:56px 56px;
background-position:0%;
animation: slide 30s infinite linear;
}
#keyframes slide {
0%{ background-position:0%; }
100% { background-position:100%;}
}
<div class="menu_block">
</div>
I'm very new to animations in CSS, very new indeed. What I'm trying to accomplish is relatively simple conceptually. I want to animate my target element, in which case my background image, back into it's original position.
For example,
#keyframes animatedBackground {
from {
background-position: 0 0;
}
to {
background-position: 100% 0;
}
}
Would move the background image to it's width, but I'm wondering if it would be possible to animate it back to 0. Otherwise, after reaching it's width, the animation will cut and the background will transition back to 0 instantly, making it seem very choppy.
You might be aware, but defining the animation is a lot like creating a function. You still have to invoke it somewhere.
HTML
<html>
<head>
<title>Hello, Lamb!</title>
<body>
<h1>Hello, Lamb!</h1<
</body>
</html>
CSS
body {
background-image: url('http://static.guim.co.uk/sys-images/Guardian/Pix/pictures/2014/4/11/1397210130748/Spring-Lamb.-Image-shot-2-011.jpg');
background-repeat: none;
animation: animatedBackground 5s infinite;
}
#keyframes animatedBackground {
0%{
background-position: 0 0;
}
50% {
background-position: 100% 0;
}
100% {
background-position: 0 0;
}
}
http://codepen.io/BigDaddyTeemoe/pen/qdQdMw
Something like this I think:
#keyframes animatedBackground {
0%{
background-position: 0 0;
}
50% {
background-position: 100% 0;
}
100% {
background-position: 0 0;
}
}
I'm trying to animate a background-image, so that the image appears from right to left.
I have used an image which has a greater width than the div-container, where the background is located. On start, the backgrond is the following
background: url(../img/zeppelin.png);
background-repeat: no-repeat;
background-position: right;
but when the page is loaded, I want the background to be animated, so that it is positioned left. This should take a eg. 2 seconds and only should be done one time. (the image should be positioned left afterwards).
I don't wanna use any mouse-events or pseudo-classes. Is there a way to animate it by using only CSS? I tried finding a solution with keyframes without success.
You could try using this tutorial: CSS Background Animation
#keyframes animatedBackground {
0% { background-position: 0 0; }
100% { background-position: -300px 0; }
}
#-moz-keyframes animatedBackground {
0% { background-position: 0 0; }
100% { background-position: -300px 0; }
}
#-webkit-keyframes animatedBackground {
0% { background-position: 0 0; }
100% { background-position: -300px 0; }
}
#-ms-keyframes animatedBackground {
0% { background-position: 0 0; }
100% { background-position: -300px 0; }
}
#-o-keyframes animatedBackground {
0% { background-position: 0 0; }
100% { background-position: -300px 0; }
}
html {
width: 100%;
height: 100%;
background-image: url(background.png);
background-position: 0px 0px;
animation: animatedBackground 10s linear infinite;
-moz-animation: animatedBackground 10s linear infinite;
-webkit-animation: animatedBackground 10s linear infinite;
-ms-animation: animatedBackground 10s linear infinite;
-o-animation: animatedBackground 10s linear infinite;
}
Example here: http://jsfiddle.net/verber/6rAGT/5/
Hope it that what you need)
working link: http://sagiavinash.com/labs/tests/css_anim/
This is an unorthodox trick.
<html>
<head>
<style>
.img{
width:1000px;
height:500px;
background:url(1.jpg) no-repeat left;
transition:background-position 1s;
-ms-transition:background-position 1s;
-moz-transition:background-position 1s;
-o-transition:background-position 1s;
-webkit-transition:background-position 1s;
}
</style>
</head>
<body>
<div class="img"></div>
<link rel="stylesheet" type="text/css" href="style.css">
</body>
</html>
style.css:
img{
background-position:right;
whats happening here is initially the css mentioned in the <style> is rendered.
later since the external stylesheet is in the body just before </body>.
So style.css is loaded after the resources in the are loaded. so there is a lag in implementation of the css which allows us to apply a css transition.
NO JAVASCRIPT, NO EVENTS still we get what we want!
you need to use animation and numbers for value,so it can calculate each position in between start - end.
basicly it would be :
html {
background:url(http://gravatar.com/avatar/21ffdef6c07de75379e31a0da98d9543?s=512) no-repeat;
background-size:10%;/* demo purpose */
background-position: 100% 0;
animation: bgmve 2s;
}
#keyframes bgmve {
to {background-position: 0 0;} /* make it short */
}
DEMO
to fire animation on load you can add a class to html via javascript:
onload=function() {
var root = document.getElementsByTagName( 'html' )[0]; // '0' to assign the first (and only `HTML` tag)
root.setAttribute( "class", "move" );
}
and css turns to be :
html {
background:url(http://gravatar.com/avatar/21ffdef6c07de75379e31a0da98d9543?s=512) no-repeat;
background-size:10%;
background-position: 100% 0;
}
.move {
animation: bgmve 2s;
}
#keyframes bgmve {
to {background-position: 0 0;
}
}
You have tagged Jquery. So will provide you with Jquery function for that.
$( "#ID" ).animate({
left: "50px",
}, 2000);
For class:
$( ".class" ).animate({
left: "50px",
}, 2000);
You can change your value of "Left" acc. to the position you want to give.
set position:relative; to the image with left:50%; for example and on document.ready event reduce the left value to e.g. 0 using jquery animate.
Check out this fiddle
I have this: http://d.pr/i/A2b3 which acts as the divider between the header and the main content.
The image is set as the background image, repeat-x, of the header container.
<header> <--- image is background of this
<div id="container"></div>
</header>
I want the image to slide across the screen slowly almost in a wave like effect. Is this possible with CCS3 animations? If so can someone help?
Thanks
I would suggest using jQuery and a simple image slider with a shortened image pause so the image keeps on switching. As far as i know its not possible to get a video to appear in a slider (not without a play button of some sort). http://www.catchmyfame.com/2009/06/04/jquery-infinite-carousel-plugin/ would be an example of what i would use.
Try this!
CSS:
header {
width: 100%;
height: 150px;
background-image: url(http://www.scottishheritageusa.org/imgs/header_separator.png); //replace with your image
background-position: 0px;
background-repeat: repeat-x;
-webkit-animation: myanim 5s infinite linear;
-moz-animation: myanim 5s infinite linear;
-o-animation: myanim 5s infinite linear;
animation: myanim 5s infinite linear;
}
#-webkit-keyframes myanim {
0% { background-position: 0px; }
100% { background-position: 100px; } /* set this to the width of the image */
}
#-moz-keyframes myanim {
0% { background-position: 0px; }
100% { background-position: 100px; } /* set this to the width of the image */
}
#-o-keyframes myanim {
0% { background-position: 0px; }
100% { background-position: 100px; } /* set this to the width of the image */
}
#keyframes myanim {
0% { background-position: 0px; }
100% { background-position: 100px; } /* set this to the width of the image */
}
I created a Fiddle for you to play with here:
http://jsfiddle.net/e3WLD/3/