菜鸟教程小白 发表于 2022-12-11 19:51:18

javascript - ionic 2 : Run functions while scrolling on iOS


                                            <p><p>我试图在滚动时执行一些功能。基本上,如果遇到某个滚动位置,我希望应用程序做一些事情。</p>

<p>这里是一些代码:</p>

<pre><code>ionViewDidLoad() {
    this.content.ionScroll.subscribe((event) =&gt; {
      this.scrollPosition = event.scrollTop;
      if(this.scrollPosition &gt;= 100){
      console.log(&#34;more than 100&#34;);
      }
      else {
      console.log(&#34;less than 100&#34;);
      }
    });
}
</code></pre>

<p>它在网络浏览器或 Android 设备上按预期工作,它在滚动时重复运行条件内的“console.log()”。
相反,在 iOS 上,它等待滚动停止,然后执行 console.log()。
出于性能原因,我正在阅读 iOS 上的 javascript 在滚动时停止。有解决办法吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>Safari 在滚动完成之前不会触发滚动事件,因此在这种情况下您必须进行一些自定义事件监听。 </p>

<p>查看这篇文章:<a href="https://stackoverflow.com/questions/2863547/javascript-scroll-event-for-iphone-ipad" rel="noreferrer noopener nofollow">javascript scroll event for iPhone/iPad?</a> </p>

<p>可以这样实现:</p>

<pre><code>document.addEventListener(&#34;touchmove&#34;, ScrollStart, false);
document.addEventListener(&#34;scroll&#34;, 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
}
</code></pre>

<p>我建议创建一个指令,使用 HostListener 来监听这两个事件。 </p></p>
                                   
                                                <p style="font-size: 20px;">关于javascript -ionic2 : Run functions while scrolling on iOS,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/44504515/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/44504515/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: javascript - ionic 2 : Run functions while scrolling on iOS