CSS Smooth end of an animation - css

I have made this loader bar. Is it possible that when I remove the class "animate" to wait the animation to end?
Or just make the bar fade out (it should now but the animation stops).
I thought to let the animation run continuously and fade the bar's colours but i don't like to leave an animation always running....
I don't really like that it just disappears :(
$("#btn").click(function() {
$(".loadBar").addClass("animate");
setTimeout(function() {
$(".loadBar").removeClass("animate");
}, 5000)
})
#btn {
margin-top: 50px;
}
.loadBar {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 2px;
background-color: transparent;
z-index: 4;
opacity: 0;
-moz-transition: opacity ease-in 1s;
-o-transition: opacity ease-in 1s;
-webkit-transition: opacity ease-in 1s;
transition: opacity ease-in 1s;
}
.loadBar .bar {
content: "";
display: inline;
position: absolute;
width: 0;
height: 100%;
left: 50%;
text-align: center;
-moz-transition: left 2s 2s;
-o-transition: left 2s 2s;
-webkit-transition: left 2s 2s;
transition: left 2s 2s;
}
.loadBar .bar:nth-child(1) {
background-color: #4183D7;
}
.loadBar .bar:nth-child(2) {
background-color: #FFF;
}
.loadBar .bar:nth-child(3) {
background-color: #FFF;
}
.loadBar.animate .bar {
-moz-transition: left 0s 0s;
-o-transition: left 0s 0s;
-webkit-transition: left 0s 0s;
transition: left 0s 0s;
}
.loadBar.animate .bar:nth-child(1) {
-moz-animation: loading 1.5s infinite;
-o-animation: loading 1.5s infinite;
-webkit-animation: loading 1.5s infinite;
animation: loading 1.5s infinite;
}
.loadBar.animate .bar:nth-child(2) {
-moz-animation: loading 1.5s 0.5s infinite;
-o-animation: loading 1.5s 0.5s infinite;
-webkit-animation: loading 1.5s 0.5s infinite;
animation: loading 1.5s 0.5s infinite;
}
.loadBar.animate .bar:nth-child(3) {
/* -moz-animation : loading 3s linear 2s infinite;
-o-animation : loading 3s linear 2s infinite;
-webkit-animation: loading 3s linear 2s infinite;
animation : loading 3s linear 2s infinite;*/
}
.loadBar.animate {
opacity: 1;
}
#-moz-keyframes loading {
from {
left: 50%;
width: 0;
z-index: 100;
}
50% {
left: 0;
width: 100%;
z-index: 10;
}
to {
left: 0;
width: 100%;
}
}
#-webkit-keyframes loading {
from {
left: 50%;
width: 0;
z-index: 100;
}
50% {
left: 0;
width: 100%;
z-index: 10;
}
to {
left: 0;
width: 100%;
}
}
#keyframes loading {
from {
left: 50%;
width: 0;
z-index: 100;
}
50% {
left: 0;
width: 100%;
z-index: 10;
}
to {
left: 0;
width: 100%;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="loadBar">
<div class="bar"></div>
<div class="bar"></div>
<div class="bar"></div>
</div>
<button id="btn">Show loadbar</button>

Well currently your animation doesnt end...
animation: loading 1.5s 0.5s infinite;
so lets fix that by making it iterate 1 time:
animation: loading 1.5s 0s 1;
Because its one iteration and no delay the overall animation runtime is 1500ms...so you set the timeout to 1500ms like so...
EDIT: Because you also specified (in the comments to this answer) that you would want the animation to continue until ajax has loaded I have amended my code to do so
$("#btn").click(function() {
doajax();
})
function doajax() {
$(".loadBar").addClass("animate");
//every 1500ms check if ajax has done loading
setInterval(function() {
if ($(".loadBar").hadClass("page-loaded")) {
$(".loadBar").removeClass("animate");
}
}, 1500);
$.ajax({
url: "http://www.mypage/",
type: "POST",
data: "",
success: function(data) {
$(".loadBar").addClass("page-loaded");
}
});
}
#btn {
margin-top: 50px;
}
.loadBar {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 2px;
background-color: transparent;
z-index: 4;
opacity: 0;
-moz-transition: opacity ease-in 1s;
-o-transition: opacity ease-in 1s;
-webkit-transition: opacity ease-in 1s;
transition: opacity ease-in 1s;
}
.loadBar .bar {
content: "";
display: inline;
position: absolute;
width: 0;
height: 100%;
left: 50%;
text-align: center;
-moz-transition: left 2s 2s;
-o-transition: left 2s 2s;
-webkit-transition: left 2s 2s;
transition: left 2s 2s;
}
.loadBar .bar:nth-child(1) {
background-color: #4183D7;
}
.loadBar .bar:nth-child(2) {
background-color: #FFF;
}
.loadBar .bar:nth-child(3) {
background-color: #FFF;
}
.loadBar.animate .bar {
-moz-transition: left 0s 0s;
-o-transition: left 0s 0s;
-webkit-transition: left 0s 0s;
transition: left 0s 0s;
}
.loadBar.animate .bar:nth-child(1) {
-moz-animation: loading 1.5s 2;
-o-animation: loading 1.5s 2;
-webkit-animation: loading 1.5s 2;
animation: loading 1.5s 2;
}
.loadBar.animate .bar:nth-child(2) {
-moz-animation: loading 1.5s 0.5s 2;
-o-animation: loading 1.5s 0.5s 2;
-webkit-animation: loading 1.5s 0.5s 2;
animation: loading 1.5s 0.5s 2;
}
.loadBar.animate .bar:nth-child(3) {
/* -moz-animation : loading 3s linear 2s infinite;
-o-animation : loading 3s linear 2s infinite;
-webkit-animation: loading 3s linear 2s infinite;
animation : loading 3s linear 2s infinite;*/
}
.loadBar.animate {
opacity: 1;
}
#-moz-keyframes loading {
from {
left: 50%;
width: 0;
z-index: 100;
}
50% {
left: 0;
width: 100%;
z-index: 10;
}
to {
left: 0;
width: 100%;
}
}
#-webkit-keyframes loading {
from {
left: 50%;
width: 0;
z-index: 100;
}
50% {
left: 0;
width: 100%;
z-index: 10;
}
to {
left: 0;
width: 100%;
}
}
#keyframes loading {
from {
left: 50%;
width: 0;
z-index: 100;
}
50% {
left: 0;
width: 100%;
z-index: 10;
}
to {
left: 0;
width: 100%;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="loadBar">
<div class="bar"></div>
<div class="bar"></div>
<div class="bar"></div>
</div>
<button id="btn">Show loadbar</button>

Related

How to animate a text so it goes to the top of the screen in CSS?

As I open my page, my text appears in the middle of the screen by an animation (I use “animate.css”) and then I want it to move to the top of the screen no matter what the screen size is. Is there a way to do that?
How I centered my text div;
.center {
position: absolute;
margin: auto;
top: 0;
right: 0;
bottom: 0px;
left: 0;
width: 100%;
height: 170px;
background-color: red;
}
My animation techniques; (instead of bottom: 300px, I want it to go to the top of the page.)
.center:hover {
-webkit-animation: animationShrink 2s 1 ease forwards;
}
.container:hover {
-webkit-animation: animationUp 2s 1 ease forwards;
}
#-webkit-keyframes animationShrink {
from {
font-size: 1em;
}
to {
font-size: 0.6em;
}
}
#-webkit-keyframes animationUp {
from {
bottom: 0px;
} to {
bottom: 300px;
}
}
Thanks.
HTML:
<a class='center'>Text</a>
CSS:
.center {
position: absolute;
left: 0;
top: calc(50vh - (170px / 2));
width: 100%;
height: 170px;
background-color: red;
-ms-transition: top 1s linear;
-moz-transition: top 1s linear;
-o-transition: top 1s linear;
-webkit-transition: top 1s linear;
transition: top 1s linear;
}
.center:active {
top: 0;
-ms-transition: top 2s linear;
-moz-transition: top 2s linear;
-o-transition: top 2s linear;
-webkit-transition: top 2s linear;
transition: top 2s linear;
}
Check this plunker for example:
https://plnkr.co/edit/GvH176vMGKSD47VrleXQ?p=preview
I used pseudo-class "active" to start animation (activate by clicking on element, just for example). This code should give you good base for customising.

