首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >波纹效应指令是全屏的,而不仅仅是在元素上

波纹效应指令是全屏的,而不仅仅是在元素上
EN

Stack Overflow用户
提问于 2018-10-27 06:23:45
回答 1查看 528关注 0票数 0

我想有我的按钮和链接的材料设计涟漪效应。

在网上搜索时,我发现了npm上的NG2-纹波指令和指令的GitHub上的要旨

不幸的是,这是不可行的,所以我修改了它,并包括了一个模块,以便我可以将它导入到我的应用程序中。

它的一半工作,因为当你点击你得到一个涟漪效应,但不只是在按钮,它是全屏。

我摆弄了css和指令,但没有成功。有人能帮忙吗?

ripple-effect.directive.ts

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
.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;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-04 16:41:37

我仍然希望通过一个指令来实现这个目标,但是由于激动,我继续我的搜索,并且能够通过CSS完成连锁反应,这要感谢CodeBurst。我现在只使用CSS就有了我想要的涟漪效果。希望其他人会发现这是有用的。

我使用的是引导带4和角6。

只需将下面的内容放入根styles.css中,并将“纹章”类添加到按钮中:

代码语言:javascript
复制
.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;
} 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53019299

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档