Left to right CSS3 animation starting from center - html

I'm working on an HTML5 game which has an object that needs to start centered, move to the left out of view and then appear from the right to complete and repeat.
#-webkit-keyframes marquee {
0% { transform: translate3d(-50%, 0, 0); }
50% { transform: translate3d(-100vw, 0, 0); }
75% { transform: translate3d(100vw, 0, 0); }
100% { transform: translate3d(-50%, 0, 0); }
}
#-moz-keyframes marquee {
0% { transform: translate3d(-50%, 0, 0); }
50% { transform: translate3d(-100vw, 0, 0); }
75% { transform: translate3d(100vw, 0, 0); }
100% { transform: translate3d(-50%, 0, 0); }
}
#-ms-keyframes marquee {
0% { transform: translate3d(-50%, 0, 0); }
50% { transform: translate3d(-100vw, 0, 0); }
75% { transform: translate3d(100vw, 0, 0); }
100% { transform: translate3d(-50%, 0, 0); }
}
#keyframes marquee {
0% { transform: translate3d(-50%, 0, 0); }
50% { transform: translate3d(-100vw, 0, 0); }
75% { transform: translate3d(100vw, 0, 0); }
100% { transform: translate3d(-50%, 0, 0); }
}
div {
width: 200px;
height: 50px;
background: red;
position: absolute;
left: 50%;
top: 15px;
transform: translate3d(-50%, 0, 0);
animation: marquee 5s linear infinite;
}
http://jsfiddle.net/gRoberts/0esr1abL/
I've created a simple fiddle which shows what I am trying to do, however you'll notice that once it reaches the left, it zips to the right and then carries on. I've tried getting around this by using opacity, however this causes undesired effects (object fades in/out.)
I have the following working now, however the object starts off screen, which isn't ideal:
#-webkit-keyframes marquee {
0% { transform: translate3d(calc(100vw + 100%), 0, 0); }
100% { transform: translate3d(calc(0vw - 100%), 0, 0); }
}
#-moz-keyframes marquee {
0% { transform: translate3d(calc(100vw + 100%), 0, 0); }
100% { transform: translate3d(calc(0vw - 100%), 0, 0); }
}
#-ms-keyframes marquee {
0% { transform: translate3d(calc(100vw + 100%), 0, 0); }
100% { transform: translate3d(calc(0vw - 100%), 0, 0); }
}
#keyframes marquee {
0% { transform: translate3d(calc(100vw + 100%), 0, 0); }
100% { transform: translate3d(calc(0vw - 100%), 0, 0); }
}
div {
width: 200px;
height: 50px;
background: red;
position: absolute;
top: 15px;
animation: marquee 5s linear infinite;
}
http://jsfiddle.net/gRoberts/rr969j09/
Any suggestions on to get the above fiddle working but starting from the center?

You can use the first snippet from the question but change the opacity immediately (within .1% of the animation duration). Changing the opacity from 1 to 0 between 50% and 50.1% would make the element get hidden abruptly and not show up as it goes from the left to the right. Similarly changing the opacity back to 1 at 75.1% would make it visible when it comes back into view from the right.
Initially when you had tried, I think you may have changed the opacity at a higher interval and thus making it give a fade-in/out like appearance.
The animation was speeding up and slowing down because for the first 50% it was going from centre to left (-100vw) but it was taking only 25% of the time to come back in from the right to centre. I have modified this to allocate equal splits (that is, center to left from 0-45% and from right to centre between 55-100%) and this makes it look more smoother. It could be tuned further but I think you get the idea.
#keyframes marquee {
0% {
transform: translate3d(-50%, 0, 0);
}
45% {
transform: translate3d(-100vw, 0, 0);
opacity: 1;
}
45.1% {
opacity: 0; /* at this point in time the element is fully on the left and hidden */
}
55% {
transform: translate3d(100vw, 0, 0);
opacity: 0; /* at this point in time the element is fully on the right but still hidden */
}
55.1% {
opacity: 1; /* now we make it visible again so that it can come back into view */
}
100% {
transform: translate3d(-50%, 0, 0);
}
}
div {
width: 200px;
height: 50px;
background: red;
position: absolute;
left: 50%;
top: 15px;
animation: marquee 5s linear infinite;
}
body {
overflow: hidden;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div></div>

Related

Add CSS shaking animation to a button without hover

I am trying to make the button shake without hovering on it. When I am removing the "hover" element, the button doesn't shake at all. What should I change in the code to make it work?
.first_button:hover {
animation: shake 0.82s cubic-bezier(.36, .07, .19, .97) both;
transform: translate3d(0, 0, 0);
backface-visibility: hidden;
perspective: 1000px;
}
#keyframes shake {
10%,
90% {
transform: translate3d(-1px, 0, 0);
}
20%,
80% {
transform: translate3d(2px, 0, 0);
}
30%,
50%,
70% {
transform: translate3d(-4px, 0, 0);
}
40%,
60% {
transform: translate3d(4px, 0, 0);
}
}
<button class="first_button">Button</button>
Use the infinite prop for your animation.
More info on animation syntax
.first_button {
animation: shake 0.82s cubic-bezier(.36, .07, .19, .97) both infinite;
transform: translate3d(0, 0, 0);
backface-visibility: hidden;
perspective: 1000px;
}
#keyframes shake {
10%,
90% {
transform: translate3d(-1px, 0, 0);
}
20%,
80% {
transform: translate3d(2px, 0, 0);
}
30%,
50%,
70% {
transform: translate3d(-4px, 0, 0);
}
40%,
60% {
transform: translate3d(4px, 0, 0);
}
}
<button class="first_button">Button</button>

