Scaling messes up the z-index - html

I have a set of absolute divs having background images which contains animation.
when i apply scale property to the divs it messes up my z-index totally
here is the link to the fiddle https://jsfiddle.net/kq2soozp/3/ (Uncomment transform:scale() line)
The HTML Code
<div class='me'>
<div class="torso">
<div class="left leg">
<div class="left thigh">
<div class="left shin">
<div class="left foot">
<div class="left toes"></div>
</div>
</div>
</div>
</div>
<div class="right leg">
<div class="right thigh">
<div class="right shin">
<div class="right foot">
<div class="right toes"></div>
</div>
</div>
</div>
</div>
<div class="left arm">
<div class="left bicep">
<div class="left forearm">
<div class="kite"></div>
</div>
</div>
</div>
<div class="right arm">
<div class="right bicep">
<div class="right forearm"></div>
</div>
</div>
</div>
</div>
The CSS
.me,.me div{
background-repeat: no-repeat;
position: absolute;
-webkit-animation-duration: 2000ms;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
//-webkit-transform: scale(0.9);
}
.me{
top: 80px;
left: 350px;
-webkit-animation-name: me;
}
.torso{
height: 274px;
width: 86px;
background-image: url("https://s9.postimg.org/41xfy5cin/torso.png");
}
.arm{
left: 12px;
-webkit-transform-origin: 20px 10px;
}
.kite{
width: 395px;
height: 424px;
top: -115px;
left: 0px;
background-image: url("https://s3.postimg.org/ix240ioab/kite.png");
-webkit-transform: rotate(45deg) scale(0.6);
}
.right.bicep{
width: 51px;
height: 124px;
background-image: url("https://s3.postimg.org/mlrszzyb7/right-bicep.png");
}
.left.bicep{
width: 52px;
: 126px;
background-image: url("https://s3.postimg.org/jb3g048dv/left-bicep.png");
}
.left.forearm{
width: 37px;
height: 124px;
background-image: url("https://s3.postimg.org/7ahzze0z7/left-forearm.png");
-webkit-transform: rotate(-45deg);
}
.right.forearm{
width: 36px;
height: 121px;
background-image: url("https://s3.postimg.org/q6noj82ur/right-forearm.png");
-webkit-animation-name: right-forearm;
}
.left.thigh{
width: 69px;
height: 144px;
background-image: url("https://s3.postimg.org/577krq16b/left-thigh.png");
}
.right.thigh{
width: 69px;
height: 144px;
background-image: url("https://s3.postimg.org/72ud2vq0j/right-thigh.png");
}
.shin{
width: 53px;
height: 173px;
background-image: url("https://s3.postimg.org/3xecqews3/shin.png");
}
.foot{
width: 67px;
height: 34px;
background-image: url("https://s3.postimg.org/l0cj86o37/foot.png");
}
.toes{
width: 28px;
height: 25px;
background-image: url("https://s3.postimg.org/vm0zxxjsj/toes.png");
}
.right.arm{
top: 93px;
-webkit-animation-name: right-bicep;
}
.left.arm{
top: 87px;
-webkit-transform: rotate(-26deg);
}
.forearm{
top: 108px;
left: 14px;
-webkit-transform-origin: 3px 7px;
}
.leg{
left: 6px;
-webkit-transform-origin: 30px 20px;
-webkit-animation-name: thigh;
}
.right.leg{
top: 235px;
-webkit-animation-name: right-thigh;
}
.left.leg{
top:225px;
-webkit-animation-name: left-thigh;
}
.shin{
top: 115px;
-webkit-transform-origin: 30px 25px;
}
.right.shin {
-webkit-animation-name: right-shin;
}
.left.shin {
-webkit-animation-name: left-shin;
}
.foot{
top: 155px;
left: 2px;
-webkit-transform-origin: 0 50%;
}
.right.foot {
-webkit-animation-name: right-foot;
}
.left.foot {
-webkit-animation-name: left-foot;
}
.toes{
top: 9px;
left: 66px;
-webkit-transform-origin: 0% 100%;
}
.right.toes {
-webkit-animation-name: right-toes;
}
.left.toes {
-webkit-animation-name: left-toes;
}
div.right.arm { z-index: 1; }
div.left.arm { z-index: -3; }
div.arm > div.bicep > div.forearm { z-index: -1; }
div.right.leg { z-index: -1; }
div.left.leg { z-index: -2; }
div.leg > div.thigh > div.shin { z-index: -1; }
#-webkit-keyframes me {
0% { -webkit-transform: rotate(5deg) translate( 10px, 0px); }
25% { -webkit-transform: rotate(5deg) translate(-5px, -14px); }
50% { -webkit-transform: rotate(5deg) translate( 10px, 0px); }
75% { -webkit-transform: rotate(5deg) translate(-5px, -14px); }
100% { -webkit-transform: rotate(5deg) translate( 10px, 0px); }
}
#-webkit-keyframes right-bicep {
0% { -webkit-transform: rotate(26deg); }
50% { -webkit-transform: rotate(-20deg); }
100% { -webkit-transform: rotate(26deg); }
}
/*#-webkit-keyframes left-bicep {
0% { -webkit-transform: rotate(-20deg); }
50% { -webkit-transform: rotate(26deg); }
100% { -webkit-transform: rotate(-20deg); }
}*/
#-webkit-keyframes right-forearm {
0% { -webkit-transform: rotate(-10deg); }
50% { -webkit-transform: rotate(-65deg); }
100% { -webkit-transform: rotate(-10deg); }
}
/*#-webkit-keyframes left-forearm {
0% { -webkit-transform: rotate(-45deg); }
50% { -webkit-transform: rotate(-10deg); }
100% { -webkit-transform: rotate(-45deg); }
}*/
#-webkit-keyframes right-thigh {
0% { -webkit-transform: rotate(-45deg); }
50% { -webkit-transform: rotate(10deg); }
100% { -webkit-transform: rotate(-45deg); }
}
#-webkit-keyframes left-thigh {
0% { -webkit-transform: rotate(10deg); }
50% { -webkit-transform: rotate(-45deg); }
100% { -webkit-transform: rotate(10deg); }
}
#-webkit-keyframes right-shin {
0% { -webkit-transform: rotate(30deg); }
25% { -webkit-transform: rotate(20deg); }
50% { -webkit-transform: rotate(20deg); }
75% { -webkit-transform: rotate(85deg); }
100% { -webkit-transform: rotate(30deg); }
}
#-webkit-keyframes left-shin {
0% { -webkit-transform: rotate(20deg); }
25% { -webkit-transform: rotate(85deg); }
50% { -webkit-transform: rotate(30deg); }
75% { -webkit-transform: rotate(20deg); }
100% { -webkit-transform: rotate(20deg); }
}
#-webkit-keyframes right-foot {
0% { -webkit-transform: rotate(-5deg); }
25% { -webkit-transform: rotate(-7deg); }
50% { -webkit-transform: rotate(-16deg); }
75% { -webkit-transform: rotate(-10deg); }
100% { -webkit-transform: rotate(-5deg); }
}
#-webkit-keyframes left-foot {
0% { -webkit-transform: rotate(-16deg); }
25% { -webkit-transform: rotate(-10deg); }
50% { -webkit-transform: rotate(-5deg); }
75% { -webkit-transform: rotate(-7deg); }
100% { -webkit-transform: rotate(-16deg); }
}
#-webkit-keyframes right-toes {
0% { -webkit-transform: rotate(0deg); }
25% { -webkit-transform: rotate(-10deg); }
50% { -webkit-transform: rotate(-10deg); }
75% { -webkit-transform: rotate(-25deg); }
100% { -webkit-transform: rotate(0deg); }
}
#-webkit-keyframes left-toes {
0% { -webkit-transform: rotate(-10deg); }
25% { -webkit-transform: rotate(-25deg); }
50% { -webkit-transform: rotate(0deg); }
75% { -webkit-transform: rotate(-10deg); }
100% { -webkit-transform: rotate(-10deg); }
}
Please help me solve this issue. I looked into the other post about this problem but I am not able to correct it.
Thank you

You can read this one
z-index is canceled by setting transform(rotate)
basically, transforming an element using 'transform' which gives the element its own stacking context that is different than other element which is not transformed. What you can do is make all element to transform, for example:
<div class="a"><img src="..."></div>
<div class="b"><img src="..."></div>
you want div 'a' to scale(0.9) and be on top of 'b'. you can make the div 'b' to transform to translate(0,0) or scale(0) which won't make any different. Or you can just transform the content inside of it (for my example it's an image element) instead of the div that wrapping it. then just apply the z-index to div.

Since the transform property resets the stacking order of the elements, what i did is a wrapper div to the class "me" and apply transform scale property to the wrapper to reduce the size
.wrapper{
-webkit-transform: scale(0.4);
}
here is the link to the working fiddle https://jsfiddle.net/kq2soozp/5/

It's not the z-index that is getting messed up, it's the relative size of the inner divs in relation to the main .me div. You resize .me to 90% of its original size, and then resize all the divs inside as well, so they end up at 81% of their original sizes.
Solution: apply the scale only to the .me and not to the divs in it.
.wrapper {
position: relative;
}
.me,
.me div {
background-repeat: no-repeat;
position: absolute;
animation-duration: 2000ms;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
.me {
top: 80px;
left: 350px;
transform: scale(0.9); /* moved here */
}
.torso {
height: 274px;
width: 86px;
background-image: url("https://s9.postimg.org/41xfy5cin/torso.png");
}
.arm {
left: 12px;
transform-origin: 20px 10px;
}
.kite {
width: 395px;
height: 424px;
top: -115px;
left: 0px;
background-image: url("https://s3.postimg.org/ix240ioab/kite.png");
transform: rotate(45deg) scale(0.6);
}
.right.bicep {
width: 51px;
height: 124px;
background-image: url("https://s3.postimg.org/mlrszzyb7/right-bicep.png");
}
.left.bicep {
width: 52px;
height: 126px;
background-image: url("https://s3.postimg.org/jb3g048dv/left-bicep.png");
}
.left.forearm {
width: 37px;
height: 124px;
background-image: url("https://s3.postimg.org/7ahzze0z7/left-forearm.png");
transform: rotate(-45deg);
}
.right.forearm {
width: 36px;
height: 121px;
background-image: url("https://s3.postimg.org/q6noj82ur/right-forearm.png");
animation-name: right-forearm;
}
.left.thigh {
width: 69px;
height: 144px;
background-image: url("https://s3.postimg.org/577krq16b/left-thigh.png");
}
.right.thigh {
width: 69px;
height: 144px;
background-image: url("https://s3.postimg.org/72ud2vq0j/right-thigh.png");
}
.shin {
width: 53px;
height: 173px;
background-image: url("https://s3.postimg.org/3xecqews3/shin.png");
}
.foot {
width: 67px;
height: 34px;
background-image: url("https://s3.postimg.org/l0cj86o37/foot.png");
}
.toes {
width: 28px;
height: 25px;
background-image: url("https://s3.postimg.org/vm0zxxjsj/toes.png");
}
.right.arm {
top: 93px;
animation-name: right-bicep;
}
.left.arm {
top: 87px;
transform: rotate(-26deg);
}
.forearm {
top: 108px;
left: 14px;
transform-origin: 3px 7px;
}
.leg {
left: 6px;
transform-origin: 30px 20px;
animation-name: thigh;
}
.right.leg {
top: 235px;
animation-name: right-thigh;
}
.left.leg {
top: 225px;
animation-name: left-thigh;
}
.shin {
top: 115px;
transform-origin: 30px 25px;
}
.right.shin {
animation-name: right-shin;
}
.left.shin {
animation-name: left-shin;
}
.foot {
top: 155px;
left: 2px;
transform-origin: 0 50%;
}
.right.foot {
animation-name: right-foot;
}
.left.foot {
animation-name: left-foot;
}
.toes {
top: 9px;
left: 66px;
transform-origin: 0% 100%;
}
.right.toes {
animation-name: right-toes;
}
.left.toes {
animation-name: left-toes;
}
div.right.arm {
z-index: 1;
}
div.left.arm {
z-index: -3;
}
div.arm>div.bicep>div.forearm {
z-index: -1;
}
div.right.leg {
z-index: -1;
}
div.left.leg {
z-index: -2;
}
div.leg>div.thigh>div.shin {
z-index: -1;
}
#keyframes me {
0% {
transform: rotate(5deg) translate( 5px, 0px);
}
25% {
transform: rotate(5deg) translate(-5px, -14px);
}
50% {
transform: rotate(5deg) translate( 5px, 0px);
}
75% {
transform: rotate(5deg) translate(-5px, -14px);
}
100% {
transform: rotate(5deg) translate( 5px, 0px);
}
}
#keyframes right-bicep {
0% {
transform: rotate(26deg);
}
50% {
transform: rotate(-20deg);
}
100% {
transform: rotate(26deg);
}
}
/*#keyframes left-bicep {
0% { transform: rotate(-20deg); }
50% { transform: rotate(26deg); }
100% { transform: rotate(-20deg); }
}*/
#keyframes right-forearm {
0% {
transform: rotate(-10deg);
}
50% {
transform: rotate(-65deg);
}
100% {
transform: rotate(-10deg);
}
}
/*#keyframes left-forearm {
0% { transform: rotate(-45deg); }
50% { transform: rotate(-10deg); }
100% { transform: rotate(-45deg); }
}*/
#keyframes right-thigh {
0% {
transform: rotate(-45deg);
}
50% {
transform: rotate(10deg);
}
100% {
transform: rotate(-45deg);
}
}
#keyframes left-thigh {
0% {
transform: rotate(10deg);
}
50% {
transform: rotate(-45deg);
}
100% {
transform: rotate(10deg);
}
}
#keyframes right-shin {
0% {
transform: rotate(30deg);
}
25% {
transform: rotate(20deg);
}
50% {
transform: rotate(20deg);
}
75% {
transform: rotate(85deg);
}
100% {
transform: rotate(30deg);
}
}
#keyframes left-shin {
0% {
transform: rotate(20deg);
}
25% {
transform: rotate(85deg);
}
50% {
transform: rotate(30deg);
}
75% {
transform: rotate(20deg);
}
100% {
transform: rotate(20deg);
}
}
#keyframes right-foot {
0% {
transform: rotate(-5deg);
}
25% {
transform: rotate(-7deg);
}
50% {
transform: rotate(-16deg);
}
75% {
transform: rotate(-10deg);
}
100% {
transform: rotate(-5deg);
}
}
#keyframes left-foot {
0% {
transform: rotate(-16deg);
}
25% {
transform: rotate(-10deg);
}
50% {
transform: rotate(-5deg);
}
75% {
transform: rotate(-7deg);
}
100% {
transform: rotate(-16deg);
}
}
#keyframes right-toes {
0% {
transform: rotate(0deg);
}
25% {
transform: rotate(-10deg);
}
50% {
transform: rotate(-10deg);
}
75% {
transform: rotate(-25deg);
}
100% {
transform: rotate(0deg);
}
}
#keyframes left-toes {
0% {
transform: rotate(-10deg);
}
25% {
transform: rotate(-25deg);
}
50% {
transform: rotate(0deg);
}
75% {
transform: rotate(-10deg);
}
100% {
transform: rotate(-10deg);
}
}
<div class="wrapper">
<div class='me'>
<div class="torso">
<div class="left leg">
<div class="left thigh">
<div class="left shin">
<div class="left foot">
<div class="left toes"></div>
</div>
</div>
</div>
</div>
<div class="right leg">
<div class="right thigh">
<div class="right shin">
<div class="right foot">
<div class="right toes"></div>
</div>
</div>
</div>
</div>
<div class="left arm">
<div class="left bicep">
<div class="left forearm">
<div class="kite"></div>
</div>
</div>
</div>
<div class="right arm">
<div class="right bicep">
<div class="right forearm"></div>
</div>
</div>
</div>
</div>
</div>
By the way, some remarks:
// for a comment is an error in CSS. Don't do this. It happens to have no effect in your fiddle, but just look at this fiddle where it goes wrong.
You don't need the vendor prefixes during testing. Remove them and the code works in all browsers. If you want to be backwards compatible with older ones, fine, you can add in the prefixed properties (above the unprefixed ones), but do that after you're done twiddling with them.

