How to add ripple effect to an ionic 2 element?

2019-04-19 09:50发布

Some elements, like the button, have native ripple effect on Ionic 2. Others like cards doesn't. How could I add support to ripple effect on an arbitrary Ionic 2 element?

5条回答
我想做一个坏孩纸
2楼-- · 2019-04-19 10:01

As I see by sources of Ionic v3.3, a container element must contain an empty div element with button-effect class, also the container must have tappable attribute and be styled as position: relative; overflow: hidden.

In my project I use a following directive to style button-like containers:

import {Directive, ElementRef, Host, Renderer2} from '@angular/core';

@Directive({
    selector: '[m-ripple-effect]',
    host: {
        'tappable': '',
        'role': 'button',
        'style': 'position: relative; overflow: hidden'
    }
})
export class RippleEffectDirective {
    constructor(@Host() host: ElementRef, renderer: Renderer2) {
        const div = renderer.createElement('div');
        renderer.addClass(div, 'button-effect');
        renderer.appendChild(host.nativeElement, div);
    }
}
查看更多
爷的心禁止访问
3楼-- · 2019-04-19 10:03

Try wrapping the content like this:

<button ion-item>
   <ion-item style="background: rgba(0,0,0,0);">Content</ion-item>
</button>
查看更多
狗以群分
4楼-- · 2019-04-19 10:10

For IONIC 4 you can now use like this:

Make sure you div position is relative.

 <div
  ion-activatable
  class="ion-activatable"
  style="position:relative;height:100px;width:100%;background:blue;">
  <ion-ripple-effect></ion-ripple-effect>
  Content here...
</div>

:)

查看更多
干净又极端
5楼-- · 2019-04-19 10:17

You need to use the button element and you can still have the ion-item directive:

From:

<ion-item (click)="viewArticle()"></ion-item>

To:

<button ion-item (click)="viewArticle()"></button>
查看更多
迷人小祖宗
6楼-- · 2019-04-19 10:20

A more complete example based on the answer of Bartosz Kosarzycki:

                <ion-list>
                      <button ion-button style="height: auto!important;width: 100%" clear >
                            <ion-item style="background: rgba(0,0,0,0);">
                                  <ion-icon name="car" item-left></ion-icon>
                                  <h1>Title 1</h1>
                                  <p>Subtítulo</p>
                                  <ion-icon name="person" item-right></ion-icon>
                            </ion-item>
                      </button>
                      <button ion-button style="height: auto!important;width: 100%" clear>
                            <ion-item style="background: rgba(0,0,0,0);">
                                  <ion-icon name="person" item-left></ion-icon>
                                  <h1>Title 2</h1>
                                  <p>Subtítulo</p>
                                  <ion-icon name="car" item-right></ion-icon>
                            </ion-item>
                      </button>
                </ion-list>
查看更多
登录 后发表回答