How can I move an element upside down while moving forward?

.anim-cat-2 img {
animation: animcats 10s infinite;
}
#keyframes animcats {
0% {
transform: translate(100px, 100px)rotate(-15deg);
}
10% {
transform: translate(150px, -100px) rotate(15deg);
}
20% {
transform: rotate(-15deg) translate(200px, 100px);
}
30% {
transform: rotate(15deg)translate(250px, -100px);
}
40% {
transform: rotate(-15deg)translate(300px, 100px);
}
50% {
transform: rotate(15deg)translate(350px, -100px);
}
60% {
transform: rotate(-15deg)translate(400px, 100px);
}
70% {
transform: rotate(15deg)translate(450px, -100px);
}
80% {
transform: rotate(-15deg)translate(500px, 100px);
}
90% {
transform: rotate(15deg)translate(550px, -100px);
}
100% {
transform: rotate(-15deg)translate(600 px, 100px);
}
}
<div class="anim-cat-2">
<img src="https://66.media.tumblr.com/95927edf9e85ece73dac69ade623432c/tumblr_otrmgaTiin1vxe4v6o1_400.png" style="height: 130px;width: 180px;margin-top: 20px;">
</div>
i cant make it go smoothly forward in right tilt, its suppossed to make the right angle and move up and down moving forward. i already tried to change the degree, x n y axis but it still didnt work,please help me
Run code snippet.
Although it makes the right angle, move up and down, it did'nt meets the expectation at the end of the animation.
.anim-cat-2 img {
animation: animcats 8s infinite;
}
#keyframes animcats {
0% {
transform: rotate(0deg) translate(0px,0px);
}
8% {
transform: rotate(9deg);
}
10% {
transform: translate(100px,150px);
}
20% {
transform:rotate(-9deg) translate(100px,150px);
}
30% {
transform: translate(200px,0px);
}
40% {
transform: rotate(9deg) translate(200px,0px);
}
50% {
transform: translate(300px,150px);
}
60% {
transform: rotate(-9deg) translate(300px,150px);
}
70% {
transform: translate(400px,0px);
}
80% {
transform: rotate(9deg) translate(400px,0px);
}
90% {
transform: translate(500px,150px);
}
100% {
transform: rotate(-9deg) translate(500px,150px);
}
}
<div class="anim-cat-2">
<img src="https://66.media.tumblr.com/95927edf9e85ece73dac69ade623432c/tumblr_otrmgaTiin1vxe4v6o1_400.png" style="height: 130px;width: 180px;margin-top: 20px;">
</div>

Making css-doodle responsive with media queries

