6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

スマートフォンのブラウザで、スクロールのタイミングで何かする

Posted at

単にscrollイベントにbindしただけでは、iOSではスクロール終了後に1度発火するだけなので注意。
また、Androidだと頻度と処理の内容によってはスクロールがカクついたりでいろいろと工夫が必要。

以上のことをふまえ、iOSではtouchmoveにbindし、Androidはscrollイベントで直接実行するのではなく、timerを併用して実行頻度を抑える。

var pageScrollEventQueue = [];
function executeOnPageScroll(f) {
  if (isIOS()) {
    $(window).bind('touchmove', f);
  } else {
    pageScrollEventQueue.push(f);
  }
}
(function() {
  var scrollTop = $(window).scrollTop();
  var executeOnPageScrollEvent = function() {
    var current = $(window).scrollTop();
    if (scrollTop != current) {
      //console.log('executeOnPageScrollEvent');
      for (var i = 0; i < pageScrollEventQueue.length; i++) {
        pageScrollEventQueue[i]();
      }
    }
    setTimeout(executeOnPageScrollEvent, 250);
    scrollTop = current;
  }
  setTimeout(executeOnPageScrollEvent, 250);
})();
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?