-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisable-scroll.js
executable file
·67 lines (59 loc) · 2.21 KB
/
disable-scroll.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
(function(factory) {
var ngModule = factory(this.angular);
if (typeof module === "object" && typeof module.exports === "object") {
module.exports = ngModule;
}
})(function(angular) {
return angular.module("ngDisableScroll", []).directive("ngDisableScroll", [
"$document",
function($document) {
var lastElementToDisableScroll = null;
$document.find("head").append(
"<style type='text/css'>.ng-disable-scroll{overflow:hidden !important;}</style>"
);
return {
restrict: "A",
link: function($scope, $element, $attrs) {
var rootHtmlElement = angular.element($document[0].documentElement);
$scope.$watch($attrs.ngDisableScroll, function(shouldDisable) {
if (shouldDisable) {
lastElementToDisableScroll = $element;
rootHtmlElement.addClass("ng-disable-scroll");
$document.bind("touchmove", touchHandler);
} else {
unbindHandler();
}
});
$scope.$on("$destroy", unbindHandler);
function unbindHandler() {
$document.unbind("touchmove", touchHandler);
if ($element === lastElementToDisableScroll) {
return rootHtmlElement.removeClass("ng-disable-scroll");
}
}
function touchHandler(event) {
if (!scrollAllowed(event)) {
return event.preventDefault();
}
}
function scrollAllowed(event) {
var selector = $attrs.scrollableElements;
if (!selector) {
return canScroll(event, $element[0]);
}
var predicate = canScroll.bind(null, event);
return scrollableNodes(selector).some(predicate);
}
function canScroll(event, scrollable) {
return scrollable.contains(event.target) &&
scrollable.scrollHeight > scrollable.clientHeight;
}
function scrollableNodes(selector) {
var nodes = $element[0].querySelectorAll(selector);
return Array.prototype.slice.apply(nodes);
}
}
};
}
]);
});