Ionic 2:在iOS上滚动时运行功能

时间:2017-06-12 16:37:50

标签: javascript ios ionic-framework scroll ionic2

我正在尝试在滚动时执行某些功能。基本上,如果满足某个滚动位置,我希望应用程序做一些事情。

这里有一些代码:

  ionViewDidLoad() {
    this.content.ionScroll.subscribe((event) => {
      this.scrollPosition = event.scrollTop;
      if(this.scrollPosition >= 100){
        console.log("more than 100");
      }
      else {
        console.log("less than 100");
      }
    });
  }

它在Web浏览器或Android设备上按预期工作,它在滚动条件下重复运行“console.log()”。 在iOS上,它等待滚动停止,然后它执行console.log()。 我正在阅读在iOS上,由于性能原因,javascript会在滚动时停止。有解决方法吗?

1 个答案:

答案 0 :(得分:0)

Safari doesnt fire a scroll event until after the scroll is complete, so you'd have to do some custom event listening in this case.

Check out this post: javascript scroll event for iPhone/iPad?

It could be achieved like so:

document.addEventListener("touchmove", ScrollStart, false);
document.addEventListener("scroll", Scroll, false);

function ScrollStart() {
    //start of scroll event for iOS
}

function Scroll() {
    //end of scroll event for iOS
    //and
    //start/end of scroll event for other browsers
}

I would recommend creating a directive, using HostListener to listen for those two events.