CSS transform and rotate with animation - html

HTML
<div id="div1">
<div id="div2">HELLO</div>
</div>
<div id="div3">
<div id="div4">HELLO</div>
</div>
CSS
#div1
{
position: relative;
height: 150px;
width: 150px;
margin: 50px;
padding:10px;
border: 1px solid black;
-webkit-perspective:150px; /* Chrome, Safari, Opera */
perspective:150px;
}
#div2
{
padding:50px;
position: absolute;
border: 1px solid black;
background-color: red;
-webkit-transform-origin:0%; /* Chrome, Safari, Opera */
-webkit-transform: rotateY(117deg); /* Chrome, Safari, Opera */
transform: rotateX(-75deg);
}
#div3
{
position: relative;
height: 150px;
width: 150px;
margin: 50px;
padding:10px;
border: 1px solid black;
-webkit-perspective:150px; /* Chrome, Safari, Opera */
perspective:150px;
}
#div4
{
padding:50px;
position: absolute;
border: 1px solid black;
background-color: red;
-webkit-transform-origin:0%; /* Chrome, Safari, Opera */
-webkit-transform: rotateY(0deg); /* Chrome, Safari, Opera */
transform: rotateX(-75deg);
}
Trying to do this rotate the inner div from starting position to final position with a delay 2
Two positions
Code for doing above got it from Here But its not working .What changes to make to make it work
-webkit-backface-visibility: visible;
-webkit-transform-origin: 0% 50%;
-webkit-transform: perspective(800px) rotateY(90deg) rotateY(-90deg);

Here: FIDDLE: http://jsfiddle.net/9dqAK/12/
HTML
<div id="stage">
<div id="spinner">
hello
</div>
</div>
CSS
#-webkit-keyframes spinner {
from {
-webkit-transform: rotateY(0deg);
-moz-transform: rotateY(0deg);
}
to {
-webkit-transform: rotateY(-360deg);
-moz-transform: rotateY(-360deg);
}
}
#spinner {
-webkit-transform-origin: 150px 0 0;
-webkit-animation-name: spinner;
-webkit-animation-timing-function: linear;
-webkit-animation-iteration-count: infinite;
-webkit-animation-duration: 6s;
-webkit-transform-style: preserve-3d;
-moz-transform-origin: 150px 0 0;
-moz-animation-name: spinner;
-moz-animation-timing-function: linear;
-moz-animation-iteration-count: infinite;
-moz-animation-duration: 6s;
-moz-transform-style: preserve-3d;
padding:50px;
position: absolute;
border: 1px solid black;
background-color: red;
}
#spinner:hover {
-webkit-animation-play-state: paused;
}

