Animation with css that changes on click - css

I’ m doing an animation for a website.
My client wants a row with some images.
These images have to translate constantly on load, but when I click in the row, the images must accelerate as an exponential function for 4 seconds: after that, the website must change page.
Yesterday I have created the animation, changing the animation-duration every some millis by an hook, that strategy was working perfectly, but I don’t like that because it seems to be too heavy.
Now I have found another solution that uses only css and an hook to change the class.
Given this new solution, I still have 2 issues:
after the click, the animation restarts
the second part of the animation calculates the delay from the start and not from the first part of animation
Thank you for your reply
const row = document.querySelector(".row");
row.addEventListener("click", () => row.classList.add('row-click'));
.row{
display: flex;
flex-direction:row;
overflow: hidden;
}
.cell{
background:#f00;
color:#fff;
font-size:26px;
min-width:100px;
margin-right:12px;
animation: slide-first-desktop 10s linear infinite;
}
.row-click > .cell{
animation:
slide-first-desktop 5s cubic-bezier(1,0,.74,1),
slide-first-desktop 2s linear infinite;
}
#keyframes slide-first-desktop {
0%{
transform: translateX(0px);
}
100% {
transform: translateX(-1680px);
}
}
<div class="row">
<div class="cell">1</div>
<div class="cell">2</div>
<div class="cell">3</div>
<div class="cell">4</div>
<div class="cell">5</div>
<div class="cell">6</div>
<div class="cell">7</div>
<div class="cell">8</div>
<div class="cell">9</div>
<div class="cell">10</div>
<div class="cell">11</div>
<div class="cell">12</div>
<div class="cell">13</div>
<div class="cell">14</div>
<div class="cell">15</div>
<div class="cell">1</div>
<div class="cell">2</div>
<div class="cell">3</div>
<div class="cell">4</div>
<div class="cell">5</div>
<div class="cell">6</div>
<div class="cell">7</div>
<div class="cell">8</div>
</div>

Instead of relying on the animation-delay to queue the two animations, you can listen to animation events in JavaScript: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/animationstart_event
const animation = document.querySelector('p.animation');
animation.addEventListener('animationstart', () => {
console.log('animation started');
animation.classList.add('animation-running');
// do something
});
animation.addEventListener('animationend', () => {
console.log('animation ended');
animation.classList.remove('animation-running');
// do something
});
In the example, I'm using the events to toggle a animation-running class. You could use this to check if an animation is running and prevent to restart it on click.

Related

detect when css keyframe is done - Angular

I have an animation that last one second and I have an #InputI'm taking, but the #Input happens so fast that the animation doesn't take place. How can I know when the animation is done in order to trigger the #Input after
CSS
#keyframes bulkSlideOut {
100% {
transform: translateY(100vh);
}
}
HTML
<div *ngIf="displayBulkPay" class="bulk-pay-storage-container">
<div class="header-container">
.
.
</div>
</div>
TS
#Input()
displayBulkPay: boolean;
There is a .start and .done event in your animation triggers that you can call a function or set a value with.
<div id="whatever" [#displayBulkPay]="canDoAFunctionToo(anything)"
(#displayBulkPay.start)="onStart($event)"
(#displayBulkPay.done)="onDone($event)">

CSS not rendered on a dynamic smarty template element

I have a dynamic span with dynamic class in my smarty template. The problem here is that the CSS assigned to those classes are not rendered. Everything works if I set things to static but when changed to dynamic it will not work anymore. I'm guessing the CSS is loaded first before the elements are rendered. What would be a good workaround for this?
Smarty:
<div id="wrapper">
<div id="word">
{counter start=9 print=false}
{foreach from=$currentUser item=name}
<span class="{counter}">{$name}</span>
{/foreach}
</div>
</div>
CSS that is not rendered:
#word span.l0 {
animation-delay: 0s;
}
#word span.l1 {
animation-delay: 0.375s;
}
#word span.l2 {
animation-delay: 0.75s;
}
#word span.l3 {
animation-delay: 1.125s;
}
#word span.l4 {
animation-delay: 1.5s;
}
Try this HTML code:
<div id="wrapper">
<div id="word">
{foreach from=$currentUser item=name key=i}
<span class="span.l{i}">
{$name}
</span>
{/foreach}
</div>
</div>

Css animation queue

I have a troublesome task. Image should be hidden and button showed by default. I want to show image and hide button, but when one animation ends, secound should start. transition should peroid 1 second. How to make it?
All code:
https://jsfiddle.net/169vuuk8/
HTML code:
<div class="showSingle" itemprop="1">
<img src="https://img.thegearpage.net/board/data/avatars/m/47/47608.jpg?1487188345" alt="logo">
<button class="button">button </button>
</div>
Looks like you just need transition-delay css property for second animation: https://developer.mozilla.org/en/docs/Web/CSS/transition-delay
You can either use transitionend event in js, but it is not really needed here: https://developer.mozilla.org/en/docs/Web/Events/transitionend
Not sure what you need exactly but below the image fades in the first second and the button out after 2 seconds delay.
.showSingle img {
opacity: 0;
animation: fadeInImage 1s ease-out forwards;
}
.showSingle button {
opacity: 1;
animation: fadeOutButton 1s ease-out 2s forwards;}
#keyframes fadeInImage {
to {
opacity: 1
}
}
#keyframes fadeOutButton {
to {
opacity: 0
}
}
<div class="showSingle" itemprop="1">
<img src="https://img.thegearpage.net/board/data/avatars/m/47/47608.jpg?1487188345" alt="logo">
<button class="button">button </button>
</div>

