Skip to content

Instantly share code, notes, and snippets.

@naosim
Created March 31, 2018 08:35
Show Gist options
  • Save naosim/9bee1004c76994752d21800777d2b09e to your computer and use it in GitHub Desktop.
Save naosim/9bee1004c76994752d21800777d2b09e to your computer and use it in GitHub Desktop.
Elementが画面外に出たことを検知する
class DisplayOutMonitor {
constructor(option) {
option = option || {}
this.document = option.document || document
this.interval = option.interval || 100
}
/**
* @param {string}
* @param {(isOut:boolean)=>void}
*/
register(selector, cb) {
var target = this.document.querySelector(selector);
var isOut = false;
setInterval(()=>{
var p = target.getBoundingClientRect().top + target.getBoundingClientRect().height;
if(p <= 0 && !isOut) {
cb(isOut = true);
}
if(p > 0 && isOut) {
cb(isOut = false);
}
}, this.interval);
}
}
const displayOutMonitor = new DisplayOutMonitor()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment