Masking a circle and moving the mask with animation - html

I have a circular div that is vertically and horizontally centered in div. I am trying to achieve a css animation, that it seems like it's fading in from top to bottom.
I thought of making height 0 initially and moving to 50px, however as it is centered, it starts to getting created from the center. Instead, I want to it to get positioned to the initial position and it gets created from top to bottom. Just like there is a square only masking the circle and nothing else, and it moves to down.
#circle {
width: 80px;
height: 80px;
border-radius: 50px;
}
How can I add this a square mask to achieve the below effect?
Please note that background has a gradient, so I can't put a square and assign it a color directly, thus I thought I need to mask them.
How to achieve this effect?
What I have tried:
#keyframes example {
from {height: 0}
to {height: 80px}
}
As the circle is centered, it starts to expand from the middle. This is not what I want. That's why I thought of the mask

Edited answer:
I am able to achieve this with a combination of a image background and background-position animation.
This will not work if you set the background as a CSS color like #fff. it needs to be an image or a gradient. You also need to set background-repeat to no-repeat
The animation simply starts with the background out of the div display area then pulls the the background downward into the display area.
Kindly check the examples in full-screen.
Working snippet (jpeg image as object background):
body {
background: linear-gradient(135deg, rgba(244, 226, 156, 0) 0%, rgba(59, 41, 58, 1) 100%), linear-gradient(to right, rgba(244, 226, 156, 1) 0%, rgba(130, 96, 87, 1) 100%);
margin: 0 auto;
height: 120vh;
overflow: hidden;
}
.sun {
height: 400px;
width: 400px;
border-radius: 100vw;
margin: 5em auto;
animation-name: sunrise;
animation-duration: 10s;
animation-delay: .5s;
animation-timing-function: linear;
animation-iteration-count: 1;
background: url(https://image.ibb.co/eVdQ3Q/white.jpg);
background-repeat: no-repeat;
animation-fill-mode: forwards;
opacity: 0;
}
#keyframes sunrise {
from {
opacity: 1;
background-position: 0 -700px;
}
to {
opacity: 1;
background-position: 0 0px;
}
}
<div class="sun"></div>
Working snippet (gradient as object background):
body {
background: linear-gradient(135deg, rgba(244, 226, 156, 0) 0%, rgba(59, 41, 58, 1) 100%), linear-gradient(to right, rgba(244, 226, 156, 1) 0%, rgba(130, 96, 87, 1) 100%);
margin: 0 auto;
height: 120vh;
overflow: hidden;
}
.sun {
height: 400px;
width: 400px;
border-radius: 100vw;
margin: 5em auto;
animation-name: sunrise;
animation-duration: 10s;
animation-timing-function: linear;
animation-iteration-count: 1;
background: linear-gradient(white,white);
background-repeat: no-repeat;
animation-fill-mode: forwards;
opacity: 0;
}
#keyframes sunrise {
from {
opacity: 1;
background-position: 0 -700px;
}
to {
opacity: 1;
background-position: 0 0px;
}
}
<div class="sun"></div>

Related

How do I make this animation smoother in CSS?

I'm a novice to web development, and I'd like to make a circle rotate the orientation of its linear gradient smoothly, but there is a jump in between each orientation.
I expected it to be smooth, since I used steps and set the animation-timing-function to linear, but there is a jump in between each step of the animation.
I'm not quite sure how to display the code here, if anyone has any tips for a beginner I would appreciate it.
Edit: Here is the code :)
/* The animation: */
#keyframes gradientShift {
0% {background-image: linear-gradient(to right, rgb(0, 4, 255), rgb(0, 162, 255));}
25% {background-image: linear-gradient(rgb(0, 4, 255), rgb(0, 162, 255));}
50% {background-image: linear-gradient(to right, rgb(0, 162, 255), rgb(0, 4, 255));}
75% {background-image: linear-gradient(rgb(0, 162, 255), rgb(0, 4, 255));}
100% {background: linear-gradient(to right, rgb(0, 4, 255), rgb(0, 162, 255));}
}
/* The other styles*/
.circle-wrapper {
background-image: linear-gradient(to right, rgb(0, 4, 255), rgb(0, 162, 255));
animation: gradientShift;
animation-duration: 2s;
animation-iteration-count: infinite;
animation-timing-function: linear;
margin-top: 28vh;
width: 12vh;
height: 12vh;
margin-left: 35vh;
border-radius: 100px;
position: absolute;
padding: 3px;
z-index: 1000;
}
<div class="circle-wrapper">
<div class="circle-module"> </div>
</div>
That is because CSS cannot handle transitions in background images. Basically your CSS animation is "stepped" and will have 5 distinct frames with no interpolation in between.
Seeing that you are only rotating the angle of the gradient and not performing and color changes, you can simply set the linear-gradient on a pseudo-element and rotate it instead:
/* The animation: */
#keyframes rotate {
0% {
transform: rotate(0);
}
100% {
transform: rotate(360deg);
}
}
/* The other styles*/
.circle-wrapper {
margin-top: 28vh;
width: 12vh;
height: 12vh;
margin-left: 35vh;
border-radius: 100px;
position: absolute;
padding: 3px;
z-index: 1000;
overflow: hidden;
}
.circle-wrapper::before {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-image: linear-gradient(to right, rgb(0, 4, 255), rgb(0, 162, 255));
animation: rotate;
animation-duration: 2s;
animation-iteration-count: infinite;
animation-timing-function: linear;
content: '';
}
.circle-wrapper>* {
position: relative;
}
<div class="circle-wrapper">
<div class="circle-module"> </div>
</div>
There is no way in css to animate the background-image property.
You see the CSS Animated Properties to know what are the animatable css properties.
But it still possible with javascript, you can use setInterval() with a very small amount of time to change your rotation degree:
let circle = document.getElementsByClassName("circle-wrapper")[0]
let rotateDeg = 0
setInterval(function() {
circle.style.backgroundImage = "linear-gradient(" + ++rotateDeg + "deg, rgb(0, 4, 255), rgb(0, 162, 255))"
}, 2000/360)
.circle-wrapper {
background-image: linear-gradient(to right, rgb(0, 4, 255), rgb(0, 162, 255));
margin-top: 28vh;
width: 12vh;
height: 12vh;
margin-left: 35vh;
border-radius: 100px;
position: absolute;
padding: 3px;
z-index: 1000;
}
<div class="circle-wrapper">
<div class="circle-module"> </div>
</div>

