Ionic2 Sticky 粘附效果

ionic2中实现sticky功能,在ios中使用的是position:sticky属性,但是这个属性在大部分的浏览器中并不支持,android浏览器也一样不支持。在安卓中使用fixed属性。
我在做这个功能的时候花了大量的时间,主要还是因为对ionic2的不熟悉造成的。下面给出我的爬坑之路。
第一坑:使用Directive
理论上来说使用directive是最好的方法,但是本人实力有限实现不了。
首先执行:ionic g directive stick,会在项目中生成一个components文件夹,里面有一个stick.ts文件:

import { Directive } from '@angular/core';

/*
  Generated class for the Stick directive.

  See https://angular.io/docs/ts/latest/api/core/index/DirectiveMetadata-class.html
  for more info on Angular 2 Directives.
*/
@Directive({
  selector: '[stick]' // Attribute selector
})
export class Stick {

  constructor() {
    console.log('Hello Stick Directive');
  }

}

具体用法<element stick></element>
在使用directive的时候注意不能在page对应的ts文件中通过directives:[Stick]来使用,而是应该在app.modules.ts中去声明

@NgModule({
  declarations: [
    MyApp,
    Stick
  ]
  })

然后就可以在所有页面去应用这个directive了。
第二坑:在ionic2中使用原生js进行scroll的监听
1、使用angular2提供的HostListener

      @HostListener('window:scroll',['$event'])
        handleScrollEvent(e) {
            console.log('scrool');
        }
       //这种方式监听不到scroll事件但是可以监听到click事件

2、使用window.onscroll

window.onscroll = (e)=>{
console.log('scrool');
}
//这样的方式也监听不到滚动事件

3、使用document.addEventListener

document.addEventListener('scroll', (e) => {
console.log('scrool');
}, true);
这种方式是可以监听到滚动事件的,但是在这里面没有办法获取到滚动时距顶部的距离,也没有办法使用

最终在多番尝试之后使用的是angular2提供的addScrollListener方法


import { Component, ViewChild, ElementRef,Renderer } from '@angular/core';
import { NavController, MenuController,Content } from 'ionic-angular';

@ViewChild(Content) content:Content;
@ViewChild('item') stick;

  ionViewDidLoad() {
   this.content.addScrollListener((event)=>{
    let top = event.target.scrollTop;
    let domTop = this.stick._elementRef.nativeElement.offsetTop;
    if(top>domTop){
   //this.render.setElementClass(this.stick._elementRef.nativeElement,'stick-item',true)
   this.stick._elementRef.nativeElement.classList.add('stick-item')
    }else{
      this.stick._elementRef.nativeElement.classList.remove('stick-item')
    }
   })
   }

使用以下的方法即可实现滚动的时候获取距离顶部的距离了。但是要使用这个方法,必须在页面对应的ts文件中,而不能写成directive,但是按道理是可以写进directive的,但是本人水平有限,望高人不吝赐教。
但是在ionic rc.4中addScrollListener又不适用了,而是需要使用以下方法,

this.content.ionScroll.subscribe(($event: any) => { 
        let top = $event.scrollTop;
 }

然后添加stick-item这个class后就可以写相应的样式了

--scss文件---
    ion-content {
        ion-list {
            ion-item-divider.stick-item {
                background: red;
                position: fixed;
                top: 44px;
            }
        }
    }
<ion-content padding>
    <ion-list>
        <ion-item-divider #item>Item1</ion-item-divider>
        <ion-item *ngFor="let item1 of items1">
            {{item1.name}}
        </ion-item>

        <ion-item-divider>Item2</ion-item-divider>

        <ion-item *ngFor="let item2 of items2">
            {{item2.name}}
        </ion-item>
    </ion-list>
</ion-content>

这里使用#item标识然后用@ViewChild获取。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Ionic是一个基于Angular2的开发手机web app的框架,它包含了一整套手机端的样式组件,和一系列的功能...
    王兆龙阅读 4,954评论 1 1
  • 痛点在于开发环境吧,base app的下载和gradle编译都需要较好的外网环境 环境准备 npm install...
    charles0427阅读 8,798评论 0 16
  • 0 开始之前 通过本教程之前,您应该至少了解一些基本的Ionic 2概念。您还必须已经安装了Ionic 2 在您的...
    孙亖阅读 5,561评论 2 10
  • 基于ionic2的跨平台项目(iOS) 一、技术背景 为了开发html5,除了最新使用React Native等之...
    庆华_8f67阅读 5,262评论 1 3
  • 巴塞罗那并不是一个我喜欢的城市,追忆旅程难忘的只有高迪,追忆高迪难忘的只有门把手、扶梯栏和教堂力学模型。今天偶尔看...
    呆梨羊阅读 3,199评论 0 1

友情链接更多精彩内容