Animation CSS with dynamic text - css

I need to add an animation to this links on hover, I have to deal with the "width" of the link word can change. Is there any way to change dynamically the value of the translate animation?
.link {
text-decoration: none;
padding-left: 15px;
position: relative;
transition: 1s;
&:hover {
padding-left: 0;
.link--decoration {
animation: in 1s ease both;
}
}
}
.link--decoration {
animation: out 1s ease both;
left: 0;
position: absolute;
}
#keyframes in {
to {
transform: translate(calc(100% + 75px), 0);
}
}
#keyframes out {
from {
transform: translate(calc(100% + 75px), 0);
}
to {
transform: translate(0, 0);
}
}
<p><span class="link--decoration">&dash;</span><span class="link--text">Read Article</span></p>
<p><span class="link--decoration">&dash;</span><span class="link--text">Open Hours</span></p>
<p><span class="link--decoration">&dash;</span><span class="link--text">Open Localization</span></p>
https://codepen.io/serrazina/pen/zzEaLE

There are 2 approaches I see to solving this. The first uses left to animate the decoration. Like this:
.link {
text-decoration: none;
padding-left: 15px;
position: relative;
transition: 1s;
}
.link:hover {
padding-left: 0;
}
.link:hover .link--decoration {
animation: in 1s ease both;
}
.link--decoration {
animation: out 1s ease both;
left: 0;
position: absolute;
}
#keyframes in {
to {
left: calc(100% + 5px);
}
}
#keyframes out {
from {
left: calc(100% + 5px);
}
to {
left: 0;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p><span class="link--decoration">&dash;</span><span class="link--text">Read Article</span></p>
<p><span class="link--decoration">&dash;</span><span class="link--text">Open Hours</span></p>
<p><span class="link--decoration">&dash;</span><span class="link--text">Open Localization</span></p>
The second method uses transform to take advantage of hardware acceleration. The difference between this method and yours was adding a width: 100% to the decoration as the translate is based on the width of the element. In your example that meant transform: translateX(100%) was 100% the width of the decoration element, which was about 20px in width. In the demo below I've made the element 100% the width of it's parent element, so transform: translateX(100%) moves it to the right of the parent element.
.link {
text-decoration: none;
padding-left: 15px;
position: relative;
transition: 1s;
}
.link:hover {
padding-left: 0;
}
.link:hover .link--decoration {
animation: in 1s ease both;
}
.link--decoration {
animation: out 1s ease both;
left: 0;
width: 100%;
position: absolute;
}
#keyframes in {
to {
transform: translateX(calc(100% + 5px));
}
}
#keyframes out {
from {
transform: translateX(calc(100% + 5px));
}
to {
transform: translateX(0);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p><span class="link--decoration">&dash;</span><span class="link--text">Read Article</span></p>
<p><span class="link--decoration">&dash;</span><span class="link--text">Open Hours</span></p>
<p><span class="link--decoration">&dash;</span><span class="link--text">Open Localization</span></p>

Related

how can i make a specific animation on hover

Can somebody tell me how can i do this animation on hover please ?
Here some CSS code :
the effect is like a shake but it's not like the GIF
the effect is like a shake but it's not like the GIF
the effect is like a shake but it's not like the GIF
the effect is like a shake but it's not like the GIF
the effect is like a shake but it's not like the GIF
https://codepen.io/meruem92/pen/ZEoPWgP
.carousel-item {
transition: font-size 1s;
text-decoration: none;
text-transform: uppercase;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #fff;
&:before,
&:after {
display: block;
content: attr(data-glitch);
text-transform: uppercase;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
opacity: 0.8;
}
&:after {
color: #fff;
z-index: -2;
}
&:before {
color: #ffffff69;
z-index: -1;
}
&:hover {
font-size: 35px !important;
&:before {
animation: glitch 0.8s cubic-bezier(0.25, 0.46, 0.45, 0.94) both 2;
}
&:after {
animation: glitch 0.8s cubic-bezier(0.25, 0.46, 0.45, 0.94) reverse both 2;
}
}
#keyframes glitch {
0% { transform: translate(0); }
20% { transform: translate(-5px, 5px); }
40% { transform: translate(-5px, -5px); }
60% { transform: translate(5px, 5px); }
80% { transform: translate(5px, -5px); }
to { transform: translate(0); }
}
}
<p data-glitch="Explore" class="carousel-item"> lorem ipsum </p>
You will want to use the CSS pseudo :hover to make the animation work. Something like this:
div {
font-size:16px;
transition: all .3s ease;
}
div:hover {
font-size:20px;
}

Execute :hover and :hover:after animation with keyframes on component load

