-
Notifications
You must be signed in to change notification settings - Fork 0
/
iframe.js
58 lines (46 loc) · 1.27 KB
/
iframe.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
function ready(fn) {
var docRef = this.document;
if (docRef.attachEvent ? docRef.readyState === 'complete' : docRef.readyState !== 'loading') {
fn();
} else {
docRef.addEventListener('DOMContentLoaded', fn);
}
}
function elementInParentViewport(el) {
var parentWindow = window.parent;
var top = el.offsetTop;
var left = el.offsetLeft;
var width = el.offsetWidth;
var height = el.offsetHeight;
while (el.offsetParent) {
el = el.offsetParent;
top += el.offsetTop;
left += el.offsetLeft;
}
return (
top < parentWindow.pageYOffset + parentWindow.innerHeight &&
left < parentWindow.pageXOffset + parentWindow.innerWidth &&
top + height > parentWindow.pageYOffset &&
left + width > parentWindow.pageXOffset
);
}
function onShow() {
alert('Me-Mow~');
}
ready(function selfReadyF() {
var parentReadyF = function() {
var windowRef = this;
var parentWindowRef = window.frameElement;
if (elementInParentViewport(parentWindowRef)) {
onShow();
return;
}
windowRef.addEventListener('scroll', function f(event) {
if (elementInParentViewport(parentWindowRef)) {
onShow();
windowRef.removeEventListener('scroll', f);
}
});
};
ready(parentReadyF.bind(window.parent));
});