Created
March 31, 2018 08:35
-
-
Save naosim/9bee1004c76994752d21800777d2b09e to your computer and use it in GitHub Desktop.
Elementが画面外に出たことを検知する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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