Related

CSS phone ringng animation

I want to have my font awesome icon "ring" each 1 seconds, by ring I mean having a css animation which makes the phone look like if it where ringing (maybe a brief rotation with transform origin center?), I'm a noob with css animations:
<div style="width:50px; height:50px; border-radius:50%; display:flex; align-items:center; justify-content:center; background-color:white;"><i class="LAYOUTnav7_links_button_icon fa fa-phone fs_bigger main"></i></div>
Any help?
You can use animation and keyframes to do this, here is an example:
.phone {
display:block;
width: 60px;
height: 60px;
font-size: 40px;
margin:50px auto 0;
color: #9e9e9e;
animation: ring 4s .7s ease-in-out infinite;
transform-origin: 50% 4px;
}
#keyframes ring {
0% { transform: rotate(0); }
5% { transform: rotate(30deg); }
10% { transform: rotate(-28deg); }
15% { transform: rotate(34deg); }
20% { transform: rotate(-32deg); }
25% { transform: rotate(30deg); }
30% { transform: rotate(-28deg); }
35% { transform: rotate(26deg); }
40% { transform: rotate(-24deg); }
45% { transform: rotate(22deg); }
50% { transform: rotate(-20deg); }
55% { transform: rotate(18deg); }
60% { transform: rotate(-16deg); }
65% { transform: rotate(14deg); }
70% { transform: rotate(-12deg); }
75% { transform: rotate(10deg); }
80% { transform: rotate(-8deg); }
85% { transform: rotate(6deg); }
90% { transform: rotate(-4deg); }
95% { transform: rotate(2deg); }
100% { transform: rotate(-1deg); }
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay" crossorigin="anonymous">
<div>
<span class="phone fa fa-phone"></span>
</div>

Div gets clipped during transform animation