How to animate linear-gradient from top right to bottom left?

In my code, on hover the direction of linear-gradient animation is like counterclockwise, how can do it stright linear from top right to bottom left?
.boxstyle
{
background-color:rgba(0,69,255,1);
background-size: 0% 100%;
transition: background-color .5s;
width: 150px;
height: 150px;
}
.boxstyle:hover
{
background-image:linear-gradient(to left bottom, rgba(189,41,242,0) 0%, rgba(189,41,242,0) 40%, rgba(189,41,242, 0.9) 100%);
background-repeat:no-repeat;
background-size: 200% 100%;
transition:background-size 1s, background-color 1s;
}
<div class="boxstyle">hover it</div>
If you really want the pink to start at the top right and move to the bottom left you could use before and after pseudo elements, the before with just the color, the after also with the linear-gradient.
The after pseudo element moves diagonally across to the bottom left on hover of the main element.
.boxstyle {
width: 150px;
height: 150px;
position: relative;
overflow: hidden;
}
.boxstyle::before,
.boxstyle::after {
content: '';
position: absolute;
z-index: -1;
}
.boxstyle::before {
background-color: rgba(0, 69, 255, 1);
width: 100%;
height: 100%;
}
.boxstyle::after {
background-image: linear-gradient(to left bottom, rgba(189, 41, 242, 0) 0%, rgba(189, 41, 242, 0) 40%, rgba(189, 41, 242, 0.9) 100%);
bottom: 100%;
left: 100%;
transition: all 1s linear;
width: 200%;
height: 200%;
}
.boxstyle:hover::after {
bottom: -10%;
left: -10%;
}
<div class="boxstyle">hover it</div>
Old comment: This fits your verbal description of the effect desired, but I suspect it isn't exactly what you intended - did you want it more 'blendy' and subtle, becoming the final picture only at the very end?
UPDATE: Since seeing your images this snippet uses an after that has twice the dimensions of the main element so spreading out the colors more. It 'settles' on a hover a bit beyond the bottom left.
You can try like below. Invert the positions to get the opposite direction. More details: Using percentage values with background-position on a linear-gradient
.boxstyle {
background: linear-gradient(to top right, #0000 40%, rgb(189 41 242/0.9) 100%);
background-size: 200% 200%;
background-position: 0% 100%;
background-color: rgba(0, 69, 255, 1);
transition: .5s;
width: 150px;
height: 150px;
}
.boxstyle:hover {
background-position: 100% 0;
}
<div class="boxstyle">hover it</div>
If you prefer the pink color area to travel through the box (from top right corner to bottom left corner), it can be implemented by setting the linear-gradient so that has the pink area in the middle.
Example:
.boxstyle {
background: linear-gradient(to top right, #0000 10%, rgb(189 41 242/0.9) 40%, rgb(189 41 242/0.9) 60%, #0000 90%);
background-size: 500% 500%;
background-position: 0% 100%;
background-color: rgba(0, 69, 255, 1);
transition: .5s ease-in-out;
width: 150px;
height: 150px;
color: #fff;
}
.boxstyle:hover {
background-position: 100% 0;
}
<div class="boxstyle">hover it</div>

How do I custom lazyload on both text and images with animation?

I need to make a lazyload that actually works like one that is in Facebook. I created a css class that has this.
#keyframes placeholder {
0% { background-position: -600px 0 }
100% { background-position: 600px 0 }
}
.text-loader{
color: transparent !important;
animation-duration: 1s;
animation-fill-mode: forwards;
animation-iteration-count: infinite;
animation-name: placeholder;
animation-timing-function: linear;
background: #eeeeee;
background: linear-gradient(to right, #eee 8%, #419AF3 18%, #eee 33%);
background-size: 1200px 100px;
min-height: 30px;
width: 100%;
margin: 5px 0 5px 0;
border-radius: 3px;
&::selection{
color: transparent;
}
}
And wrote some scripts to make it look like it actually works.
var agalar = $("span, p, h1, h2, h3");
$(".contents").find(agalar).each(function(){
$(this).addClass("text-loader");
});
$(window).on("load",function(){
$(agalar).removeClass("text-loader");
});
Everything "seems" to work but I am not sure if it actually works. (I am making an infinite scrolling section so I guess will need to pull content from back-end), the thing is how can I make this a real lazyload?

CSS3 sliding gradient animation - how it works

Following code produces sliding gradient animation without any line of javascript code:
html {
height: 100%
}
body {
height: 100%;
margin: 0
}
#keyframes loading {
from {
background-position: -5000% 0, 0 0
}
to {
background-position: 5000% 0, 0 0
}
}
.skeleton {
height: 100%;
animation-name: loading;
animation-duration: 1.5s;
animation-iteration-count: infinite;
background-color: #fff;
background-repeat: no-repeat;
background-image: linear-gradient(90deg, hsla(0, 0%, 100%, 0), hsla(0, 0%, 100%, .8) 50%, hsla(0, 0%, 100%, 0)), linear-gradient(#e5e5e5 100%, transparent 0);
background-size: 99% 100%;
}
<div class="skeleton"></div>
I experimented with some properties and still do not understand how it works. Especially, when background-size: 99% 100%; is changed to background-size: 100% 100%; animation slides in opposite direction!
Could you explain it?
I don't know what's your browser and its version. But on my computer, if background-size: 100% 100% then the animation will be stop. (Actually, the background-position will be ignored)
The idea of this trick is moving background-image (linear-gradient) by background-position. (Check the comment in code below for detail)
About your second question, you should refer this answer CSS background-position ignored when using background-size. A quick summary, you can't use percent for background-position if background-size reaches to 100%. This happens because the image in background has no space to move.
If you insist to use background-size with 100%. I afraid you have to use absolute values.
BTW, I've upgraded the code. Now it looks better.
html {
height: 100%
}
body {
height: 100%;
margin: 0
}
#keyframes loading {/* original code */
from {/* This is the position of image of the first frame */
background-position: -5000% 0, 0 0
}
to {/* This is the pos of img of the last frame */
background-position: 5000% 0, 0 0
}
}
#keyframes betterLoading {
0% {/* This is the position of image of the first frame */
background-position: -5000% 0, 0 0
}
50% {
/* This is the pos of img of a frame in the middle happening animation */
/* If duration is 1s then the pos below will be at 0.5s */
background-position: 5000% 0, 0 0
}
100% {/* This is the pos of img of the last frame */
background-position: -5000% 0, 0 0
}
}
.skeleton {
height: 100%;
animation-name: betterLoading;
animation-duration: 1.5s;
animation-iteration-count: infinite;
background-color: #fff;
background-repeat: no-repeat;
background-image: linear-gradient(90deg, hsla(0, 0%, 100%, 0), hsla(0, 0%, 100%, .8) 50%, hsla(0, 0%, 100%, 0)), linear-gradient(green 100%, transparent 0);
background-size: 99% 100%, cover;
}
<div class="skeleton"></div>