You forgot to add when to do the animation. I assume that it is upon let's say hover.
So, add end styles to :hover pseudo-class. And, also specify some transition so that you can see it animating.
Demo: http://jsfiddle.net/abhitalks/WEX75/4/
CSS:
#div1 {
position: relative;
height: 150px;
width: 150px;
margin: 50px;
padding:10px;
border: 1px solid black;
-webkit-perspective:150px;
}
#div2 {
padding:50px;
position: absolute;
border: 1px solid black;
background-color: red;
-webkit-transform-origin:0%;
-webkit-transform: rotateY(117deg);
transition: 1s all;
}
#div1:hover > #div2 {
-webkit-backface-visibility: visible;
-webkit-transform-origin: 50% 50%;
-webkit-transform: perspective(500px) rotateY(0deg);
}
Edit: (After reading Op's comments somewhere above)
You want the animation to happen on page load. So, in that case just apply the relevant CSS using jQuery. Wrap that code in domready.
Demo 2: http://jsfiddle.net/abhitalks/WEX75/3/
JS:
$("#div2").css({
'-webkit-backface-visibility': 'visible',
'-webkit-transform-origin': '50% 50%',
'-webkit-transform': 'perspective(500px) rotateY(0deg)'
});
Edit 2:
You can introduce a delay of 2s by using transition-delay: 2s;.
Demo 3: http://jsfiddle.net/abhitalks/WEX75/5/

Add -webkit- to your last transform CSS declaration

Related

Loader timing issues

I am confused why the after selector is moving faster than the no selector div, can anyone explain why this is the case? The before and after selectors are working perfectly but I cannot seem to edit the actual main div I have the selectors off of. I am kinda new at this and apologize for poor formatting or anything of that sort, But I would greatly appreciate some help on this! I want the inner cicle to move the fastest, the middle to move slower, and the outer the slowest.
https://jsfiddle.net/wnmsfudo/1/
html,
body {
height: 100vh;
width: 100vw;
}
.loader {
margin-top: -80px;
content: "";
display: block;
position: absolute;
top: 50vh;
left: 46vw;
width: 100px;
height: 100px;
border-radius: 50%;
border: 3px solid transparent;
border-top-color: red;
animation: spin 2s linear infinite;
}
.loader::before {
content: "";
display: block;
position: absolute;
top: 12.5px;
left: 12.5px;
width: 75px;
height: 75px;
border-radius: 50%;
border: 3px solid transparent;
border-top-color: green;
animation: spin 1s linear infinite;
}
.loader::after {
content: "";
display: block;
position: absolute;
top: -12.5px;
left: -12.5px;
width: 125px;
height: 125px;
border-radius: 50%;
border: 3px solid transparent;
border-top-color: blue;
animation: spin 3s linear infinite;
}
#keyframes spin {
0% {
-webkit-transform: rotate(0deg);
/* Chrome, Opera 15+, Safari 3.1+ */
-ms-transform: rotate(0deg);
/* IE 9 */
transform: rotate(0deg);
/* Firefox 16+, IE 10+, Opera */
}
100% {
-webkit-transform: rotate(360deg);
/* Chrome, Opera 15+, Safari 3.1+ */
-ms-transform: rotate(360deg);
/* IE 9 */
transform: rotate(360deg);
/* Firefox 16+, IE 10+, Opera */
}
}
<div class="container">
<div class="loader"></div>
</div>
The issue is that the entire .loader div rotates once every 2 seconds, and that the ::before and ::after pseudo elements therefore rotate with it. Their own rotation is added to the parent's!
So the solution is to adapt the pseudo-elements' rotations so that they move relatively to the main element. The ::after even needs to rotate in reverse if you want to make it appear slower.
html, body {
height: calc(100% - 16px);
}
.loader {
margin-top: -80px;
content: "";
display: block;
position: absolute;
top: 50vh;
left: 46vw;
width: 100px;
height: 100px;
border-radius: 50%;
border: 3px solid transparent;
border-top-color: red;
animation: spin 2s linear infinite;
}
.loader::before {
content: "";
display: block;
position: absolute;
top: 12.5px;
left: 12.5px;
width: 75px;
height: 75px;
border-radius: 50%;
border: 3px solid transparent;
border-top-color: green;
animation: spin 5s linear infinite;
}
.loader::after {
content: "";
display: block;
position: absolute;
top: -12.5px;
left: -12.5px;
width: 125px;
height: 125px;
border-radius: 50%;
border: 3px solid transparent;
border-top-color: blue;
animation: spin 5s linear reverse infinite;
}
#keyframes spin {
0% {
-webkit-transform: rotate(0deg);
/* Chrome, Opera 15+, Safari 3.1+ */
-ms-transform: rotate(0deg);
/* IE 9 */
transform: rotate(0deg);
/* Firefox 16+, IE 10+, Opera */
}
100% {
-webkit-transform: rotate(360deg);
/* Chrome, Opera 15+, Safari 3.1+ */
-ms-transform: rotate(360deg);
/* IE 9 */
transform: rotate(360deg);
/* Firefox 16+, IE 10+, Opera */
}
}
<div class="container">
<div class="loader">
</div>
</div>

How to center a div (CSS loader)

