我想有我的按钮和链接的材料设计涟漪效应。
在网上搜索时,我发现了npm上的NG2-纹波指令和指令的GitHub上的要旨。
不幸的是,这是不可行的,所以我修改了它,并包括了一个模块,以便我可以将它导入到我的应用程序中。
它的一半工作,因为当你点击你得到一个涟漪效应,但不只是在按钮,它是全屏。
我摆弄了css和指令,但没有成功。有人能帮忙吗?
ripple-effect.directive.ts
import { Directive, HostListener, HostBinding, Input } from '@angular/core';
import * as htmlElementStringify from 'html-element-stringify';
@Directive({
selector: 'button[appRipple], a[appRipple]'
})
export class RippleEffectDirective {
isRippling = false;
@Input() appRipple = '#fff';
@HostBinding('class.ripple') public ripple = 'true';
@HostBinding('style.position') public position = '"relative"';
@HostBinding('style.outline') public outline = '"none"';
@HostListener('click', ['$event']) onClick(evt: MouseEvent) {
evt.preventDefault();
if (this.isRippling) {
return;
}
this.isRippling = true;
const button: any = evt.target;
const div = document.createElement('div');
const xPos = evt.pageX - button.offsetLeft;
const yPos = evt.pageY - button.offsetTop;
div.classList.add('ripple-effect');
div.style.height = button.clientHeight;
div.style.width = button.clientHeight;
div.style.top = (yPos - (button.clientHeight / 2)) + 'px';
div.style.left = (xPos - (button.clientWidth / 2)) + 'px';
div.style.background = this.appRipple;
button.insertAdjacentHTML('beforeend', htmlElementStringify(div));
window.setTimeout(() => {
button.removeChild(button.querySelector('.ripple-effect'));
this.isRippling = false;
}, 3000);
}
}ripple-effect.directive.module.ts
import { NgModule } from '@angular/core';
import { RippleEffectDirective } from './ripple-effect.directive';
@NgModule({
imports: [],
declarations: [RippleEffectDirective],
exports: [RippleEffectDirective],
})
export class RippleEffectDirectiveModule {
static forRoot() {
return {
ngModule: RippleEffectDirectiveModule,
providers: [],
};
}
}ripple-effect.css
.ripple {
overflow: hidden;
}
.ripple .ripple-effect {
position: absolute;
border-radius: 50%;
width: 50px;
height: 50px;
background: white;
animation-name: ripple-animation;
animation-duration: 2s;
}
@keyframes ripple-animation {
from {
transform: scale(1);
opacity: 0.4;
}
to {
transform: scale(100);
opacity: 0;
}
}发布于 2018-11-04 16:41:37
我仍然希望通过一个指令来实现这个目标,但是由于激动,我继续我的搜索,并且能够通过CSS完成连锁反应,这要感谢CodeBurst。我现在只使用CSS就有了我想要的涟漪效果。希望其他人会发现这是有用的。
我使用的是引导带4和角6。
只需将下面的内容放入根styles.css中,并将“纹章”类添加到按钮中:
.ripple:after {
content: "";
display: block;
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
pointer-events: none;
background-image: radial-gradient(circle, #555 10%, transparent 10.01%);
background-repeat: no-repeat;
background-position: 50%;
transform: scale(10, 10);
opacity: 0;
transition: transform .5s,
opacity 1s;
}
.ripple:active:after {
transform: scale(0, 0);
opacity: .3;
transition: 0s;
} https://stackoverflow.com/questions/53019299
复制相似问题