Skip to content

Instantly share code, notes, and snippets.

@aloerina01
Last active October 3, 2016 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aloerina01/d4c5314cd9166a7b5a860f31d299dbf6 to your computer and use it in GitHub Desktop.
Save aloerina01/d4c5314cd9166a7b5a860f31d299dbf6 to your computer and use it in GitHub Desktop.
スクロールインイベントを一度だけキャッチする
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="./main.css" />
</head>
<body>
<div class="area">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div id="target" class="box">6</div>
<div class="box">7</div>
<div class="box">8</div>
<div class="box">9</div>
<div class="box">10</div>
</div>
<script src="./scrollin.js"></script>
</body>
</html>
.area {
position: relative;
display: flex;
flex-direction: column;
align-items: center;
}
.box {
width: 100%;
height: 250px;
margin: 20px;
background-color: #ddd;
border: 1px solid #aaa;
text-align: center;
}
#target {
background-color: #90CAF9;
}
(function(){
window.addEventListener('scroll', this._scrollIn = (event) => {
let viewportHeight = window.innerHeight;
let targetTop = document.getElementById('target').getBoundingClientRect().top;
if(0 < targetTop && targetTop <= viewportHeight) {
console.log('scrollin!'); // do something
window.removeEventListener('scroll', this._scrollIn);
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment