-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewport.html
60 lines (59 loc) · 1.23 KB
/
viewport.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="js/jquery-2.1.4.js"></script>
<title>viewport</title>
<style>
*{
margin: 0;
padding: 0;
}
#item{
width: 100px;
height:100px;
border: 1px solid blue;
background: lightblue;
margin: 10px auto;
}
#down{
height: 1800px;
background: lightblue;
}
</style>
</head>
<body>
<div id="item">
exist
</div>
<div id="down">
ddd
</div>
<script>
var lastScrollTop = 0;
$(window).scroll(function() {
var nowScrollTop = $(this).scrollTop();
var direction = true;
if(nowScrollTop < lastScrollTop){
direction = false;
}
if(direction){
console.log("down");
}else{
console.log("up");
}
lastScrollTop = nowScrollTop;
var itemOffsetTop = $("#item").offset().top;
var itemOuterHeight = $("#item").outerHeight(true);
var windowFromBottomHeight = $(window).scrollTop() + $(window).height();
var windowScrollTop = $(window).scrollTop();
if((windowFromBottomHeight > itemOffsetTop) && (windowScrollTop < itemOuterHeight)){
console.log("visible");
}
else {
console.log("invisible");
}
});
</script>
</body>
</html>