JS Fiddle: fiddle and here is the code:
.loader {
border: 16px solid #f3f3f3;
border-radius: 50%;
border-top: 16px solid #3498db;
width: 120px;
height: 120px;
-webkit-animation: spin 2s linear infinite;
animation: spin 2s linear infinite;
}
#-webkit-keyframes spin {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); }
}
#keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
<div class="loader"></div>
How do I centre it horizontally & vertically?
I tried:
position:absolute;
left:50%;
top:50%;
transform:translate(-50%,-50%);
But transform:translate(-50%,-50%); do not work
Give below css to .loader class:
margin:auto;
left:0;
right:0;
top:0;
bottom:0;
position:fixed;
.loader {
border: 16px solid #f3f3f3;
border-radius: 50%;
border-top: 16px solid #3498db;
width: 120px;
height: 120px;
-webkit-animation: spin 2s linear infinite;
animation: spin 2s linear infinite;
margin:auto;
left:0;
right:0;
top:0;
bottom:0;
position:fixed;
}
#-webkit-keyframes spin {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); }
}
#keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
<div class="loader"></div>
You can make it position:absolute;
and give it:
top:0;
left:0;
right:0;
bottom:0;
to make it both vertically and horizontally aligned into the middle.
body {
overflow:hidden;
}
.loader {
border: 16px solid #f3f3f3;
border-radius: 50%;
border-top: 16px solid #3498db;
width: 120px;
height: 120px;
-webkit-animation: spin 2s linear infinite;
animation: spin 2s linear infinite;
margin:auto;
top:0;
left:0;
bottom:0;
right:0;
position:absolute;
}
#-webkit-keyframes spin {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); }
}
#keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
<div class="loader"></div>
Easiest way to center element using CSS is by using flexbox. No hacks required.
if need to set parent container with display: flex.
<div class="container">
<div class="item">
Aligned Item
<div>
</div>
CSS:
.container {
display: flex;
align-items: center;
justify-content: center;
}
.item {
width: 200px;
height: 200px;
}
All elements with class item will be centrally aligned.
More details can be found at
https://philipwalton.github.io/solved-by-flexbox/demos/vertical-centering/
Several ways to do it.
Using flexbox. This would work in any container anywhere on the page.
body { /* or some wrapper, if you plan to have other things in body */
min-height: 100vh; /* this just expands the body height so the vertical alignment is visible in the snippet */
display: flex;
justify-content: center; /* center horizontally */
align-items: center; /* center vertically */
}
.loader {
border: 16px solid #f3f3f3;
border-radius: 50%;
border-top: 16px solid #3498db;
width: 120px;
height: 120px;
animation: spin 2s linear infinite;
}
#keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
<div class="loader"></div>
Using position: absolute. This centers the div relative to the document, not loader's parent element.
.loader {
border: 16px solid #f3f3f3;
border-radius: 50%;
border-top: 16px solid #3498db;
width: 120px;
height: 120px;
animation: spin 2s linear infinite;
/* i added this: */
position: absolute;
left: calc(50% - 120px / 2); /* 50 % of body width minus half of .loader size… */
top: calc(50% - 120px / 2); /* …and the same thing with height */
}
#keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
<div class="loader"></div>
Please add body {height: 100vh;} and update the css of loader div as the following:
.loader {
border: 16px solid #f3f3f3;
border-radius: 50%;
border-top: 16px solid #3498db;
width: 120px;
height: 120px;
-webkit-animation: spin 2s linear infinite;
animation: spin 2s linear infinite;
margin: auto;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
https://jsfiddle.net/7baw2rmp/

Keep box-shadow direction consistent while rotating

When giving e.g. a <div> a box-shadow as well as rotating it will cause a rotation of the box-shadow direction - which is problematic when the box-shadow should create an illusion of lighting.
Example: https://jsfiddle.net/5h7z4swk/
div {
width: 50px;
height: 50px;
margin: 20px;
box-shadow: 10px 10px 10px #000;
display: inline-block;
}
#box1 {
background-color: #b00;
}
#box2 {
background-color: #0b0;
transform: rotate(30deg);
}
#box3 {
background-color: #00b;
transform: rotate(60deg);
}
#box4 {
background-color: #b0b;
transform: rotate(90deg);
}
#keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
#box6 {
background-color: #0bb;
animation-name: spin;
animation-duration: 2s;
animation-iteration-count: infinite;
}
<div id="box1"></div>
<div id="box2"></div>
<div id="box3"></div>
<div id="box4"></div>
<div id="box6"></div>
The answer to this problem should look similar to this mock up:
How can I rotate a <div> and still keep the box-shadow going the same direction?
The solution should be pure CSS...
Note: The animation in the CSS is for demonstration purposes. The use case will use JavaScript to set the rotation. But the JavaScript will know nothing about the box-shadow as it is in the responsibility of the design to define a (or many...) shadows. That's why it should be a pure CSS solution.
Keeping direction of an offset box-shadow consistent during rotation is simple with CSS transforms.
This approach relies on the fact that the transform origin is moved with the transforms. This means that when several transforms are set on the same element, the coordinate system of each transform changes according to the previous ones.
In the following example, the blue element is a pseudo element and the shadow is the div element:
div {
width: 40px; height: 40px;
margin: 40px;
box-shadow: 0px 0px 10px 5px #000;
animation: spinShadow 2s infinite;
background-color: #000;
}
#keyframes spinShadow {
to { transform: rotate(360deg); }
}
div:before {
content: '';
position: absolute;
left:-5px; top:-5px;
width: 50px; height: 50px;
transform: rotate(0deg) translate(-10px, -10px) rotate(0deg);
animation:inherit;
animation-name: spinElt;
background-color: #0bb;
}
#keyframes spinElt {
to { transform: rotate(-360deg) translate(-10px, -10px) rotate(360deg); }
}
<div></div>
Explanation of the transition property on the pseudo element (See the following code snippet for an illustration of the steps):
transform: rotate(-360deg) translate(-10px, -10px) rotate(360deg)
rotate(-360deg) counters the rotation of the parent to make the pseudo element static.
translate(-10px, -10px) the pseudo element is translated to make the shadow offset
rotate(360deg) the pseudo element is rotated in the same direction as parent
div {
width: 40px; height: 40px;
margin: 40px;
box-shadow: 0px 0px 10px 5px #000;
animation: spinShadow 2s infinite;
background-color: #000;
}
#keyframes spinShadow {
to { transform: rotate(360deg); }
}
div:before {
content: '';
position: absolute;
left:-5px; top:-5px;
width: 50px; height: 50px;
animation:inherit;
background-color: #0bb;
}
#first:before{
transform: rotate(0deg);
animation-name: first;
}
#keyframes first {
to { transform: rotate(-360deg); }
}
#second:before{
transform: rotate(0deg) translate(-10px, -10px);
animation-name: second;
}
#keyframes second {
to { transform: rotate(-360deg) translate(-10px, -10px); }
}
#complete:before{
transform: rotate(0deg) translate(-10px, -10px) rotate(0deg);
animation-name: complete;
}
#keyframes complete {
to { transform: rotate(-360deg) translate(-10px, -10px) rotate(360deg); }
}
<ol>
<li>Counter rotate:<div id="first"></div></li>
<li>Translate :<div id="second"></div></li>
<li>Rotate:<div id="complete"></div></li>
<ol>
You could as well integrate box-shadow direction inside animation frames:
div {
display: inline-block;
margin: 1em ;
height: 50px;
width: 50px;
box-shadow: 15px 15px 15px 5px gray;
animation: rte 5s infinite linear;
}
.red {
background: red
}
.green {
background: green;
animation-delay:2s;
}
.blue {
background: blue;
animation-delay:4s;
}
.bob {
background: #b0b;
animation-delay:6s;
}
.cyan {
background: cyan;
animation-delay:8s;
}
#keyframes rte {
25% {
box-shadow: 15px -15px 15px 5px gray;
}
50% {
box-shadow: -15px -15px 15px 5px gray;
}
75% {
box-shadow: -15px 15px 15px 5px gray;
}
100% {
transform: rotate(360deg);
}
}
<div class="red"></div>
<div class="green"></div>
<div class="blue"></div>
<div class="bob"></div>
<div class="cyan"></div>
Inspired by the other answers I created my own answer as well:
https://jsfiddle.net/zoxgbcrg/1/
.shadow {
background-color: black !important;
width: 40px;
height: 40px;
box-shadow: 0px 0px 10px 5px #000;
margin-top: 35px;
margin-left: 35px;
position: absolute;
z-index: -1;
}
<div class="box1 shadow"></div><div class="box1"></div>
The trick is also to create an additional <div> to handle the shadow. In my case it's not a :before but a real DOM element that is moved by margin.
Note: it seems that of today (31.01.2016) Firefox and Chrome have a subtile rendering difference. So for Firefox https://jsfiddle.net/zoxgbcrg/ is creating the desired result, for Chrome I suggest https://jsfiddle.net/zoxgbcrg/1/

