css - 离子自定义按钮动画在所有按钮上触发
问题描述
我正在尝试在 Ionic 应用程序中实现一个动画收藏按钮,但遇到了一些问题。
该按钮在单击时按预期工作,但它也会触发所有其他按钮来播放动画。每次页面加载时,动画也会在所有按钮上播放。我怀疑我添加活动类的 ngClass 语句在页面加载帖子时以某种方式被触发,并且在添加收藏夹时从可观察到的数据更新时再次触发。
<ion-button class="favourite" fill="clear" size="small" (click)="toggleFavourite( post, user.userId )">
<ion-icon [ngClass]="{'active': post.favourites && post.favourites[user.userId] ? true : false}" class="heart"></ion-icon>
</ion-button>
CSS:
.heart {
width: 100px;
height: 100px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%) scale(0.9);
background: url('/assets/images/heart.png') no-repeat;
background-position: 0 0;
cursor: pointer;
animation: fave-heart 1s steps(28);
}
.heart.active {
background-position: -2800px 0;
transition: background 1s steps(28);
}
@keyframes fave-heart {
0% {
background-position: 0 0;
}
100% {
background-position: -2800px 0;
}
}
我做了更多的测试,发现每次更新我的帖子变量时都会触发按钮动画。这些帖子作为可观察对象返回,因此它会在页面加载时以及用户收藏帖子时更新,以便我可以更新按钮。但是,我添加了一个调用 getPosts() 函数的按钮(如下),它还会在每次单击时触发动画。
<ion-button (click)="getPosts(limit)">click</ion-button>
getPosts(limit){
return new Promise((resolve) => {
this.postsService.getPosts(limit).subscribe(data => {
this.posts = data.reverse()
let key = data[data.length - 1].$key;
if (this.lastId === key){
this.hasMoreData = false;
}
resolve(data);
});
});
}
解决方案
我做了更多的研究,并找到了一种更好的方法。这种方式使用 Ionic 5 动画,因此我可以在选择时定位已单击的元素的 id 以播放动画。
&.heart {
width: 100px;
height: 100px;
position: absolute;
transform: scale(0.9);
background: url('/assets/images/heart.png') no-repeat;
background-position: 0 0;
cursor: pointer;
}
&.heart.active {
background-position: -2800px 0;
}
<ion-col *ngFor="let post of postList ; let i = index;">
<ion-button (click)="toggleFavourite( post, user.userId, '#favourite' + i )">
<ion-icon [id]="'favourite'+i" class="heart" color="pink" [ngClass]="{'active': post.favourites && post.favourites[user.userId] ? true : false}"></ion-icon>
</ion-button>
</ion-col>
import { AnimationController } from '@ionic/angular';
constructor(private animationCtrl: AnimationController){ }
animateForward(elementId){
const animation = this.animationCtrl
.create()
.addElement(document.querySelector(elementId))
.duration(1000)
.iterations(1)
.easing( 'steps(28, end)')
.keyframes([
{ offset: 0, backgroundPosition: '0 0' },
{ offset: 1, backgroundPosition: '-2800px 0' }
])
animation.play()
}
animateBack(elementId){
const animation = this.animationCtrl
.create()
.addElement(document.querySelector(elementId))
.duration(300)
.iterations(1)
.easing( 'steps(28, end)')
.keyframes([
{ offset: 0, transform: 'scale(0.9)' },
{ offset: 0.7, transform: 'scale(1)' },
{ offset: 1, transform: 'scale(0.9)' }
])
animation.play()
}
推荐阅读
- youtube-iframe-api - 播放列表 iframe 的行为好像链接的播放列表/视频不可用
- azure - ARM 模板部署:使用 reference() 函数进行条件部署时出现 DeploymentNotFound 错误
- flutter - 在flutter中使用charts_flutter更改选择时的图表栏颜色
- sql - 为什么这两个等效查询的执行计划如此不同(内部联接+分区依据)?
- css - 动画 Bootstrap 5 下拉菜单
- javascript - 使用 React 和 Material UI 使用 Accordion 加载图像
- java - 将对象从另一个类绘制到同一个 JFrame
- java - 每次进行计算时,我如何打印出我的 RPN 计算器的堆栈?
- http - 指定 HTTP 请求的本地端点
- typescript - 如何定义柯里化函数的返回类型?