CSS transition making page too wide

I am trying to make a box slide in from the left of my page while in the viewport, however it's making my page too wide and I can't get it to reverse once out of the viewport once I've scrolled past it. This is what I have:
.box {
background: #2db34a;
border-radius: 6px;
cursor: pointer;
height: 95px;
line-height: 95px;
text-align: center;
transition-property: background;
transition-duration: 1s;
transition-timing-function: linear;
width: 95px;
margin-top: 500px;
}
#slide {
position: absolute;
right: -100px;
width: 100px;
height: 100px;
background: blue;
overflow: hidden;
animation: slide 0.5s forwards;
animation-delay: 1s
-webkit-animation: moveToRight .6s ease both;
animation: moveToRight .6s ease both;
-webkit-animation: moveFromRight .6s ease both;
animation: moveFromRight .6s ease both;
}
#-webkit-keyframes moveToRight {
from { }
to { -webkit-transform: translateX(100%); }
}
#keyframes moveToRight {
from { }
to { -webkit-transform: translateX(100%); transform: translateX(100%); }
}
#-webkit-keyframes moveFromRight {
from { -webkit-transform: translateX(100%); }
}
#keyframes moveFromRight {
from { -webkit-transform: translateX(100%); transform: translateX(100%); }
}
</style>
</head>
<body>
<div id="slide" class="box">Box</div>
</body>
</html>
I can't use any plug-ins to do this either.

