Instantly share code, notes, and snippets.

Embed
What would you like to do?
<html lang="ja">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body>
<div id="item-1" class="item" style="height: 100px; border-style: dotted">
テストだよ。
</div>
<div id="item-2" class="item" style="height: 100px; border-style: dotted">
テストだよ。
</div>
<div id="item-3" class="item" style="height: 100px; border-style: dotted">
テストだよ。
</div>
<div id="item-4" class="item" style="height: 100px; border-style: dotted">
テストだよ。
</div>
<div id="item-5" class="item" style="height: 100px; border-style: dotted">
テストだよ。
</div>
<script type="text/javascript">
( function (){
var targets;
var overpassed = function(ele, scrollTop) {
var rect = ele.getBoundingClientRect();
return rect.top < scrollTop ? true : false;
};
window.onload = function() {
targets = document.querySelectorAll(".item");
};
window.onscroll = function() {
var scrollTop = document.body.scrollTop;
var _rect;
var i;
for ( i = 0; i < targets.length; i++ ) {
if ( overpassed(targets[i], scrollTop) && !targets[i]._overpassed ) {
targets[i]._overpassed = true;
console.log(targets[i].id);
}
}
};
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment