13
13

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_callbackをする

  • scroll_callbackに実行したい関数を代入する
  • scroll_callbackに、nullを代入すると止まる。

##参考

var isWin9X = (navigator.appVersion.toLowerCase().indexOf('windows 98')+1);
var isIE = (navigator.appName.toLowerCase().indexOf('internet explorer')+1?1:0);
var isOpera = (navigator.userAgent.toLowerCase().indexOf('opera')+1?1:0);
if (isOpera) isIE = false;
var isSafari = (navigator.appVersion.toLowerCase().indexOf('safari')+1?1:0);

function getScrollPositionY() {
  return document.documentElement.scrollTop || document.body.scrollTop;
}

function getScrollSizeY() {
  return document.documentElement.scrollHeight || document.body.scrollHeight;
}

function getScreenSizeY() {
  var y = new Object();
  if (!isSafari && !isOpera) {
    y = document.documentElement.clientHeight || document.body.clientHeight || document.body.scrollHeight;
  } else {
    y = window.innerHeight;
  }
  return y;
}

//上書きする
var scroll_callback = null;

//イベントリスナー
var scroll_timer;  
 window.addEventListener("scroll", function(){
 clearTimeout(scroll_timer);  
 scroll_timer = setTimeout(function(){  
 if(scroll_callback!=null){
   if( (getScrollSizeY() - getScrollPositionY() - getScreenSizeY()) < 50 ){
    scroll_callback();
   }
  }
 }, 500);  
});
13
13
1

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
13
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?