I've set up a background for my page using css-doodle. It looks fine when on full screen but because of the way my grid is set up, the shapes get smaller along with the screen and are completely invisible on split screen or when simulating mobile devices. I would like them to visually always be the same size.
Here's a media query that I tried adding but it doesn't seem to do anything. (I'm fairly new to RWD so I'm not sure what the issue is.)
#media only screen and (max-width: 600px) {
.doodle {
grid: 22 / 100vmax;
grid-gap: 20px;
}
}
And here's my CSS-Doodle code.
<css-doodle grid="5" class="doodle">
:doodle {
#grid: 50 / 100vmax;
grid-gap: 30px;
}
background: white;
opacity: #r(-1, .08);
clip-path: polygon(51% 26%, 0% 100%, 100% 100%);
</css-doodle>
Using the example of the docs, I tried using em units instead of vmax. Codepen Demo
html {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
body {
background: linear-gradient(to bottom, rgb(8, 36, 83), rgb(2, 91, 137));
}
.doodle {
position: fixed;
z-index: -1;
animation: spin 200s infinite;
}
#keyframes spin {
from {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
to {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
#media only screen and (max-width: 600px) {
.doodle {
#grid: 25 / 100em;
grid-gap: 20px;
}
}
<css-doodle grid="5" class="doodle">
:doodle {
#grid: 50/100em;
grid-gap: 30px;
}
background: white;
opacity: #r(-1, .08);
clip-path: polygon(51% 26%, 0% 100%, 100% 100%);
animation: flow #r(10s, 50s) infinite linear;
#keyframes flow {
0%, 100%{
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0)
}
50% {
-webkit-transform: translate3d(#r(-500%, 1000%), #r(-500%, 1000%), 0);
transform: translate3d(#r(-500%, 1000%), #r(-500%, 1000%), 0);
}
}
</css-doodle>
<script src="https://cdnjs.cloudflare.com/ajax/libs/css-doodle/0.7.1/css-doodle.js"></script>

Different transition for on/off class but same property

I have a div that translates itself in with a simple transition:
div{
transform: translate3d(0, -100%, 0);
transition: all .5s;
}
div.active{
transform: translate3d(0, 0, 0);
}
Then, I toggle the class with JS and it works perfectly. This does the following:
On --> Slide div down
Off --> Slide div up
What I want to do is:
On --> Slide div down
Off --> Slide div down again
Is there a way to achieve this?
EDIT: Here's a demo of what it does: https://jsfiddle.net/bwzy89oq/ (click anywhere)
This does what you need, sort of, but it starts off playing the animation. I'm working on how to logic that out:
div {
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
transform: translate3d(0, 100%, 0);
background: black;
transition: all .5s ease-in-out;
animation: slider2 .5s forwards;
}
div.active {
animation: slider .5s forwards;
/*transform: translate3d(0, 0, 0);*/
}
#keyframes slider {
from {
transform: translate3d(0, -100%, 0);
}
to {
transform: translate3d(0, 0, 0);
}
}
#keyframes slider2 {
from {
transform: translate3d(0, 0, 0);
}
to {
transform: translate3d(0, 100%, 0);
}
}

CSS3 animation only on certain pages

This is my code for CSS3 animation: shake that I applied on one of the nav links. It works like a charm, but I want it turned off when someone opens the page that it is linked to it.
How can I solve this problem?
#menu-item-313 {
animation: shake 1.4s;
-webkit-animation-iteration-count: 10; /* Chrome, Safari, Opera */
animation-iteration-count: 6;
transform: translate3d(0, 0, 0);
}
#keyframes shake {
10%, 90% {
transform: translate3d(-2px, 0, 0);
}
20%, 80% {
transform: translate3d(4px, 0, 0);
}
30%, 50%, 70% {
transform: translate3d(-8px, 0, 0);
}
40%, 60% {
transform: translate3d(8px, 0, 0);
}
}
One way is to set a class on the body, like this, and use the :not selector
Sample not shaking
body:not(.pg313) #menu-item-313 {
animation: shake 1.4s;
-webkit-animation-iteration-count: 10; /* Chrome, Safari, Opera */
animation-iteration-count: 6;
transform: translate3d(0, 0, 0);
}
#keyframes shake {
10%, 90% {
transform: translate3d(-2px, 0, 0);
}
20%, 80% {
transform: translate3d(4px, 0, 0);
}
30%, 50%, 70% {
transform: translate3d(-8px, 0, 0);
}
40%, 60% {
transform: translate3d(8px, 0, 0);
}
}
<body class="pg313">
<div id="menu-item-313">
Menu item 313
</div>
<div id="menu-item-314">
Menu item 314
</div>
</body>
Sample shaking
body:not(.pg313) #menu-item-313 {
animation: shake 1.4s;
-webkit-animation-iteration-count: 10; /* Chrome, Safari, Opera */
animation-iteration-count: 6;
transform: translate3d(0, 0, 0);
}
#keyframes shake {
10%, 90% {
transform: translate3d(-2px, 0, 0);
}
20%, 80% {
transform: translate3d(4px, 0, 0);
}
30%, 50%, 70% {
transform: translate3d(-8px, 0, 0);
}
40%, 60% {
transform: translate3d(8px, 0, 0);
}
}
<body class="pg314">
<div id="menu-item-313">
Menu item 313
</div>
<div id="menu-item-314">
Menu item 314
</div>
</body>