What is the simplest CSS for animating two elements to overlay each other when their parent is hovered on?
Here are two attempts I have done, each with separate issues.
https://codepen.io/ndullea/pen/RwNaXdJ
In this one they will both move, but it is not smooth because they have different parents and the elements are also misaligned because of that.
HTML:
<div class="container">
<div class="bar"/>
</div>
<div class="container_two">
<div class="bar_two"/>
</div>
CSS:
.container {
height: 100vh;
width: 100vw;
}
.bar {
height: 100px;
background-color: #29cf7c;
width: 10px;
}
.bar_two {
height: 150px;
background-color: #eb4034;
width: 10px;
margin-left: 200px;
z-index: 1;
}
.container:hover > .bar {
transform: translateX(100px);
transition: all .5s linear;
}
.container:not(:hover) > .bar {
transform: translate(0px, 0px);
transition: all .5s linear;
}
.container:hover > .bar_two {
transform: translateX(-100px);
transition: all .5s linear;
}
.container:not(:hover) > .bar_two {
transform: translate(0px, 0px);
transition: all .5s linear;
}
https://codepen.io/ndullea/pen/wvBWwML
This one is smooth, but they both animate they same way (instead of going towards each other), I think because of the margin property.
HTML:
<div class="container">
<div class="bar"/>
<div class="bar_two"/>
</div>
CSS:
.container {
height: 100vh;
width: 100vw;
}
.bar {
height: 100px;
background-color: #29cf7c;
width: 10px;
}
.bar_two {
height: 150px;
background-color: #eb4034;
width: 10px;
margin-left: 200px;
z-index: 1;
}
.container:hover > .bar {
transform: translateX(100px);
transition: all .5s linear;
}
.container:not(:hover) > .bar {
transform: translate(0px, 0px);
transition: all .5s linear;
}
.container:hover > .bar_two {
transform: translateX(-100px);
transition: all .5s linear;
}
.container:not(:hover) > .bar_two {
transform: translate(0px, 0px);
transition: all .5s linear;
}
I have edited your second fiddle with some improvements: https://jsfiddle.net/v1m0rsfj/11/
HTML
<div class="container">
<div class="bar"></div>
<div class="bar_two"></div>
</div>
CSS
.container {
height: 100vh;
width: 100vw;
border: 2px solid #000;
}
.bar,
.bar_two {
position: absolute;
height: 100px;
background-color: #29cf7c;
width: 10px;
}
.bar {
}
.bar_two {
left: 200px;
background-color: #eb4034;
z-index: 1;
}
.container:hover > .bar {
transform: translateX(100px);
transition: all .5s linear;
}
.container:not(:hover) > .bar {
transform: translate(0px, 0px);
transition: all .5s linear;
}
.container:hover > .bar_two {
transform: translateX(-100px);
transition: all .5s linear;
}
.container:not(:hover) > .bar_two {
transform: translate(0px, 0px);
transition: all .5s linear;
}
I found the biggest issue to be how you were closing your div tags <div /> isn't the right way to close a div tag please use <div></div>
Also I found margin-left to be mis-positioning your element hence the changes to your css
I want when the component is open to make an animation or to make a fade.
This is the code
This is the HTML
<div *ngIf="showMePartially" class="container">
<div class="body-container">
</div>
</div>
This is the CSS
.container {
position: fixed;
z-index: 1;
padding-top: 100px;
transition: opacity 0.5s ease-in;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0, 0, 0, 0.4);
transition: 3s;
}
.body-container {
width: calc(100% - 73em);
position: relative;
left: 70em;
top: 7.5em;
background: white;
height: calc(100% - 18em);
border-radius: 4px;
padding-left: 11px;
transition: opacity 0.5s ease-in;
animation-direction: normal;
}
You can use css animation:
.body-container {
/* above styles */
animation: animation-name .3s ease-out forwards;
}
#keyframes animation-name {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
You can make your own animation inside of keyframes. This should work, because *ngIf will either remove element from dom, if condition is false, or append it into dom(this is when animation should work).
EDIT
For left to right animation you can do like:
#keyframes animation-name {
0% {
transform: translateX(100%);
}
100% {
transform: translateX(0%);
}
}
I want to move the block to the left along its length.
How I can do this?
simple example, pure css :
.mytext {
border: 8px black solid;
text-align: center;
font-size: 45px;
left: 300px;
width: inherit;
height: 60px;
transition: 2s;
-webkit-transition: 2s;
-moz-transition: 2s;
position: absolute;
}
.mytext:hover {
left: 0;
}
.mytext2 {
margin-top: 100px;
border: 8px black solid;
text-align: center;
font-size: 45px;
width: 300px;
height: 60px;
transition: 2s;
-webkit-transition: 2s;
-moz-transition: 2s;
position: absolute;
}
#keyframes slideToLeft {
0% {
transform: translateX(100%);
}
100% {
transform: translateX(0);
}
}
.mytext2 {
/* This section calls the slideInFromLeft animation we defined above */
animation: 2s ease-out 0s 1 slideToLeft;
}
<div class="mytext">text</div>
<div class="mytext2">text2</div>
I want to rotate the inner circle when hover over outer circle, its works fine if I don't apply animation to it. But if I apply animation, inner circle does not rotate on hover. My animation makes inner circle rotate once only.
body, html {
height: 100%;
}
.main-content {
position: relative;
height: 100%;
}
.box-container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.box-container ul {
list-style: none;
}
.box-container .box {
display: inline-block;
height: 100px;
width: 100px;
background: pink;
border-radius: 50%;
text-align: center;
position: relative;
cursor: pointer;
}
.box-container .box span {
transform-origin: 0% 0%;
position: absolute;
top: 50%;
left: 50%;
border-radius: 50%;
margin-top: -20px;
margin-left: -20px;
display: block;
width: 40px;
height: 40px;
background: #58C9B9;
color: #fff;
line-height: 40px;
text-align: center;
transform-origin: calc(100% - 20px) calc(100% - 20px);
}
.box-container .box:hover span {
background: #4F86C6;
transform: rotateY(360deg);
transition: .5s;
}
.animate1 {
animation: animate .5s ease-in-out forwards;
}
.animate2 {
animation: animate .5s ease-in-out .5s forwards;
}
.animate3 {
animation: animate .5s ease-in-out 1.0s forwards;
}
#keyframes animate {
0% {
opacity: 0;
transform: rotateY(0deg);
}
100% {
opacity: 1;
transform: rotateY(360deg);
}
}
<div class="main-content">
<div class="box-container">
<ul class="list-unstyle list-inline">
<li class="box "><span class="animate1">20%</span></li>
<li class="box "><span class="animate2">40%</span></li>
<li class="box "><span class="animate3">50%</span></li>
</ul>
</div>
</div>
<div>
Remove property forwards from animation-fill-mode and then it works fine, as below.
animation-fill-mode:forwards - After the animation ends,
the animation will apply the property values for the time the animation ended.
Over-here animation ends at 360deg when using forwards.
Update -
And in your case you need to use forwards just to fade-in span tag i.e. from opacity 0 to 1, so for that you can declare two different animation.
body, html {
height: 100%;
}
.main-content {
position: relative;
height: 100%;
}
.box-container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.box-container ul {
list-style: none;
}
.box-container .box {
display: inline-block;
height: 100px;
width: 100px;
background: pink;
border-radius: 50%;
text-align: center;
position: relative;
cursor: pointer;
}
.box-container .box span {
transform-origin: 0% 0%;
position: absolute;
top: 50%;
left: 50%;
border-radius: 50%;
margin-top: -20px;
margin-left: -20px;
display: block;
width: 40px;
height: 40px;
background: #58C9B9;
color: #fff;
line-height: 40px;
text-align: center;
transform-origin: calc(100% - 20px) calc(100% - 20px);
opacity:0;
transform:rotate(0deg);
}
li:nth-child(1):hover > .animate1{
transition:.5s ease;
transform:rotateY(360deg);
}
li:nth-child(2):hover > .animate2{
transition:.5s ease;
transform:rotateY(360deg);
}
li:nth-child(3):hover > .animate3{
transition:.5s ease;
transform:rotateY(360deg);
}
.animate1{
animation: animate .5s ease-in-out, opty .5s ease-in-out forwards;
}
.animate2 {
animation: animate .5s ease-in-out .5s, opty .5s ease-in-out forwards .5s;
}
.animate3 {
animation: animate .5s ease-in-out 1.0s, opty .5s ease-in-out forwards 1s;
}
#keyframes animate {
0% {
opacity: 0;
transform: rotateY(0deg);
}
100% {
opacity: 1;
transform: rotateY(360deg);
}
}
#keyframes opty {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
<div class="main-content">
<div class="box-container">
<ul class="list-unstyle list-inline">
<li class="box "><span class="animate1">20%</span></li>
<li class="box "><span class="animate2">40%</span></li>
<li class="box "><span class="animate3">50%</span></li>
</ul>
</div>
</div>
<div>
I think the problem is with your animation property .I'm edited it and it's working fine.I'm added the snippet below.
body, html {
height: 100%;
}
.main-content {
position: relative;
height: 100%;
}
.box-container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.box-container ul {
list-style: none;
}
.box-container .box {
display: inline-block;
height: 100px;
width: 100px;
background: pink;
border-radius: 50%;
text-align: center;
position: relative;
cursor: pointer;
}
.box-container .box span {
transform-origin: 0% 0%;
position: absolute;
top: 50%;
left: 50%;
border-radius: 50%;
margin-top: -20px;
margin-left: -20px;
display: block;
width: 40px;
height: 40px;
background: #58C9B9;
color: #fff;
line-height: 40px;
text-align: center;
transform-origin: calc(100% - 20px) calc(100% - 20px);
}
.box-container .box:hover span {
background: #4F86C6;
transform: rotateY(360deg);
transition: .5s;
}
.animate1 {
animation: animate .5s ease-in-out 1;
}
.animate2 {
animation: animate .5s ease-in-out .5s;
}
.animate3 {
animation: animate .5s ease-in-out 1s;
}
#keyframes animate {
0% {
opacity: 0;
transform: rotateY(0deg);
}
100% {
opacity: 1;
transform: rotateY(360deg);
}
}
<div class="main-content">
<div class="box-container">
<ul class="list-unstyle list-inline">
<li class="box "><span class="animate1">20%</span></li>
<li class="box "><span class="animate2">40%</span></li>
<li class="box "><span class="animate3">50%</span></li>
</ul>
</div>
</div>
<div>
I want to make it so that I can have multiple buttons opening their own transition. As of now I have this code which works fine, however, if I repeat it or even change the css (class) to match an individual button, the primary button will still open the secondary transition and the newly placed secondary button remains inactive.
CSS
<style>
body,
.container,
.content-wrap {
overflow: hidden;
width: 100%;
height: 100%;
}
.container {
background: #fff;
}
.menu-wrap a {
color: #b8b7ad;
}
.menu-wrap a:hover,
.menu-wrap a:focus {
color: #c94e50;
}
.content-wrap {
overflow-y: scroll;
-webkit-overflow-scrolling: touch;
}
.content {
position: absolute;
background: #fff;
overflow-y: visible;
}
.content::before {
position: absolute;
top: 0;
left: 0;
z-index: 10;
width: 100%;
height: 100%;
background: none;
content: '';
opacity: 0;
-webkit-transform: translate3d(100%,0,0);
transform: translate3d(100%,0,0);
-webkit-transition: opacity 0.4s, -webkit-transform 0s 0.4s;
transition: opacity 0.4s, transform 0s 0.4s;
-webkit-transition-timing-function: cubic-bezier(0.7,0,0.3,1);
transition-timing-function: cubic-bezier(0.7,0,0.3,1);
}
/* Menu Button 1 */
.menu-button {
position: absolute;
z-index: 1000;
margin: 1em;
padding: 0;
width: 10px;
height: 50px;
border: none;
text-indent: 2.5em;
font-size: 1.5em;
color: transparent;
background: transparent;
opacity: 1;
top: 510px;
left: 855px;
-ms-transform: rotate(7deg); /* IE 9 */ -webkit-transform: rotate(7deg); /* Chrome, Safari, Opera */
transform: rotate(90deg);
}
.menu-button::before {
position: absolute;
top: 0.5em;
right: 0.2em;
bottom: 0.4em;
left: -1px;
background: linear-gradient(#929292 20%, transparent 20%, transparent 40%, #929292 40%, #929292 58%, transparent 0%, transparent 80%, #929292 80%);
content: '';
}
.menu-button:hover {
opacity: 1;
}
/* Close Button */
.close-button {
width: 50px;
height: 50px;
position: absolute;
right: 1em;
top: 1em;
overflow: hidden;
text-indent: 1em;
font-size: 0.75em;
border: none;
background: transparent;
color: transparent;
opacity: 0;
}
.close-button::before,
.close-button::after {
content: '';
position: absolute;
width: 3px;
height: 100%;
top: 0;
left: 50%;
background: #bdc3c7;
}
.close-button::before {
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
.close-button::after {
-webkit-transform: rotate(-45deg);
transform: rotate(-45deg);
}
/* Comments */
.menu-wrap {
position: absolute;
z-index: 1000;
width: 429.0500011444092px;
height: 600.875px;
right: 0;
background: #0C0C0C;
top: 6px;
padding: 0;
font-size: 1.15em;
-webkit-transform: translate3d(500px,0,0);
transform: translate3d(500px,0,0);
-webkit-transition: -webkit-transform 0.4s;
transition: transform 0.4s;
-webkit-transition-timing-function: cubic-bezier(0.7,0,0.3,1);
transition-timing-function: cubic-bezier(0.7,0,0.3,1);
}
.menu,
.icon-list {
height: 100%;
}
.icon-list {
-webkit-transform: translate3d(0,100%,0);
transform: translate3d(0,100%,0);
}
.icon-list a {
display: block;
padding: 0.8em;
-webkit-transform: translate3d(0,500px,0);
transform: translate3d(0,500px,0);
}
.icon-list,
.icon-list a {
-webkit-transition: -webkit-transform 0s 0.4s;
transition: transform 0s 0.4s;
-webkit-transition-timing-function: cubic-bezier(0.7,0,0.3,1);
transition-timing-function: cubic-bezier(0.7,0,0.3,1);
}
.icon-list a:nth-child(2) {
-webkit-transform: translate3d(0,1000px,0);
transform: translate3d(0,1000px,0);
}
.icon-list a:nth-child(3) {
-webkit-transform: translate3d(0,1500px,0);
transform: translate3d(0,1500px,0);
}
.icon-list a:nth-child(4) {
-webkit-transform: translate3d(0,2000px,0);
transform: translate3d(0,2000px,0);
}
.icon-list a:nth-child(5) {
-webkit-transform: translate3d(0,2500px,0);
transform: translate3d(0,2500px,0);
}
.icon-list a:nth-child(6) {
-webkit-transform: translate3d(0,3000px,0);
transform: translate3d(0,3000px,0);
}
.icon-list a span {
margin-left: 10px;
font-weight: 700;
}
/* Shown menu */
.show-menu .menu-wrap {
-webkit-transform: translate3d(0,0,0);
transform: translate3d(0,0,0);
-webkit-transition: -webkit-transform 0.8s;
transition: transform 0.8s;
-webkit-transition-timing-function: cubic-bezier(0.7,0,0.3,1);
transition-timing-function: cubic-bezier(0.7,0,0.3,1);
}
.show-menu .icon-list,
.show-menu .icon-list a {
-webkit-transform: translate3d(0,0,0);
transform: translate3d(0,0,0);
-webkit-transition: -webkit-transform 0.8s;
transition: transform 0.8s;
-webkit-transition-timing-function: cubic-bezier(0.7,0,0.3,1);
transition-timing-function: cubic-bezier(0.7,0,0.3,1);
}
.show-menu .icon-list a {
-webkit-transition-duration: 0.9s;
transition-duration: 0.9s;
}
.show-menu .content::before {
opacity: 1;
-webkit-transition: opacity 0.8s;
transition: opacity 0.8s;
-webkit-transition-timing-function: cubic-bezier(0.7,0,0.3,1);
transition-timing-function: cubic-bezier(0.7,0,0.3,1);
-webkit-transform: translate3d(0,0,0);
transform: translate3d(0,0,0);
}</style>
BEING TRANSITIONED
<div class="menu-wrap">
</div>
BUTTON
<button class="menu-button" id="open-button">Open Menu</button>
DEMO
DEMO
As you can see if you scroll towards the middle of the demo result window you will see the first button opening both transitions while the second button does nothing, I want the second button to open the bottom transition on its own.