Strange behaviour in CSS3 transition with linear gradient

I am trying to make a progress bar and using css3 transition to give it a fill effect.
jsfiddle here
When I give it a fixed size, it works as usual, but Problem is when I set the background-size:100% the fill becomes stretch.
How can I create fill effect using background-size:100%?
Progressbar1 is with fixed width and background-size
Progressbar2 is with 100% width and background-size
/* PROGRESS */
.progress {
background-color: #e5e9eb;
height: 0.25em;
position: relative;
width: 24em;
}
.progress-bar {
animation-duration: 3s;
animation-name: width;
background-image: repeating-linear-gradient(to right, transparent, #000 50px, #fff 100px, transparent 150px);
background-size: 24em 0.25em;
height: 100%;
position: relative;
width:100%
}
.progress2 {
background-color: #e5e9eb;
height: 0.25em;
position: relative;
width: 100%;
}
.progress-bar2 {
animation-duration: 3s;
animation-name: width;
background-image: repeating-linear-gradient(to right, transparent, #000 50px, #fff 100px, transparent 150px);
background-size: 100% 0.25em;
height: 100%;
position: relative;
width:100%
}
/* ANIMATIONS */
#keyframes width {
0%, 100% {
transition-timing-function: cubic-bezier(1, 0, 0.65, 0.85);
}
0% {
width: 0%;
}
100% {
width: 100%;
}
}
<div class="progress">
<div class="progress-bar">
</div>
</div>
<br>
<div class="progress2">
<div class="progress-bar2">
</div>
</div>