I currently have a problem developing an animation.
The point is that I have a big DIV within a smaller DIV that is absolutely positioned. The big DIV is initially hidden and should be displayed with a rotate-X animation when a certain action is performed.
However, during the rotate animation the DIV is cut off and is only displayed completely after the animation.
The same thing happens when the hide animation is played.
With Firefox the DIV is always clipped.
With Chrome the clipping only occurs while the animation ist playing.
The overflow: hidden of the .super-parend DIV is required unfortunately.
Does anyone know a work around for this problem?
$('#btnShow').on('click', function() {
$('.child').removeClass('hide');
$('.child').addClass('show');
});
$('#btnHide').on('click', function() {
$('.child').removeClass('show');
$('.child').addClass('hide');
});
.super-parent {
overflow: hidden;
height: 100px;
border: 1px dashed red;
}
.parent {
width: 100%;
border: 1px solid black;
overflow: visible;
perspective: 800px;
perspective-origin: 50% 50%;
}
.static-child {
text-align: center;
vertical-align: middle;
}
.child {
position: absolute;
border: 1px solid green;
top: 0;
opacity: 0;
left: 50%;
transform: translateX(-50%);
transform-origin: top;
background: white;
width: 100px;
height: 200px;
text-align: center;
}
.child.show {
animation: swing-in-top-fwd 0.75s cubic-bezier(0.18, 0.89, 0.41, 1.01) both;
}
.child.hide {
animation: swing-out-top-bck 0.75s cubic-bezier(0.18, 0.89, 0.41, 1.01) both;
}
input {
margin-top: 50px;
}
#-webkit-keyframes swing-in-top-fwd {
0% {
-webkit-transform: rotateX(-100deg) translateX(-50%);
transform: rotateX(-100deg) translateX(-50%);
opacity: 0
}
100% {
-webkit-transform: rotateX(0deg) translateX(-50%);
transform: rotateX(0deg) translateX(-50%);
opacity: 1
}
}
#keyframes swing-in-top-fwd {
0% {
-webkit-transform: rotateX(-100deg) translateX(-50%);
transform: rotateX(-100deg) translateX(-50%);
opacity: 0
}
100% {
-webkit-transform: rotateX(0deg) translateX(-50%);
transform: rotateX(0deg) translateX(-50%);
opacity: 1
}
}
#-webkit-keyframes swing-out-top-bck {
0% {
-webkit-transform: rotateX(0deg) translateX(-50%);
transform: rotateX(0deg) translateX(-50%);
opacity: 1
}
100% {
-webkit-transform: rotateX(-100deg) translateX(-50%);
transform: rotateX(-100deg) translateX(-50%);
opacity: 0
}
}
#keyframes swing-out-top-bck {
0% {
-webkit-transform: rotateX(0deg) translateX(-50%);
transform: rotateX(0deg) translateX(-50%);
opacity: 1
}
100% {
-webkit-transform: rotateX(-100deg) translateX(-50%);
transform: rotateX(-100deg) translateX(-50%);
opacity: 0
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="super-parent">
<div class="parent">
<div class="static-child">
01.01.2018
</div>
<div class="child hide">
test
</div>
</div>
</div>
<input type="button" id="btnShow" value="show">
<input type="button" id="btnHide" value="hide">
I think I found the solution myself:
The perspective: 800px in the .parent div seems to be responsible for the clipping.
When I move the perspective property to the body, the div is fully shown.
$('#btnShow').on('click', function() {
$('.child').removeClass('hide');
$('.child').addClass('show');
});
$('#btnHide').on('click', function() {
$('.child').removeClass('show');
$('.child').addClass('hide');
});
body {
perspective: 800px;
}
.super-parent {
overflow: hidden;
height: 100px;
border: 1px dashed red;
}
.parent {
width: 100%;
border: 1px solid black;
overflow: visible;
/* perspective: 800px; */
perspective-origin: 50% 50%;
}
.static-child {
text-align: center;
vertical-align: middle;
}
.child {
position: absolute;
border: 1px solid green;
top: 0;
opacity: 0;
left: 50%;
transform: translateX(-50%);
transform-origin: top;
background: white;
width: 100px;
height: 200px;
text-align: center;
}
.child.show {
animation: swing-in-top-fwd 0.75s cubic-bezier(0.18, 0.89, 0.41, 1.01) both;
}
.child.hide {
animation: swing-out-top-bck 0.75s cubic-bezier(0.18, 0.89, 0.41, 1.01) both;
}
input {
margin-top: 50px;
}
#-webkit-keyframes swing-in-top-fwd {
0% {
-webkit-transform: rotateX(-100deg) translateX(-50%);
transform: rotateX(-100deg) translateX(-50%);
opacity: 0
}
100% {
-webkit-transform: rotateX(-1deg) translateX(-50%);
transform: rotateX(-1deg) translateX(-50%);
opacity: 1
}
}
#keyframes swing-in-top-fwd {
0% {
-webkit-transform: rotateX(-100deg) translateX(-50%);
transform: rotateX(-100deg) translateX(-50%);
opacity: 0
}
100% {
-webkit-transform: rotateX(-1deg) translateX(-50%);
transform: rotateX(-1deg) translateX(-50%);
opacity: 1
}
}
#-webkit-keyframes swing-out-top-bck {
0% {
-webkit-transform: rotateX(0deg) translateX(-50%);
transform: rotateX(0deg) translateX(-50%);
opacity: 1
}
100% {
-webkit-transform: rotateX(-100deg) translateX(-50%);
transform: rotateX(-100deg) translateX(-50%);
opacity: 0
}
}
#keyframes swing-out-top-bck {
0% {
-webkit-transform: rotateX(0deg) translateX(-50%);
transform: rotateX(0deg) translateX(-50%);
opacity: 1
}
100% {
-webkit-transform: rotateX(-100deg) translateX(-50%);
transform: rotateX(-100deg) translateX(-50%);
opacity: 0
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="super-parent">
<div class="parent">
<div class="static-child">
01.01.2018
</div>
<div class="child hide">
test
</div>
</div>
</div>
<input type="button" id="btnShow" value="show">
<input type="button" id="btnHide" value="hide">
Are you allowed to change the html structure? Seems everything works if: 1) - div.parent has absolute position and no more children and 2) - there is a super parent div with relative position.
$('#btnShow').on('click', function() {
$('.child').removeClass('hide');
$('.child').addClass('show');
});
$('#btnHide').on('click', function() {
$('.child').removeClass('show');
$('.child').addClass('hide');
});
.super-parent {
overflow: hidden;
height: 100px;
border: 1px dashed red;
}
.super-super-parent {
position: relative;
}
.parent {
width: 100%;
border: 1px solid black;
overflow: visible;
perspective: 800px;
perspective-origin: 50% 50%;
position: absolute;
}
.static-child {
text-align: center;
vertical-align: middle;
}
.child {
position: absolute;
border: 1px solid green;
top: 0;
opacity: 0;
left: 50%;
transform: translateX(-50%);
transform-origin: top;
background: white;
width: 100px;
height: 200px;
text-align: center;
}
.child.show {
animation: swing-in-top-fwd 0.75s cubic-bezier(0.18, 0.89, 0.41, 1.01) both;
}
.child.hide {
animation: swing-out-top-bck 0.75s cubic-bezier(0.18, 0.89, 0.41, 1.01) both;
}
input {
margin-top: 50px;
}
#-webkit-keyframes swing-in-top-fwd {
0% {
-webkit-transform: rotateX(-100deg) translateX(-50%);
transform: rotateX(-100deg) translateX(-50%);
opacity: 0
}
100% {
-webkit-transform: rotateX(0deg) translateX(-50%);
transform: rotateX(0deg) translateX(-50%);
opacity: 1
}
}
#keyframes swing-in-top-fwd {
0% {
-webkit-transform: rotateX(-100deg) translateX(-50%);
transform: rotateX(-100deg) translateX(-50%);
opacity: 0
}
100% {
-webkit-transform: rotateX(0deg) translateX(-50%);
transform: rotateX(0deg) translateX(-50%);
opacity: 1
}
}
#-webkit-keyframes swing-out-top-bck {
0% {
-webkit-transform: rotateX(0deg) translateX(-50%);
transform: rotateX(0deg) translateX(-50%);
opacity: 1
}
100% {
-webkit-transform: rotateX(-100deg) translateX(-50%);
transform: rotateX(-100deg) translateX(-50%);
opacity: 0
}
}
#keyframes swing-out-top-bck {
0% {
-webkit-transform: rotateX(0deg) translateX(-50%);
transform: rotateX(0deg) translateX(-50%);
opacity: 1
}
100% {
-webkit-transform: rotateX(-100deg) translateX(-50%);
transform: rotateX(-100deg) translateX(-50%);
opacity: 0
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="super-super-parent">
<div class="super-parent">
<div class="parent">
<div class="static-child">
01.01.2018
</div>
<div class="child hide">
test
</div>
</div>
<div class="one-more-child">
<p>test text</p>
<p>test text</p>
<p>test text</p>
<p>test text</p>
</div>
</div>
</div>
<input type="button" id="btnShow" value="show">
<input type="button" id="btnHide" value="hide">

rotate around object smoothly in CSS

I have two images moving around a DOM element. But their rotation is wrong and the movement is not smooth.
My fiddle
#mainPage {
width: 400px;
height: 165px;
margin: 10% auto;
}
#mainPage>p {
text-align: center;
}
.bicycle {
width: 48px;
height: 30px;
background: red;
}
#bicycleOriginal {
animation: moveBicycleOriginal 20s infinite;
}
#bicycleFlipped {
animation: moveBicycleFlipped 20s infinite;
}
#mainTxt {
letter-spacing: 7px;
font-size: 30px;
margin-bottom: 30px;
}
#keyframes moveBicycleOriginal {
from {
transform: translate(0, 0);
}
1% {
transform: translate(0, 0) rotate(0deg);
}
25% {
transform: translate(350px, 0);
}
26% {
transform: translate(350px, 0) rotate(90deg);
}
50% {
transform: translate(350px, 150px);
}
51% {
transform: translate(350px, 150px) rotate(180deg);
}
75% {
transform: translate(0, 150px);
}
76% {
transform: translate(0, 150px) rotate(270deg);
}
to {
transform: translate(0, 0);
}
}
#keyframes moveBicycleFlipped {
from {
transform: translate(350px, 0);
}
1% {
transform: translate(350px, 0) rotate(0deg);
}
25% {
transform: translate(0, 0);
}
26% {
transform: translate(0, 0) rotate(90deg);
}
50% {
transform: translate(0, -150px);
}
51% {
transform: translate(0, -150px) rotate(180deg);
}
75% {
transform: translate(350px, -150px);
}
76% {
transform: translate(350px, -150px) rotate(270deg);
}
to {
transform: translate(350px, 0);
}
}
<div id="mainPage">
<div class="bicycle" id="bicycleOriginal"></div>
<p class="txt" id="mainTxt">DRAHTESEL</p>
<div class="bicycle" id="bicycleFlipped"></div>
</div>
So what I want is something like this
After passing the first keyframe the boxes get into a wrong rotation. Also they don't move smoothly, the get faster in the middle and slower when reaching the end.
Could someone help me with the keyframes?
You need to always keep the rotate defined within the transform because each transform will override the previous one and removing the rotation means rotate(0).
And to make the animation more accurate, the last state should be similar to the first state to avoid the jump when restarting the animation. So you should go to 360deg of rotation which is equivalent to 0deg. (like you did with the translation where you come back to the initial value).
Then you may adjust the animation-timing-function to control the animation progress if needed.
#mainPage {
width: 400px;
height: 165px;
margin: 10% auto;
}
#mainPage>p {
text-align: center;
}
.bicycle {
width: 48px;
height: 30px;
background: red;
}
#bicycleOriginal {
animation: moveBicycleOriginal 20s infinite;
}
#bicycleFlipped {
animation: moveBicycleFlipped 20s infinite;
}
#mainTxt {
letter-spacing: 7px;
font-size: 30px;
margin-bottom: 30px;
}
#keyframes moveBicycleOriginal {
from {
transform: translate(0, 0) rotate(0deg);
}
25% {
transform: translate(350px, 0) rotate(0deg);
}
26% {
transform: translate(350px, 0) rotate(90deg);
}
50% {
transform: translate(350px, 150px) rotate(90deg);
}
51% {
transform: translate(350px, 150px) rotate(180deg);
}
75% {
transform: translate(0, 150px) rotate(180deg);
}
76% {
transform: translate(0, 150px) rotate(270deg);
}
98% {
transform: translate(0, 0) rotate(270deg);
}
to {
transform: translate(0, 0) rotate(360deg);
}
}
#keyframes moveBicycleFlipped {
from {
transform: translate(350px, 0) rotate(0deg);
}
25% {
transform: translate(0, 0) rotate(0deg);
}
26% {
transform: translate(0, 0) rotate(90deg);
}
50% {
transform: translate(0, -150px) rotate(90deg);
}
51% {
transform: translate(0, -150px) rotate(180deg);
}
75% {
transform: translate(350px, -150px) rotate(180deg);
}
76% {
transform: translate(350px, -150px) rotate(270deg);
}
97% {
transform: translate(350px, 0) rotate(270deg);
}
to {
transform: translate(350px, 0) rotate(360deg);
}
}
<div id="mainPage">
<div class="bicycle" id="bicycleOriginal"></div>
<p class="txt" id="mainTxt">DRAHTESEL</p>
<div class="bicycle" id="bicycleFlipped"></div>
</div>
The speed slowing/speeding is because of the animationTiming (default ease), that should be 'linear'.
The reason the animation is incorrect, it because you unset the rotation. This might come unexpected, you control transformation with css transform. You also control rotation with transform.
#example{
transform: rotate(10deg)
}
#example.changed{
transform: translateX(100px);
}
In this snippet, when you add the class changed you redefine transform, telling it to forget rotate and set translateX. In this example, to keep them both:
#example.changed{
transform: rotate(10deg) translateX(100px);
}
I have taken the answer from Temani Afif, and changed it a little bit.
I set an aditional transform, applied after the rotation. This makes the turn be smooth in the sense that the object rotates following a path and not in the same place
I have set the timing to linear, as suggested by Martijn
I have simplified it to use a single keyframes rule, setting a delay on the flipped div.
And made longer the time slice for the long sides and shoprter the other, so that the perceived speed is more constant
The result:
#mainPage {
width: 400px;
height: 165px;
margin: 10% auto;
}
#mainPage>p {
text-align: center;
}
.bicycle {
width: 48px;
height: 30px;
background: red;
}
#bicycleOriginal {
animation: moveBicycleOriginal 20s infinite linear;
}
#bicycleFlipped {
position: relative;
top: -120px;
animation: moveBicycleOriginal 20s -10s infinite linear;
}
#mainTxt {
letter-spacing: 7px;
font-size: 30px;
margin-bottom: 30px;
}
#keyframes moveBicycleOriginal {
from {
transform: translate(0, 0) rotate(0deg) translate(0, -50px);
}
26% {
transform: translate(350px, 0) rotate(0deg) translate(0, -50px);
}
30% {
transform: translate(350px, 0) rotate(90deg) translate(0, -50px);
}
46% {
transform: translate(350px, 150px) rotate(90deg) translate(0, -50px);
}
50% {
transform: translate(350px, 150px) rotate(180deg) translate(0, -50px);
}
76% {
transform: translate(0, 150px) rotate(180deg) translate(0, -50px);
}
80% {
transform: translate(0, 150px) rotate(270deg) translate(0, -50px);
}
96% {
transform: translate(0, 0) rotate(270deg) translate(0, -50px);
}
to {
transform: translate(0, 0) rotate(360deg) translate(0, -50px);
}
}
<div id="mainPage">
<div class="bicycle" id="bicycleOriginal"></div>
<p class="txt" id="mainTxt">DRAHTESEL</p>
<div class="bicycle" id="bicycleFlipped"></div>
</div>

