forked from pazguille/viewport
/
index.js
139 lines (107 loc) · 3.07 KB
/
index.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Module dependencies.
*/
var Emitter = require('emitter'),
win = window,
doc = win.document,
docEl = doc.documentElement,
on = win.addEventListener || win.attachEvent,
RESIZE = (win.attachEvent) ? 'onresize' : 'resize',
SCROLL = (win.attachEvent) ? 'onscroll' : 'scroll',
resized = false,
scrolled = false;
function update() {
// No changing, exit
if (!resized && !scrolled) { return; }
var eve = (resized) ? 'resize' : 'scroll';
// Updates viewport
this.refresh();
// Change status
resized = false;
scrolled = false;
// Emits the current event
this.emit(eve);
}
/**
* Events
*/
on(RESIZE, function () { resized = true; });
on(SCROLL, function () { scrolled = true; });
/**
* Viewport
*/
function Viewport() {
this.init();
}
Emitter(Viewport.prototype);
Viewport.prototype.init = function () {
var that = this;
that.refresh();
that.calculateDeviceDimensions();
win.setInterval(function () {
update.call(that);
}, 350);
};
Viewport.prototype.device = {};
Viewport.prototype.cordinate = {};
Viewport.prototype.calculateDimensions = function () {
this.height = docEl.clientHeight;
this.width = docEl.clientWidth;
};
Viewport.prototype.calculateDeviceDimensions = function () {
this.device.height = win.screen.height;
this.device.width = win.screen.width;
};
Viewport.prototype.calculateScroll = function () {
this.scrollY = win.pageYOffset || docEl.scrollTop;
this.scrollX = win.pageXOffset || docEl.scrollLeft;
if (this.scrollY === 0) {
this.emit('top');
} else if ((this.height + this.scrollY) >= doc.body.scrollHeight) {
this.emit('bottom');
}
};
Viewport.prototype.calculateOffset = function () {
this.top = this.scrollY;
this.right = this.scrollX + this.width;
this.bottom = this.scrollY + this.height;
this.left = this.scrollX;
};
Viewport.prototype.calculateMousePostition = function (eve) {
// add support for touch events ==> eve.changedTouches;
var coordX = 0,
coordY = 0;
eve = eve || window.event;
if (eve.pageX || eve.pageY) {
coordX = eve.pageX;
coordY = eve.pageY;
} else {
coordX = eve.clientX + doc.body.scrollLeft + docEl.scrollLeft;
coordY = eve.clientY + doc.body.scrollTop + docEl.scrollTop;
}
return {
'posX': coordX,
'posY': coordY
};
};
Viewport.prototype.calculateOrientation = function () {
this.orientation = (Math.abs(win.orientation) === 90) ? 'landscape' : 'portrait';
};
Viewport.prototype.inViewport = function (el) {
var r = el.getBoundingClientRect();
return (r.top > 0) && (r.right < this.width) && (r.bottom < this.height) && (r.left > 0);
};
Viewport.prototype.isVisible = function (el) {
var r = el.getBoundingClientRect();
return (r.height >= this.top);
};
Viewport.prototype.refresh = function () {
this.calculateDimensions();
this.calculateScroll();
this.calculateOffset();
this.calculateOrientation();
};
/**
* Expose Viewport
*/
exports = module.exports = Viewport;