Transition animation need some CSS

I am trying to make a page load transition effective div. This is my DEMO FULL PAGE full page from codepen.io and also this is example page with code DEMO WITH CODE
If you open the demo full page then you can see there is a one image and this image opening with transition animation. But there is something went wrong.
When page load the image opening transition but it is opening up to bottom . I want to open it in the middle, without any deviation.
Anyone can help me in this regard ? How can i fixed it ?
This is my CSS code:
.test {
margin: 0px auto;
width: 200px;
height: auto;
margin-top: 50px;
}
.testtransition {
transform: scale(1.2);
width: 200px;
height: 200px;
margin: 0px auto;
margin-top: 50px;
overflow: hidden;
border-radius: 50%;
}
.testtransition img {
width: 100%;
max-width: 200px;
max-height: 200px;
opacity: 1;
-moz-animation: scale 0.8s;
/* Firefox */
-webkit-animation: scale 0.8s;
/* Safari and Chrome */
-o-animation: scale 0.8s;
border-radius: 50%;
-webkit-animation: scale 0.9s;
/* Safari, Chrome and Opera > 12.1 */
-moz-animation: scale 0.9s;
/* Firefox < 16 */
-ms-animation: scale 0.9s;
/* Internet Explorer */
-o-animation: scale 0.9s;
/* Opera < 12.1 */
animation: scale 0.9s;
}
#keyframes scale {
from {
width: 20px;
opacity: 0;
}
to {
width: 200px;
opacity: 1;
}
}
#-moz-keyframes scale {
/* Firefox */
from {
width: 20px;
opacity: 0;
}
to {
width: 200px;
opacity: 1;
}
}
#-webkit-keyframes scale {
/* Safari and Chrome */
from {
width: 20px;
opacity: 0;
}
to {
width: 200px;
opacity: 1;
}
}
#-o-keyframes scale {
/* Opera */
from {
width: 20px;
opacity: 0;
}
to {
width: 200px;
opacity: 1;
}
}
Here's a working pen. A summary of my changes:
Added text-align: center on .testtransition to keep the image centered
Added width, height, and padding to the animation to keep the image centered throughout the animation
Removed the width parameter from the img tag to keep things simple :)
This should do the trick! You need to set a from and to in each animation.
http://codepen.io/anon/pen/GJZvJB
.testtransition {
width: 200px;
height: 200px;
margin: 0px auto;
margin-top: 50px;
overflow: hidden;
-webkit-border-radius: 50%;
-moz-border-radius: 50%;
border-radius: 50%;
-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
-webkit-transform: translate3d(0, 0, 0);
-moz-transform: translate3d(0, 0, 0);
}
.testtransition img {
-webkit-animation: scaleAnim 1s;
-moz-animation: scaleAnim 1s;
-o-animation: scaleAnim 1s;
}
#-webkit-keyframes scaleAnim {
from { -webkit-transform: scale(0) }
to { -webkit-transform: scale(1) }
}
#-moz-keyframes scaleAnim {
from { -moz-transform: scale(0) }
to { -moz-transform: scale(1) }
}
#-o-keyframes scaleAnim {
from { -o-transform: scale(0)}
to { -o-transform: scale(1)}
}
You may have to play with the initial image to maintain an attractive circle, for now i've added backface-visibility.

