I was trying to build a auto sliding bar from right to left with an array of data in Angular. Currrently, the animation wasn't working until user click on it. I tried changedetectorref, setTimeout() and trigger click event manually, but nothing work. Please help me on solving this.
The observation is that the data fetched from backend are received and shown (this.offer has its correct value) but the animation is paused. After several seconds, the shown data disappears by itself. When user click the blank sliding bar, the function of clicking on a data triggers and the animation starts. Please let me know if any further information is required.
HTML Part
<div class="ticker-wrap bg-black">
<div class="ticker py-2.5" id="bar-part-1">
<a class="ticker__item" [id]="'talent-' + o.uniqueCode" [href]="'/talent/' + o.uniqueCode" (click)="openTalentDetailDialog(o.uniqueCode)"
*ngFor="let o of offers">
<div class="flex-col content">
<strong>{{o.uniqueCode}}</strong>
</div>
</a>
</div>
<div class="ticker py-2.5" id="bar-part-2">
<a class="ticker__item" [href]="'/talent/' + o.uniqueCode" (click)="openTalentDetailDialog(o.uniqueCode)"
*ngFor="let o of offers">
<div class="flex-col content">
<strong>{{o.uniqueCode}}</strong>
</div>
</a>
</div>
</div>
CSS Part
$duration: 40s;
#-webkit-keyframes ticker {
0% {
-webkit-transform: translate3d(0%, 0, 0);
transform: translate3d(0%, 0, 0);
visibility: visible;
}
100% {
-webkit-transform: translate3d(-100%, 0, 0);
transform: translate3d(-100%, 0, 0);
}
}
#keyframes ticker {
0% {
-webkit-transform: translate3d(0%, 0, 0);
transform: translate3d(0%, 0, 0);
visibility: visible;
}
100% {
-webkit-transform: translate3d(-100%, 0, 0);
transform: translate3d(-100%, 0, 0);
}
}
.ticker-wrap {
// position: fixed;
z-index: 100;
// bottom: 0;
width: 100%;
overflow: hidden;
// height: 4rem;
// padding-left: 100%;
box-sizing: content-box;
white-space: nowrap;
min-height: 64px;
.ticker {
// display: inline-block;
display: inline-flex;
// height: 4rem;
// line-height: 4rem;
// white-space: nowrap;
// padding-right: 100%;
box-sizing: content-box;
-webkit-animation-iteration-count: infinite;
animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
animation-timing-function: linear;
-webkit-animation-name: ticker;
animation-name: ticker;
-webkit-animation-duration: $duration;
animation-duration: $duration;
&__item {
display: inline-block;
padding: 0 2rem;
// font-size: 2rem;
color: white;
cursor: pointer;
.content {
max-width: 450px;
min-width: 450px;
white-space: pre-wrap;
}
&:hover {
text-decoration: underline;
}
}
}
&:hover {
.ticker {
animation-play-state: paused;
}
}
}
TypeScript part
loadRecentOffers() {
console.log('loadRecentOffer is triggered');
this._businessService.getRecentOffers().subscribe(async o => {
console.log('recent offers: ', o);
await Promise.all(o.map(oo => {
oo.time = moment(new Date(oo.createdAt)).fromNow();
}));
this.offers = o;
// this._matSnackBar.open('offer applied', 'Close');
setTimeout(() => {
this._cdr.markForCheck();
this._cdr.detectChanges();
// this._matSnackBar.open('cdr triggered', 'Close');
}, 100)
})
}
Related
I am currently using some Javascript to hide and show a certain div on button click.
I want the transition to be smooth, and not so jumpy. Ideally i would like to add a little bounce effect.
Any ideas on how to achieve this?
var button = document.querySelector(".button1");
button.addEventListener("click", function() {
var content = document.querySelector(".second");
content.style.display = (content.style.display === "none") ? "block" : "none";
});
.container {
display: flex;
align-items: center;
justify-content: space-around;
width: 100%; background: red;
}
.second {
display: none;
}
<div class="container">
<div class="first">
<p> Hello world</p>
</div>
<div class="second">
<p> Goodbye World</p>
</div>
</div>
<button class="button1" >Apply</button>
One of a million equally beautiful options! )))
var first = document.querySelector(".first");
var second = document.querySelector(".second");
var rectSecond = second.getBoundingClientRect();
first.style.width = `${rectSecond.width}px`;
btn.addEventListener("click", () => {
first.classList.toggle("animateFirst");
second.classList.toggle("animateSecond");
});
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
width: 100%;
height: 3rem;
background: red;
position: relative;
}
.container div {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.first {
background: red;
text-align: center;
}
.animateFirst {
animation: move1 0.8s ease-out forwards;
}
#keyframes move1 {
from {
left: 50%;
}
50% {
left: 10%;
}
75% {
left: 16%;
}
to {
left: 12%;
}
}
.animateSecond {
animation: move2 0.8s ease-out forwards;
}
#keyframes move2 {
from {
left: 50%;
}
50% {
left: 82%;
}
75% {
left: 76%;
}
to {
left: 80%;
}
}
</style>
<div class="container">
<div class="second">
<p>Goodbye World</p>
</div>
<div class="first">
<p>Hello world</p>
</div>
</div>
<button class="button1" id="btn">Apply</button>
To make CSS transition to make the transition smooth and add a bounce effect. You need to some changes to your code then you can make it very easily. Here you go -
.second {
display: none;
transition: all 0.3s ease-in-out;
}
#keyframes bounce {
0%, 20%, 50%, 80%, 100% {
transform: translateY(0);
}
40% {
transform: translateY(-30px);
}
60% {
transform: translateY(-15px);
}
}
.second.bouncing {
animation: bounce 0.5s;
}
Please add a class bouncing to the .second div in your JavaScript code.
var button = document.querySelector(".button1");
button.addEventListener("click", function() {
var content = document.querySelector(".second");
content.style.display = (content.style.display === "none") ? "block" : "none";
content.classList.toggle("bouncing");
});
I hope this will make the transition smooth, and the div will have a bouncing effect when it's displayed. Thank you
You cannot add animation from hidden state.
You need to first add element in layout.
var button = document.querySelector(".button1");
var content = document.querySelector(".second");
button.addEventListener("click", function () {
content.hidden = !content.hidden
});
.container {
display: flex;
align-items: center;
justify-content: space-around;
width: 100%;
background: red;
}
.second {
transform-origin: center bottom;
animation: bounce 1000ms ease-out forwards;
}
#keyframes bounce {
from,
20%,
53%,
to {
animation-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
transform: translate3d(0, 0, 0);
}
40%,
43% {
animation-timing-function: cubic-bezier(0.755, 0.05, 0.855, 0.06);
transform: translate3d(0, -30px, 0) scaleY(1.1);
}
70% {
animation-timing-function: cubic-bezier(0.755, 0.05, 0.855, 0.06);
transform: translate3d(0, -15px, 0) scaleY(1.05);
}
80% {
transition-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
transform: translate3d(0, 0, 0) scaleY(0.95);
}
90% {
transform: translate3d(0, -4px, 0) scaleY(1.02);
}
}
<div class="container">
<div class="first">
<p> Hello world</p>
</div>
<div class="second" hidden>
<p> Goodbye World</p>
</div>
</div>
<button class="button1">Apply</button>
Code is below - I'm trying to get this to loop smoothly from the last headline - currently there is a gap and I can't figure out how to loop the headlines without it :/. The current functionality is good, but the gap at the end of the 4 headlines (NB - eventually there will be many more headlines) is really bugging me. Any help would be awesome
<style>
.box{
width: 80%;
}
* {
box-sizing: border-box;
}
#-webkit-keyframes ticker {
0% {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
visibility: visible;
}
100% {
-webkit-transform: translate3d(-100%, 0, 0);
transform: translate3d(-100%, 0, 0);
}
}
#keyframes ticker {
0% {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
visibility: visible;
}
100% {
-webkit-transform: translate3d(-100%, 0, 0);
transform: translate3d(-100%, 0, 0);
}
}
.ticker-wrap {
position: relative;
left: 0;
width: 100%;
overflow: hidden;
height: 3rem;
background-color: #000;
box-sizing: content-box;
}
.ticker-wrap .ticker {
display: inline-block;
height: 3rem;
line-height: 3rem;
white-space: nowrap;
padding-right: 10%;
box-sizing: content-box;
-webkit-animation-iteration-count: infinite;
animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
animation-timing-function: linear;
-webkit-animation-name: ticker;
animation-name: ticker;
-webkit-animation-duration: 25s;
animation-duration: 25s;
}
.ticker-wrap .ticker__item {
display: inline-block;
padding: 0 2rem;
font-size: 2rem;
color: white;
}
</style>
<div class="rt-container">
<div class="col-rt-12">
<div class="Scriptcontent">
<div class="ticker-wrap">
<div class="ticker">
<div class="ticker__item"><h2></h2></div>
<div class="ticker__item"><h2>Massi out in FIA Race Director Shake-up</h2></div>
<div class="ticker__item"></div><div class="ticker__item"><h2>Ferrari Unveil Bold Design For 2022 Car.</h2></div>
<div class="ticker__item"></div><div class="ticker__item"><h2>Sprint Races Cut From 6 to 3 Due to Cost Cap Dispute.</h2></div>
<div class="ticker__item"></div><div class="ticker__item"><h2>Szafnauer named Alpine Team Principal as Rossi confirms new structure.</h2></div>
</div>
</div>
</div>
</div>
</div>
i have set animation in property on HTML element when it is displayed, i want same animation on reverse, when button is clicked it should hide with same animation.
how can i achieve that.
"Animation in" which is working:
.modal{
animation: myAnim 1s ease 0s 1 normal forwards;
}
#keyframes myAnim {
0% {
opacity: 0;
transform: translateX(-50px);
}
100% {
opacity: 1;
transform: translateX(0);
}
}
HTML:
<div id="myModal" class="modal fixed hidden z-1 w-full h-full bg-red-100 h-screen ">
<button id="myBtn" class=" p-2 bg-yellow-400 rounded m-4 absolute right-0 text-xs font-bold ">
HIDE/SHOW..
</button>
i am showing/hidding modal using JS. how can i hide modal with same animation.
Create two classes, one for making the modal appear and another for making it disappear,
Class for appearing
.modal-appr{
animation: appr 1s ease 0s 1 normal forwards;
}
#keyframes appr {
0% {
opacity: 0;
transform: translateX(-50px);
}
100% {
opacity: 1;
transform: translateX(0);
}
}
Class to make the modal disappear
.modal-disappr{
animation: disappr 1s ease 0s 1 normal forwards;
}
#keyframes disappr {
0% {
opacity: 1;
transform: translateX(0);
}
100% {
opacity: 0;
transform: translateX(-50px);
}
}
now just use Javascript to add this classes to the modal's classlist when the button toggles
const btn = document.querySelector('mybtn');
const modal = document.querySelector('#myModal');
btn.addEventListener ('click', function toggleClass () {
if(modal.classList.contains('appr')) {
modal.classList.add('disappr');
modal.classList.remove('appr');
} else {
modal.classList.add('appr');
modal.classList.remove('disappr');
}
})
You can achieve this by using toggle class and transition instead.
let btn = document.getElementById('model_btn');
let model = document.getElementById('model');
btn.addEventListener('click', function(){
model.classList.toggle('show');
})
/* Styling the markup */
#model{
width: 200px;
height: 100px;
background-color: yellow;
padding: 20px;
margin-bottom: 20px;
}
#model_btn{
border: none;
background-color: green;
padding: 10px;
color: white;
cursor: pointer;
}
/* Showing and hiding animation with transition */
#model{
position: relative;
visibility: none;
opacity: 0;
top: 0;
left: 0;
transform: translateX(-50px);
transition: all 1s ease;
}
#model.show{
visibility: visible;
opacity: 1;
transform: translateX(0px);
}
<div id="model">
Content
</div>
<button id="model_btn">Show/Hide Model</button>
Animation / Key-frame approach.
let btn = document.getElementById('model_btn');
let model = document.getElementById('model');
btn.addEventListener('click', function(){
model.classList.toggle('show');
})
/* Styling the markup */
#model{
width: 200px;
height: 100px;
background-color: yellow;
padding: 20px;
margin-bottom: 20px;
visibility: none;
opacity: 0;
transform: translateX(-50px);
}
#model_btn{
border: none;
background-color: green;
padding: 10px;
color: white;
cursor: pointer;
}
.show{
animation: myAnim 1s ease 0s 1 normal forwards;
}
#keyframes myAnim {
0% {
visibility: none;
opacity: 0;
transform: translateX(-50px);
}
100% {
visibility: visible;
opacity: 1;
transform: translateX(0);
}
}
<div id="model">
Content
</div>
<button id="model_btn">Show/Hide Model</button>
I have a div element that I have hide by applying .intro class. Now I want to show this hidden div along with animation on scroll bar Scrolling. I have defined my own animation within fadeInUp class. Now if I try to remove .intro class, the div will animate after being showing. But I don't want this. I just want div should show along with animation. The animation should not show after being showing a div.
Here is my jquery code
$(document).ready(function(){
$(document).scroll(function() {
var y = $(this).scrollTop();
if (y > 100) {
$('#demo').removeClass('intro');
$('#demo').addClass('fadeInUp');
}
});
My CSS is something like this
.intro{
display: none;
}
#-webkit-keyframes fadeInUp {
0% {
opacity: 0;
-webkit-transform: translate3d(0,100%,0);
transform: translate3d(0,100%,0)
}
100% {
opacity: 1;
-webkit-transform: none;
transform: none
}
}
#keyframes fadeInUp {
0% {
opacity: 0;
-webkit-transform: translate3d(0,100%,0);
-ms-transform: translate3d(0,100%,0);
transform: translate3d(0,100%,0)
}
100% {
opacity: 1;
-webkit-transform: none;
-ms-transform: none;
transform: none
}
}
.fadeInUp {
visibility: visible;
animation-duration: 9s;
animation-delay: 2s;
-webkit-animation-name: fadeIUp;
animation-name: fadeInUp;
}
HTML is something like this
<div id="demo" class=" intro" style="height:100px;width: 100px; background-color:red"></div>
Edit: We had to do three things to solve this issue-
Put animation-fill-mode: both; in the fadeInUp class.
Change the name of the animation as there was a naming conflict: animation-name and the class name were the same.
Some tiny changes in the JS part to select appropriate classes (check below code).
Working demo & Code!
HTML
<div id="demo" class="intro redbox"></div>
CSS
body {
height: 1600px;
}
.intro{
opacity:0;
}
.redbox {
position: fixed;
height: 100px;
width: 100px;
background-color: red;
}
#-webkit-keyframes fadeInUpA {
0% {
opacity: 0;
-webkit-transform: translate3d(0, 100%, 0);
transform: translate3d(0, 100%, 0);
}
100% {
opacity: 1;
-webkit-transform: none;
transform: none;
}
}
#keyframes fadeInUpA {
0% {
opacity: 0;
-webkit-transform: translate3d(0, 100%, 0);
-ms-transform: translate3d(0, 100%, 0);
transform: translate3d(0, 100%, 0);
}
100% {
opacity: 1;
-webkit-transform: none;
-ms-transform: none;
transform: none;
}
}
.fadeInUp {
visibility: visible;
animation-duration: 1s;
animation-delay: 0s;
-webkit-animation-name: fadeInUpA;
animation-name: fadeInUpA;
animation-fill-mode: both;
}
JS
myID = document.getElementById("demo");
$(document).ready(function(){
$(document).scroll(function() {
var y = $(this).scrollTop();
if (y < 200) {
myID.className = "redbox intro"
}
else{
myID.className = "redbox fadeInUp"
}
});
});
I am displaying div from right to left and after 5 sec it will hide. I tried some code right to left is working but after 5 sec it's not hiding.
I tried opacity:0 inside keyframe but then my animation not working.
Would you help me out in this?
.successAlet {
background-color: red;
color: #fff;
position: fixed;
top: 0;
width: 400px;
right: 0;
z-index: 1001;
-webkit-animation-name: fadeInRight;
animation-name: fadeInRight;
-webkit-animation-duration: 1s;
animation-duration: 1s;
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
}
#-webkit-keyframes fadeInRight {
0% {
opacity: 0;
-webkit-transform: translate3d(100%, 0, 0);
transform: translate3d(100%, 0, 0);
}
100% {
opacity: 1;
-webkit-transform: none;
transform: none;
}
}
#keyframes fadeInRight {
0% {
opacity: 0;
-webkit-transform: translate3d(100%, 0, 0);
transform: translate3d(100%, 0, 0);
}
100% {
opacity: 1;
-webkit-transform: none;
transform: none;
}
}
<div class="successAlet">
<h2>Animate and then autohide in 5 sec</h2>
</div>
Consider a second animation. You can also simplify your code by removing a lot of non needed properties
.successAlet {
background-color: red;
color: #fff;
position: fixed;
top: 0;
width: 400px;
right: 0;
z-index: 1001;
animation-name: fadeInRight,hide;
animation-duration: 1s;
animation-delay: 0s,3s;
animation-fill-mode: both;
}
#keyframes fadeInRight {
0% {
opacity: 0;
transform: translate3d(100%, 0, 0);
}
}
#keyframes hide {
100% {
opacity: 0;
}
}
<div class="successAlet">
<h2>Animate and then autohide in 5 sec</h2>
</div>
for smooth hide
#keyframes hide {
100% {
opacity: 1;
width: 0;
}
}