Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 70434

Blured background effect

$
0
0

@pabloo87 wrote:

Hi, the other day was a bit bored so i made this effect to make the background blur while showing the menu and i wanted to share it with the community.

ezgif-3-f597c30ad7dc
It’s a subtle effect, i don’t know if it can be appreciated in a low resolution gif, sorry.

Anyways, in case it fits your project you can follow this steps:

Generate a new directive: ionic generate directive blur-menu

Then paste this code to blur-menu-directive.ts

import { Directive, ElementRef, HostListener, Input } from '@angular/core';
import { Platform } from '@ionic/angular';

@Directive({
  selector: '[appBlurMenu]'
})
export class BlurMenuDirective {
  @Input('appBlurMenu') blurLevel;

  constructor(private el: ElementRef, private platform: Platform) {
    const platforms = platform.platforms();
    const applePlatforms = ['ios', 'iphone', 'ipad'];
    const apple = applePlatforms.some(p => platforms.includes(p)) ? true : false;
    el.nativeElement.style.transition = apple ? '0.4s backdrop-filter' : '0.3s backdrop-filter';
  }

  @HostListener('ionWillOpen') ionWillOpen() {
    this.setBackdropFilter(`blur(${this.blurLevel.toString() || '2'}px)`);
  }

  @HostListener('ionWillClose') ionWillClose() {
    this.setBackdropFilter('None');
  }

  setBackdropFilter(value) {
    this.el.nativeElement.style.backdropFilter = value;
  }
}

Add the appBlurMenu directive to ion-menu like this

<ion-menu contentId="main-content" type="overlay" appBlurMenu>

You may also adjust the effect level like this: (default value is 2)

<ion-menu contentId="main-content" type="overlay" appBlurMenu="5">

It’s a very simple code but i hope some of you may use it!

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 70434

Trending Articles