display:table-cell not supported in Mozilla Firefox

I have a following div
<div>Circle</div>
I applied styles to the circle using the nth-child, the circle and the text inside it are supposed to spin. In the beginning, when I created the circle, I used
display:table-cell;
property to position the text in the center of the circle. When I run my page in Chrome, all the styles including the positioning look perfect. However, when I ran my code in Mozilla Firefox, I noticed that the positioning left and top don't work when I have display:table-cell; property set. They only work if I remove the display. However, if I remove the display, then my text is messy. In that case I decided to create a pseudo div. I managed to position the text in the the center of the circle, however, when I refresh the page, the spinning circle pushes to the right and the text doesn't spin. How can I fix this problem?
style.css
div:nth-child(3) {
width: 150px;
height: 150px;
background: -webkit-linear-gradient(#006600, #009900, #006600);
/* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(#006600, #009900, #006600);
/* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(#006600, #009900, #006600);
/* For Firefox 3.6 to 15 */
background: linear-gradient(#006600, #009900, #006600);
/* Standard syntax */
-moz-border-radius: 150px;
-webkit-border-radius: 150px;
border-radius: 150px;
text-align: center;
vertical-align: middle;
position: relative;
display:table-cell;
top: 5%;
left: 75px !important;
color: #F0F0F0;
font-size: 25px;
-webkit-animation-name: spin;
-webkit-animation-duration: 500ms;
-webkit-animation-iteration-count: 1;
-webkit-animation-timing-function: linear;
-moz-animation-name: spin;
-moz-animation-duration: 500ms;
-moz-animation-iteration-count: 1;
-moz-animation-timing-function: linear;
-ms-animation-name: spin;
-ms-animation-duration: 500ms;
-ms-animation-iteration-count: 1;
-ms-animation-timing-function: linear;
animation-name: spin;
animation-duration: 500ms;
animation-iteration-count: 1;
animation-timing-function: linear;
}
#-ms-keyframes spin {
from {
-ms-transform: rotate(0deg);
}
to {
-ms-transform: rotate(360deg);
}
}
#-moz-keyframes spin {
from {
-moz-transform: rotate(0deg);
}
to {
-moz-transform: rotate(360deg);
}
}
#-webkit-keyframes spin {
from {
-webkit-transform: rotate(0deg);
}
to {
-webkit-transform: rotate(360deg);
}
}
#keyframes spin {
from {
transform:rotate(0deg);
}
to {
transform:rotate(360deg);
}
}
The following code is when I made a pseudo div, I did not include the css for spinning since it is the same as in the code above. The circle spins, but for that second that it spins it does to the right and then comes back. The text doesn't spin.
div:nth-child(3){
text-align:center;
position:relative;
width: 150px;
top: 50%;
left: 75%;
color: #F0F0F0;
font-size: 20px;
}
div:nth-child(3):after {
content: '';
width: 150px;
height: 150px;
position: absolute;
bottom: -80%;
left: 50%;
transform: translateX(-50%);
z-index: -1;
background: -webkit-linear-gradient(#006600, #009900, #006600);
/* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(#006600, #009900, #006600);
/* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(#006600, #009900, #006600);
/* For Firefox 3.6 to 15 */
background: linear-gradient(#006600, #009900, #006600);
/* Standard syntax */
-moz-border-radius: 150px;
-webkit-border-radius: 150px;
border-radius: 150px;
}
jsFiddle : http://jsfiddle.net/jLwmknk1/
Try applying this:
display: table;
table-layout: fixed;
instead of display:table-cell;