i would like to play this hover animation on component load, because I wrote a JS function that checks if the component is in the viewport and reloads it (changes its class).
You can use Sass (scss) or regular css.
.module .active {
display: inline-block;
position: relative;
color: var(--text-color-dark);
animation: loadInAnimation ease 3s;
animation-iteration-count: 1;
animation-fill-mode: forwards;
}
#keyframes loadInAnimation { // add :hover and :hover:after event
0% {
// start of animation
}
100% {
// end ofanimation
}
}
.module .active:after { // transform into keyframe animation
content: '';
position: absolute;
width: 100%;
transform: scaleX(0);
height: 2px;
bottom: 0;
left: 0;
background-color: var(--primary-color);
transform-origin: bottom right;
transition: transform 0.25s ease-out;
}
.module .active:hover:after { // transform into keyframe animation
transform: scaleX(1);
transform-origin: bottom left;
}
Thank you for your help!
JS (irrelevant):
<script>
function reveal() {
var reveals = document.querySelectorAll(".reveal");
console.log(reveals);
for (var i = 0; i < reveals.length; i++) {
var windowHeight = window.innerHeight;
var elementTop = reveals[i].getBoundingClientRect().top;
var elementVisible = 150;
if (elementTop < windowHeight - elementVisible) {
reveals[i].classList.add("active");
} else {
reveals[i].classList.remove("active");
}
}
}
window.addEventListener("scroll", reveal);
window.addEventListener("load", reveal);
</script>
HTML (also irrelevant):
<h2 className='heading reveal'>Tennisakademie Vasquez</h2>
I've fixed the animation with an additional element providing the :after feature:
.module .heading {
font-size: 2.75rem;
font-weight: 700;
margin-bottom: 2rem;
font-family: 'Oswald', sans-serif;
font-weight: 200;
color: var(--text-color-dark);
}
.module .active {
display: inline-block;
position: relative;
color: var(--text-color-dark);
animation-iteration-count: 1;
animation-fill-mode: forwards;
.reveal-line {
transform: scaleX(1);
transform-origin: bottom left;
content: '';
position: absolute;
width: 100%;
height: 2px;
bottom: 0;
left: 0;
background-color: var(--primary-color);
transform-origin: bottom right;
transition: transform 0.6s ease-out;
animation: loadInAnimation ease 1.5s;
}
}
#keyframes loadInAnimation {
0% {
width: 0%;
}
100% {
width: 100%;
}
}
<h3 className='heading reveal'>Unsere Angebote<div className='reveal-line' /></h3>

How to reverse an animation on mouse out hover on a absolute position element

I'm hiding an element which holds text with position absolute since some screen readers won't read it if the opacity is zero. I want to use a fade in animation like the first box, so I used a simple animation function, but I can't get the fade out animation to work. Do you guys know why?
Thanks in advance!
p {
color: red;
}
.card {
position: relative;
width: 100px;
height: 100px;
text-align: center;
border: 2px solid red;
}
.card.using-opacity .text-container {
opacity: 0;
-webkit-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}
.card.using-opacity:hover .text-container {
opacity: 1;
}
.card.using-position-absolute .text-container {
position: absolute;
top: -9999px;
left: -9999px;
width: 100%;
-webkit-animation: fadeOut 1s;
animation: fadeOut 1s;
}
.card.using-position-absolute:hover .text-container {
top: 0px;
left: 0px;
-webkit-animation: fadeIn 1s;
animation: fadeIn 1s;
}
#-webkit-keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
#keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
#-webkit-keyframes fadeOut {
from {
opacity: 1;
}
to {
opacity: 0;
}
}
#keyframes fadeOut {
from {
opacity: 1;
}
to {
opacity: 0;
}
}
<p>This is how it's supposed to work in terms of animation/transition but it's not accessible</p>
<div class="card using-opacity">
<div class="text-container">
<p>Some text</p>
</div>
</div>
<p>This is accessible, but I can't get the mouse out animated</p>
<div class="card using-position-absolute">
<div class="text-container">
<p>Some text</p>
</div>
</div>

How can i create an animation like this in css

I'm trying to create an animated text like bellow using css, how can i do this?
I already tried this:
span1 {
display: inline-block;
color: #e74c3c;
position: relative;
white-space: nowrap;
top: 0;
left: 0;
-webkit-animation: move 5s;
-webkit-animation-iteration-count: infinite;
-webkit-animation-delay: 1s;
}
#keyframes move {
0% {
top: 0px;
}
20% {
top: -50px;
}
40% {
top: -100px;
}
60% {
top: -150px;
}
80% {
top: -200px;
}
100% {
top: -300px;
}
}
<span1>
web developer<br /> css cowboy<br /> self-facilitating media node<br /> box inside a box<br /> part of the problem
</span1>
but it has a delay after last text that i need to remove!
See This:
*{
box-sizing: border-box;
}
body {
background-color: skyblue;
}
div {
overflow: hidden;
color: #fff;
text-align: center;
font-size: 20px;
position: relative;
height: 100px;
margin-top: 100px;
}
div p {
height: 100px;
animation: move 7s infinite linear;
position: relative;
bottom: -100px;
font-size: 36px;
margin: 0;
}
#keyframes move {
0% {bottom: -100px;}
10%, 20% {bottom: 0px}
40%,50% {bottom: 100px;}
70%,80% {bottom: 200px;}
100% {bottom: 300px}
}
<div>
<p>50% OFF</p>
<p>Some Text</p>
<p>Write by: Ehsan Taghdisi</p>
</div>
.anim1 {
animation: anim1 1.5s infinite;
}
.anim2 {
animation: anim2 1.5s infinite;
}
#keyframes anim1 {
0% {
transform: translateY(-10px);
}
50% {
transform: translateY(-80px);
}
100% {
transform: translateY(-10px);
}
}
#keyframes anim2 {
0% {
transform: translateY(0px);
}
50% {
transform: translateY(-80px);
}
100% {
transform: translateY(0px);
}
}
<div style="height:40px;overflow:hidden">
<h1 class="anim1">Hello animation 1</h1>
<h1 class="anim2">Hello animation 2</h1>

CSS3 animation - changing words to infinity without rewind

It's my first time experimenting with css3 animations and I have a question regarding the following setup:
Link to codepen
After item3 the animation rewinds to item1. I wonder if it's possible to let follow the item1 after the item3 without this rewinding, so that item3 also moves to the top and item1 slides in from the bottom again, and so on and on?
HTML
<div id="change">
<span>item1</span>
<span>item2</span>
<span>item3</span>
</div>
CSS
#change {
overflow: hidden;
height: 58px;
color: black;
font-size: 3em;
}
#change span {
position: relative;
display: block;
animation: myAnim 10s ease infinite 0s;
-webkit-animation: myAnim 10s ease infinite 0s;
}
#keyframes myAnim {
0% { top: 0px; }
20% { top: 0px; }
35% { top: -58px; }
55% { top: -58px; }
70% { top: -116px; }
90% { top: -116px; }
100% { top: 0px; }
}
#-webkit-keyframes myAnim {
0% { top: 0px; }
20% { top: 0px; }
35% { top: -58px; }
55% { top: -58px; }
70% { top: -116px; }
90% { top: -116px; }
100% { top: 0px; }
}
Unfortunately, there doesn't seem to be an easy way to do this. If we were using an image, you could easily just take advantage of repeat and force the beginning of the element to start at the end of the element. However, since we aren't using an image, the only solution I can think of would be to use the first element as the last element.
UPDATED EXAMPLE HERE
HTML
<div id="change">
<span>item1</span>
<span>item2</span>
<span>item3</span>
<span>item1</span> <!-- The first element is used as the last element-->
</div>
Modified CSS
#-webkit-keyframes myAnim {
0% { top: 0; }
20% { top: 0; }
35% { top: -58px; }
55% { top: -58px; }
70% { top: -116px; }
90% { top: -116px; }
100% { top: -172px; }
}
it didn't let me rest, so i figured out another solution. with no doubled item1, but the missing part in this is that it doesn't start with the item1 being already there at the beginning.
Link to codepen
HTML
<div id="change">
<span>item1</span>
<span>item2</span>
<span>item3</span>
</div>
CSS
#change {
overflow: hidden;
height: 58px;
color: black;
font-size: 3em;
position: relative;
}
#change span {
position: absolute;
display: block;
animation: myAnim 9s ease infinite 0s;
-webkit-animation: myAnim 9s ease infinite 0s;
}
#change span:nth-of-type(2) {
animation-delay: 3s;
-webkit-animation-delay: 3s;
top: 58px;
}
#change span:nth-of-type(3) {
animation-delay: 6s;
-webkit-animation-delay: 6s;
top: 58px;
}
#keyframes myAnim {
0% { top: 58px; }
15% { top: 0px; }
33% { top: 0px; }
48% { top: -58px; opacity:1; }
60% { top: -58px; opacity: 0; }
80% { top: 58px; opacity: 0; }
100% { top: 58px; opacity: 1; }
}
#-webkit-keyframes myAnim {
0% { top: 58px; }
15% { top: 0px; }
33% { top: 0px; }
48% { top: -58px; opacity:1; }
60% { top: -58px; opacity: 0; }
80% { top: 58px; opacity: 0; }
100% { top: 58px; opacity: 1; }
}

Resources