Loading animation and locked all elements on page

I have added on my site loading animation prepared in CSS code. I have a problem because when the animation ends it all on the page is locked and nothing can be a click away.
.loader2 {
height: 100%;
position: absolute;
top: 0;
z-index: 400;
width: 100%;
-webkit-animation: loader2 2s linear 0s;
-moz-animation: loader2 2s linear 0s;
-o-animation: loader2 2s linear 0s;
animation: loader2 2s linear 0s;
}
#-webkit-keyframes loader2{ 0%{ background-color: #0000FF; opacity: 1; }
100%{ background-color: #ffffff; opacity: 0; }}
and in html (at the bottom of the code): <div class="loader2"></div>
I solved this problem, but it adds additional animations, which I do not need.
#-webkit-keyframes loader2{ 0%{ background-color: #0000FF; opacity: 1; width: 100%; }
100%{ background-color: #ffffff; opacity: 0; width: 0%; }}
try to change the z-index property on the last keyframe, setting a negative value
#-webkit-keyframes loader2{
0% { background-color: #0000FF; opacity: 1; z-index: 400; }
99.9% { background-color: #ffffff; opacity: 0; z-index: 400; }
100% { z-index: -1; }
}

CSS3 centering animation

1.So my animation is having a flag go down into the middle of the page and then disappearing, but the flag wont go down to the middle using top: 50% and left 50%, I want it to go to the middle by whatever size the browser is.
#-webkit-keyframes brazil-animation
{
0%
{
width : 200px;
height : 150px;
top : 0px;
left : 0px;
}
80% {
width: 200px;
height: 150px;
visibility: hidden;
opacity: 0.6;
}
100% {
width: 200px;
height: 150px;
top: 50%;
left: 50%;
visibility: hidden;
}
}
div.brazil,#brazil {
position: absolute;
-webkit-animation: brazil-animation 5s both 1s linear;
-moz-animation: brazil-animation 5s both 1s linear;
-ms-animation: brazil-animation 5s both 1s linear;
-o-animation: brazil-animation 5s both 1s linear;
animation: brazil-animation 5s both 1s linear;
-webkit-animation-delay: 3s;
}
Use transform or margin
#-webkit-keyframes brazil-animation{
80% {
visibility: hidden;
opacity: 0.6;
}
100% {
top: 50vh;
left: 50vw;
transform: translate3d(-100px,-75px,0);
/*margin: -75px 0 0 -100px;/*margin: height/2 0 0 width/2 but the animation will not be smooth*/
}
}
div.flag {
top:0;
left:0;
width : 200px;
height : 150px;
position: absolute;
background: red;
-webkit-animation: brazil-animation 5s both 3s linear;
}
<div class=flag></div>
Fullscreen demo

Firefox ignoring one animation in comma-separated declaration

Firefox is ignoring the declaration for the first animation (cursorfadein), while showing the animations that handle the moving and fading out just fine. I've tried using units like 0s and 0msCode below.
#-moz-keyframes cursorfadein {
0% { opacity: 0; }
100% { opacity: 1; }
}
#-moz-keyframes cursormover {
0% {
top: 300px;
right: 90px;
}
50%, 60% {
top: 204px;
right: 234px;
}
100% {
top: 300px;
right: 290px;
}
}
#-moz-keyframes cursorhider {
0% { opacity: 1; }
100% { opacity: 0; }
}
.cursor {
position: absolute;
background: url(https://cdn.mediacru.sh/A/AhRlh9cYN5sf.png) no-repeat;
background-size: contain;
display: block;
height: 20px;
width: 20px;
-webkit-animation: cursorfadein 3s ease 0s, cursormover 3s ease-in-out 2s both, cursorhider 1s linear 6s both;
-moz-animation: cursorfadein 3s ease 0s, cursormover 3s ease-in-out 2s both, cursorhider 1s linear 6s both;
animation: cursorfadein 3s ease 0s, cursormovein 3s ease-in-out 2s both, cursorhider 1s linear 6s both;
}
I have the -webkit- and non-prefixed #keyframes as well (included in the Codepen), just wanted to be brief here on SO
Codepen here.

Resources