Last active
October 3, 2016 09:52
-
-
Save aloerina01/d4c5314cd9166a7b5a860f31d299dbf6 to your computer and use it in GitHub Desktop.
スクロールインイベントを一度だけキャッチする
This file contains 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
<!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> |
This file contains 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
.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; | |
} |
This file contains 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
(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