Animation with ng-if

Am a bit stuck with getting my head around a basic fade-in and fade-out using Angularjs.
I am displaying the results of values I am returning from a database using the ng-if and want to fade in the results and fade it out upon clicking it again, based on whether this condition is met (ng-if="detail.categoryId === expense.categoryId") As I have it now it fades in and then immediately fades out again when clicking on it once.
HTML
<div class="row expenseBreakdown fade" ng-if="detail.categoryId === expense.categoryId" ng-repeat="detail in expensesDetail">
<div class="col-md-3">{{detail.expenseDetail}}</div>
<div class="col-md-3">{{detail.dateExpense|date}}</div>
<div class="col-md-3">{{detail.expenseAmount|currency:"R"}</div>
</div>
CSS
.fade.ng-enter {
transition:0.5s linear all;
opacity:0;
}
/* The finishing CSS styles for the enter animation */
.fade.ng-enter.ng-enter-active {
opacity:1;
}
You have to use ng-if inside ng-repeat like
<div class="row expenseBreakdown fade" ng-repeat="detail in expensesDetail">
<div class="fade ng-enter ng-enter-active" ng-if="detail.categoryId == expense.categoryId">
<div class="col-md-3">{{detail.expenseDetail}}</div>
<div class="col-md-3">{{detail.dateExpense|date}}</div>
<div class="col-md-3">{{detail.expenseAmount|currency:"R"}}</div>
</div>
<div class="fade ng-enter" ng-if="detail.categoryId != expense.categoryId">
<div class="col-md-3">{{detail.expenseDetail}}</div>
<div class="col-md-3">{{detail.dateExpense|date}}</div>
<div class="col-md-3">{{detail.expenseAmount|currency:"R"}}</div>
</div>
</div>

How do you make an icon change from grey to full colour on hover?

I have a grey facebook icon and a full colour facebook icon. I would like to have them on my website so that when the mouse cursor is placed over the grey icon it becomes the full colour one. How do I achieve this?
Use CSS sprites and shift position based on CSS class for element and element:hover.
Old question, new answer with an old link:
http://webdesignerwall.com/tutorials/html5-grayscale-image-hover shows a solution with jQuery, which doesn't need 2 images (so it saves time and resorces if you want to do a larger gallery)
you need to have 2 versions from the Icon, one grey and another colored, and on hover, switch:
icon
{
background-image: greyIconURL
}
icon:hover
{
background-image: coloredIconURL
}
another way and better is #Kon solution
It can be done through the filter in css.
example
/* needed code */
.employee:hover img {
filter: none;
-webkit-filter: grayscale(0);
-webkit-transform: scale(1.01);
}
.employee img {
filter: gray;
-webkit-filter: grayscale(1);
transition: all .8s ease-in-out;
width: 100%; }
/* style col -- no need */
.row {
display:flex;
}
.col-md-3{
padding:5px;
width:25%;
}
<div class="row ourTeam">
<div class="col-md-3 col-12 q-pa-md employee">
<img src="https://placeimg.com/640/480/any">
<h6>Name</h6>
<span>CEO</span>
</div>
<div class="col-md-3 col-12 q-pa-md employee">
<img src="https://placeimg.com/640/480/any">
<h6>Name</h6>
<span>CEO</span>
</div>
<div class="col-md-3 col-12 q-pa-md employee">
<img src="https://placeimg.com/640/480/any">
<h6>Name</h6>
<span>CEO</span>
</div>
<div class="col-md-3 col-12 q-pa-md employee">
<img src="https://placeimg.com/640/480/any">
<h6>Name</h6>
<span>CEO</span>
</div>
</div>
that easy!
<img src="grey.png" onmouseover="this.src='blue.png'" onmouseout="this.src='greay.png'" />
EDIT use this instead of you wish to be following rules but increasing complexities and KBs
HTML
<img id="yourImage" />
JS
document.getElementsByTagNames('body')[0].addEventListener('load', function() {
document.getElementById('yourImage').addEventListener('mouseover', function() {
document.getElementById('yourImage').src = 'color.jpg'
}, false);
document.getElementById('yourImage').addEventListener('mouseout', function() {
document.getElementById('yourImage').src = 'grey.jpg'
}, false);
}, false);
PS this uses javascript as opposed to your css tag simply because it is a good practice to use image tag wherever possible because browsers will image as an image as opposed to a div tag where they will treat it as a content block (which is not good!).

Resources