Border not lining up for a circle

I am running into an issue in my mobile media query - anything under a 640px viewport. I have a circle that comes together and forms a full circle (see snippet), but for some reason in my media query, the circle doesn't quite line up, and I am unsure why as I am using the same math that makes it work in a desktop version.
Here is what it looks like within the 640 media query:
So how this works is I give .circle the same height and width. So let's say 200px for both height and width.
Then the class of .spinner, I divide the height and width of the .circle by two. So I would have 125px for height and width.
Then I set the border size, so lets use 5px. What I do is add that border size to the height and width numbers of .spinner and use that figure, which would be 130px to everything else ranging from .top, .bottom, q2, mask, etc.
That is how I get this to work and my math in my media query is not wrong. Does anyone see why this isn't lining up?
.blue {
background-color: blue;
width: 100%;
height: 600px;
}
.circle {
z-index: 99;
width: 500px;
height: 500px;
position: absolute;
background: inherit;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%) rotate(0deg);
transform: translate(-50%, -50%) rotate(0deg);
}
.spinner {
width: 250px;
height: 250px;
position: absolute;
border: 5px solid #b5f2ff;
z-index: 10;
}
.top {
top: 255px;
left: 255px;
border-radius: 0 0 255px 0;
border-left: none;
border-top: none;
-webkit-transform-origin: top left;
transform-origin: top left;
}
.bottom {
border-radius: 255px 0 0 0;
border-bottom: none;
border-right: none;
-webkit-transform-origin: bottom right;
transform-origin: bottom right;
}
.topright,
.bottomleft {
-webkit-animation: rotate90 4s linear forwards;
animation: rotate90 4s linear forwards;
}
.topleft,
.bottomright {
-webkit-animation: rotate180 4s linear forwards;
animation: rotate180 4s linear forwards;
}
.mask {
width: 255px;
height: 255px;
position: absolute;
opacity: 1;
background: inherit;
z-index: 15;
-webkit-animation: mask 4s linear forwards;
animation: mask 4s linear forwards;
}
.q2 {
top: 0;
left: 0;
}
.q4 {
top: 255px;
left: 255px;
}
#-webkit-keyframes rotate90 {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
20%,
80% {
-webkit-transform: rotate(-90deg);
transform: rotate(-90deg);
}
100% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
}
#keyframes rotate90 {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
20%,
80% {
-webkit-transform: rotate(-90deg);
transform: rotate(-90deg);
}
100% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
}
#-webkit-keyframes rotate180 {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
40%,
60% {
-webkit-transform: rotate(-180deg);
transform: rotate(-180deg);
}
100% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
}
#keyframes rotate180 {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
40%,
60% {
-webkit-transform: rotate(-180deg);
transform: rotate(-180deg);
}
100% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
}
#-webkit-keyframes mask {
0% {
z-index: 15
}
40%,
60% {
z-index: 4
}
100% {
z-index: 15
}
}
#keyframes mask {
0% {
z-index: 15
}
40%,
60% {
z-index: 4
}
100% {
z-index: 15
}
}
#circle-text {
display: none;
position: absolute;
color: #FFF;
font-size: 2.3em;
text-align: center;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
z-index: 100;
}
#media screen and (max-width:640px) {
.circle {
z-index: 100;
width: 250px;
height: 250px;
-webkit-transform: translate(-50%, -50%) rotate(0deg);
transform: translate(-50%, -50%) rotate(0deg);
}
.spinner {
width: 125px;
height: 125px;
z-index: 10;
}
.top {
top: 130px;
left: 130px;
border-radius: 0 0 130px 0;
}
.bottom {
border-radius: 130px 0 0 0;
}
.mask {
width: 130px;
height: 130px;
}
.q4 {
top: 130px;
left: 130px;
}
}
<div class="blue">
<div class="circle">
<div class="spinner top topright"></div>
<div class="spinner top topleft"></div>
<div class="spinner bottom bottomleft"></div>
<div class="spinner bottom bottomright"></div>
<div class="mask q2"></div>
<div class="mask q4"></div>
</div>
</div>
You have an inconsistent use of box-sizing:border-box in your CSS. It's being used in media queries, so that it doesn't apply to all screen sizes. And it would mess up your calculations.

