-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
jquery.nicescroll.iframehelper.js
105 lines (84 loc) · 3.24 KB
/
jquery.nicescroll.iframehelper.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
/* iframe script helper for jquery.nicescroll
-- version 0.9.0
-- copyright 2017-06-18 InuYaksa*2017
-- licensed under the MIT
--
-- https://nicescroll.areaaperta.com/
-- https://github.com/inuyaksa/jquery.nicescroll
--
*/
(function (document,window) {
var body = document.body;
var parent = window.parent;
if (parent && ("createEvent" in document)) {
var isoldie = ("documentMode" in document); // 11-
var ismsedge = ("msCredentials" in window); // MS Edge 14+
function onwheel(e) {
var evt = document.createEvent("MouseEvents");
evt.initEvent('wheel', true, true);
evt.deltaMode = e.deltaMode;
evt.deltaX = e.deltaX;
evt.deltaY = e.deltaY;
evt.deltaZ = e.deltaZ;
evt.wheelDelta = e.wheelDelta;
evt.wheelDeltaX = e.wheelDeltaX;
evt.wheelDeltaY = e.wheelDeltaY;
parent.dispatchEvent(evt);
}
body.addEventListener("wheel", onwheel);
}
if (window.addEventListener) {
// https://davidwalsh.name/add-rules-stylesheets
var sheet = (function () {
var style = document.createElement("style");
style.appendChild(document.createTextNode(""));
document.head.appendChild(style);
return style.sheet;
})();
var tmrscroll = false;
var lastiframe = null;
var lastiframeviewport = null;
var lastscroll = [];
window.addEventListener("scroll", function (e) {
if (lastiframeviewport) {
// var df = [ window.scrollX - lastscroll[0], window.scrollY - lastscroll[1] ];
window.scrollTo(lastscroll[0], lastscroll[1]);
// lastiframeviewport.scrollBy(df[0],df[1]);
// console.log(df);
}
});
function findNiceParent(t) {
do {
if ($.data(t, '__nicescroll') !== undefined) return t;
t = t.parentNode || false;
} while (t);
return false;
}
window.addEventListener("load", function () {
var hasstyle = false;
$.nicescroll.each(function () {
var nice = this;
nice.scrollstart(function () {
if (!hasstyle) sheet.insertRule("iframe { pointer-events: none !important; }", 0);
hasstyle = true;
});
nice.scrollend(function () {
if (hasstyle) sheet.deleteRule(0);
hasstyle = false;
});
});
$("iframe").each(function () {
this.addEventListener("mouseenter", function (e) {
lastiframe = e.target;
var chk = findNiceParent(lastiframe);
lastiframeviewport = chk;
//if (chk) lastiframeviewport = $(chk).getNiceScroll();
lastscroll = [window.scrollX, window.scrollY];
});
this.addEventListener("mouseleave", function (e) {
lastiframe = lastiframeviewport = null;
});
});
});
}
})(document,window);