【问题标题】:Ionic 2: How to make a sliding menu from the bottomIonic 2:如何从底部制作滑动菜单
【发布时间】:2017-08-05 06:08:03
【问题描述】:

嗯,我不确定它是否完全被视为滑动菜单。它不是用于导航,它会保存数据。我的灵感来自 Apple Maps:

所以我希望能够根据需要上下滑动它,并能够滚动浏览第一张图片中显示的内容。另外,有没有什么方法可以用贴图实现漫射光/模糊效果?

【问题讨论】:

    标签: angular typescript ionic2 ionic3


    【解决方案1】:

    Josh Morony 有一篇很棒的帖子,内容与here 非常相似。结果是这样的:

    Here你可以找到源代码以及如何使用它。最相关的部分是:

    组件代码:

    import { Component, Input, ElementRef, Renderer } from '@angular/core';
    import { Platform, DomController } from 'ionic-angular';
    
    @Component({
      selector: 'content-drawer',
      templateUrl: 'content-drawer.html'
    })
    export class ContentDrawer {
    
      @Input('options') options: any;
    
      handleHeight: number = 50;
      bounceBack: boolean = true;
      thresholdTop: number = 200;
      thresholdBottom: number = 200;
    
      constructor(public element: ElementRef, public renderer: Renderer, public domCtrl: DomController, public platform: Platform) {
    
      }
    
      ngAfterViewInit() {
    
        if(this.options.handleHeight){
          this.handleHeight = this.options.handleHeight;
        }
    
        if(this.options.bounceBack){
          this.bounceBack = this.options.bounceBack;
        }
    
        if(this.options.thresholdFromBottom){
          this.thresholdBottom = this.options.thresholdFromBottom;
        }
    
        if(this.options.thresholdFromTop){
          this.thresholdTop = this.options.thresholdFromTop;
        }
    
        this.renderer.setElementStyle(this.element.nativeElement, 'top', this.platform.height() - this.handleHeight + 'px');
        this.renderer.setElementStyle(this.element.nativeElement, 'padding-top', this.handleHeight + 'px');
    
        let hammer = new window['Hammer'](this.element.nativeElement);
        hammer.get('pan').set({ direction: window['Hammer'].DIRECTION_VERTICAL });
    
        hammer.on('pan', (ev) => {
          this.handlePan(ev);
        });
    
      }
    
      handlePan(ev){
    
        let newTop = ev.center.y;
    
        let bounceToBottom = false;
        let bounceToTop = false;
    
        if(this.bounceBack && ev.isFinal){
    
          let topDiff = newTop - this.thresholdTop;
          let bottomDiff = (this.platform.height() - this.thresholdBottom) - newTop;      
    
          topDiff >= bottomDiff ? bounceToBottom = true : bounceToTop = true;
    
        }
    
        if((newTop < this.thresholdTop && ev.additionalEvent === "panup") || bounceToTop){
    
          this.domCtrl.write(() => {
            this.renderer.setElementStyle(this.element.nativeElement, 'transition', 'top 0.5s');
            this.renderer.setElementStyle(this.element.nativeElement, 'top', '0px');
          });
    
        } else if(((this.platform.height() - newTop) < this.thresholdBottom && ev.additionalEvent === "pandown") || bounceToBottom){
    
          this.domCtrl.write(() => {
            this.renderer.setElementStyle(this.element.nativeElement, 'transition', 'top 0.5s');
            this.renderer.setElementStyle(this.element.nativeElement, 'top', this.platform.height() - this.handleHeight + 'px');
          });
    
        } else {
    
          this.renderer.setElementStyle(this.element.nativeElement, 'transition', 'none');
    
          if(newTop > 0 && newTop < (this.platform.height() - this.handleHeight)) {
    
            if(ev.additionalEvent === "panup" || ev.additionalEvent === "pandown"){
    
              this.domCtrl.write(() => {
                this.renderer.setElementStyle(this.element.nativeElement, 'top', newTop + 'px');
              });
    
            }
    
          }
    
        }
    
      }
    
    }
    

    查看:

    <ion-content>
        <ng-content></ng-content>
    </ion-content>
    

    样式:

    .ios, .md {
    
        content-drawer {
    
            width: 100%;
            height: 100%;
            position: absolute;
            z-index: 10 !important;
            box-shadow: 0px -4px 22px -8px rgba(0,0,0,0.75);
    
        }
    
    }
    

    然后像这样在您的页面中使用它:

    主页:

    import { Component } from '@angular/core';
    import { NavController } from 'ionic-angular';
    
    @Component({
      selector: 'page-home',
      templateUrl: 'home.html'
    })
    export class HomePage {
    
        drawerOptions: any;
    
        constructor(public navCtrl: NavController) {
    
            this.drawerOptions = {
                handleHeight: 50,
                thresholdFromBottom: 200,
                thresholdFromTop: 200,
                bounceBack: true
            };
    
        }
    
    }
    

    查看:

    <ion-header>
      <ion-navbar>
        <ion-title>
          Ionic Blank
        </ion-title>
      </ion-navbar>
    </ion-header>
    
    <ion-content padding>
      The world is your oyster.
      <p>
        If you get lost, the <a href="http://ionicframework.com/docs/v2">docs</a> will be your guide.
      </p>
    </ion-content>
    
    <content-drawer [options]="drawerOptions">
        <div class="content">
          The world is your oyster.
          <p>
            If you get lost, the <a href="http://ionicframework.com/docs/v2">docs</a> will be your guide.
          </p>
        </div>
    </content-drawer>
    

    样式:

    .ios, .md {
    
        page-home {
    
            content-drawer {
                background-color: #34495e !important;
            }
    
            content-drawer .content {
                padding: 20px;
            }
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-31
      • 1970-01-01
      相关资源
      最近更新 更多