Pulsing Heart CSS animation

I`m working on an animated heart only with CSS.
I want it to pulse 2 times, take a small break, and then repeat it again.
What I have now:
small ==> big ==> small ==> repeat animation
What I'm going for:
small ==> big ==> small ==> big ==> small ==> pause ==> repeat animation
How can I do it?
My code :
#button{
width:450px;
height:450px;
position:relative;
top:48px;
margin:0 auto;
text-align:center;
}
#heart img{
position:absolute;
left:0;
right:0;
margin:0 auto;
-webkit-transition: opacity 7s ease-in-out;
-moz-transition: opacity 7s ease-in-out;
-o-transition: opacity 7s ease-in-out;
transition: opacity 7s ease-in-out;}
#keyframes heartFadeInOut {
0% {
opacity:1;
}
14% {
opacity:1;
}
28% {
opacity:0;
}
42% {
opacity:0;
}
70% {
opacity:0;
}
}
#heart img.top {
animation-name: heartFadeInOut;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
animation-duration: 1s;
animation-direction: alternate;
}
<div id="heart" >
<img class="bottom" src="https://goo.gl/nN8Haf" width="100px">
<img class="top" src="https://goo.gl/IIW1KE" width="100px">
</div>
See also this Fiddle.
You can incorporate the pause into the animation. Like so:
#keyframes heartbeat
{
0%
{
transform: scale( .75 );
}
20%
{
transform: scale( 1 );
}
40%
{
transform: scale( .75 );
}
60%
{
transform: scale( 1 );
}
80%
{
transform: scale( .75 );
}
100%
{
transform: scale( .75 );
}
}
Working example:
https://jsfiddle.net/t7f97kf4/
#keyframes heartbeat
{
0%
{
transform: scale( .75 );
}
20%
{
transform: scale( 1 );
}
40%
{
transform: scale( .75 );
}
60%
{
transform: scale( 1 );
}
80%
{
transform: scale( .75 );
}
100%
{
transform: scale( .75 );
}
}
div
{
background-color: red;
width: 50px;
height: 50px;
animation: heartbeat 1s infinite;
}
<div>
Heart
</div>
Edit:
Working example with pure CSS heart shape:
https://jsfiddle.net/qLfg2mrd/
#keyframes heartbeat
{
0%
{
transform: scale( .75);
}
20%
{
transform: scale( 1);
}
40%
{
transform: scale( .75);
}
60%
{
transform: scale( 1);
}
80% {
transform: scale( .75);
}
100%
{
transform: scale( .75);
}
}
#heart
{
position: relative;
width: 100px;
height: 90px;
animation: heartbeat 1s infinite;
}
#heart:before,
#heart:after
{
position: absolute;
content: "";
left: 50px;
top: 0;
width: 50px;
height: 80px;
background: red;
-moz-border-radius: 50px 50px 0 0;
border-radius: 50px 50px 0 0;
-webkit-transform: rotate(-45deg);
-moz-transform: rotate(-45deg);
-ms-transform: rotate(-45deg);
-o-transform: rotate(-45deg);
transform: rotate(-45deg);
-webkit-transform-origin: 0 100%;
-moz-transform-origin: 0 100%;
-ms-transform-origin: 0 100%;
-o-transform-origin: 0 100%;
transform-origin: 0 100%;
}
#heart:after
{
left: 0;
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
-ms-transform: rotate(45deg);
-o-transform: rotate(45deg);
transform: rotate(45deg);
-webkit-transform-origin: 100% 100%;
-moz-transform-origin: 100% 100%;
-ms-transform-origin: 100% 100%;
-o-transform-origin: 100% 100%;
transform-origin: 100% 100%;
}
<div id="heart"></div>
Pulse 2 times, take a small break, and then repeat it again
Try this. Going with animation opacity is a bad choice. transform: scale() will do the job.
.heart:before {
display: block;
position: absolute;
top: 0;
left: 0;
width: 100%;
font-family: 'icons';
font-size: 21px;
text-indent: 0;
font-variant: normal;
line-height: 21px;
}
.heart {
position: relative;
width: 500px;
overflow: inherit;
margin: 50px auto;
list-style: none;
-webkit-animation: animateHeart 2.5s infinite;
animation: animateHeart 2.5s infinite;
}
.heart:before,
.heart:after {
position: absolute;
content: '';
top: 0;
left: 50%;
width: 120px;
height: 200px;
background: red;
border-radius: 100px 100px 0 0;
-webkit-transform: rotate(-45deg) translateZ(0);
transform: rotate(-45deg) translateZ(0);
-webkit-transform-origin: 0 100%;
transform-origin: 0 100%;
}
.heart:after {
left: 26%;
-webkit-transform: rotate(45deg) translateZ(0);
transform: rotate(45deg) translateZ(0);
-webkit-transform-origin: 100% 100%;
transform-origin: 100% 100%;
}
#-webkit-keyframes animateHeart {
0% {
-webkit-transform: scale(0.8);
}
5% {
-webkit-transform: scale(0.9);
}
10% {
-webkit-transform: scale(0.8);
}
15% {
-webkit-transform: scale(1);
}
50% {
-webkit-transform: scale(0.8);
}
100% {
-webkit-transform: scale(0.8);
}
}
#keyframes animateHeart {
0% {
transform: scale(0.8);
}
5% {
transform: scale(0.9);
}
10% {
transform: scale(0.8);
}
15% {
transform: scale(1);
}
50% {
transform: scale(0.8);
}
100% {
transform: scale(0.8);
}
}
span {
font-family: 'Cantora One', sans-serif;
font-size: 64px;
position: absolute;
top: 165px;
}
<div class="heart">
</div>
I like ketan's answer, but I wanted to improve the heart animation to make it more realistic.
A heart does not double in size when it beats. 10% change in size looks better to me.
I like it getting both larger and smaller
When it stops moving altogether it looks dead to me. Even when it isn't beating, it needs to expand or contract a little
I removed the "alternate directions" code so that it runs the same way through every time
I explicitly have the heart start end and at normal scale (1) and have the animation in the middle of the sequence. It seems clearer that way to me.
#heart img{
position:absolute;
left:0;
right:0;
margin:0 auto;
}
#keyframes heartFadeInOut {
0% {transform: scale(1);}
25% {transform: scale(.97);}
35% {transform: scale(.9);}
45% {transform: scale(1.1);}
55% {transform: scale(.9);}
65% {transform: scale(1.1);}
75% {transform: scale(1.03);}
100% {transform: scale(1);}
}
#heart img.bottom {
animation-name: heartFadeInOut;
animation-iteration-count: infinite;
animation-duration: 2s;
}
<div id="heart" >
<img class="bottom" src="https://i.stack.imgur.com/iBCpb.png" width="100px">
</div>
Based on various comments and making use of the ♥ we'll get this:
body {
font-size: 40pt;
color: red;
}
#keyframes heartbeat {
0% {
font-size: .75em;
}
20% {
font-size: 1em;
}
40% {
font-size: .75em;
}
60% {
font-size: 1em;
}
80% {
font-size: .75em;
}
100% {
font-size: .75em;
}
}
div {
animation: heartbeat 1s infinite;
}
<div>
♥
</div>
body{
margin: 0;
padding: 0;
background: #1f1f1f;
}
body:before
{
position: absolute;
content: '';
left: 50%;
width: 50%;
height: 100%;
background: rgba(0,0,0,.2);
}
.center
{
position: absolute;
top:50%;
left: 50%;
background: #1f1f1f;
transform: translate(-50%,-50%);
padding: 100px;
border: 5px solid white;
border-radius: 100%;
box-shadow:20px 20px 45px rgba(0,0,0,.4);
z-index: 1;
overflow: hidden;
}
.heart
{
position: relative;
width: 100px;
height: 100px;
background:#ff0036;
transform: rotate(45deg) translate(10px,10px);
animation: ani 1s linear infinite;
}
.heart:before
{
content: '';
width: 100%;
height: 100%;
background: #ff0036;
position: absolute;
top:-50%;
left:0;
border-radius: 50%;
}
.heart:after
{
content:'';
width: 100%;
height: 100%;
background: #ff0036;
position: absolute;
bottom:0;
right:50%;
border-radius: 50%;
}
.center:before
{
content: '';
position: absolute;
top:0;
left:-50%;
width: 100%;
height: 100%;
background: rgba(0,0,0,.3);
}
#keyframes ani{
0%{
transform: rotate(45deg) translate(10px,10px) scale(1);
}
25%{
transform: rotate(45deg) translate(10px,10px) scale(1);
}
30%{
transform: rotate(45deg) translate(10px,10px) scale(1.4);
}
50%{
transform: rotate(45deg) translate(10px,10px) scale(1.2);
}
70%{
transform: rotate(45deg) translate(10px,10px) scale(1.4);
}
90%{
transform: rotate(45deg) translate(10px,10px) scale(1);
}
100%{
transform: rotate(45deg) translate(10px,10px) scale(1);
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HeartBeat Animation</title>
<link rel="stylesheet" href="Style.css" type="text/css">
</head>
<body>
<div class="center">
<div class="heart">
</div>
</div>
</body>
</html>
Output
for more: Heart Beating Animation
I think this is what you want for your image animation. There is no need of top image. Just use bottom.
#button{
width:450px;
height:450px;
position:relative;
top:48px;
margin:0 auto;
text-align:center;
}
#heart img{
position:absolute;
left:0;
right:0;
margin:0 auto;
}
#keyframes heartFadeInOut {
0%
{ transform: scale( .5 ); }
20%
{ transform: scale( 1 ); }
40%
{ transform: scale( .5 ); }
60%
{ transform: scale( 1 ); }
80%
{ transform: scale( .5 ); }
100%
{ transform: scale( .5 ); }
}
#heart img.bottom {
animation-name: heartFadeInOut;
animation-iteration-count: infinite;
animation-duration: 1.5s;
animation-direction: alternate;
}
<div id="heart" >
<img class="bottom" src="https://goo.gl/nN8Haf" width="100px">
</div>
I needed this for a project I was working on. I was trying to make it look as realistic as possible, and this is what I came up with.
#keyframes heartbeat {
0% {
transform: scale( .95 );
}
20% {
transform: scale( .97 );
}
30% {
transform: scale( .95 );
}
40% {
transform: scale( 1 );
}
100% {
transform: scale( .95 );
}
}
animation: heartbeat 1s infinite;