forked from shaketbaby/ng-disable-scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disable-scroll-nocss.js
executable file
·152 lines (103 loc) · 3.88 KB
/
disable-scroll-nocss.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
140
141
142
143
144
145
146
147
148
149
150
151
152
(function(factory) {
var ngModule = factory(this.angular);
if (typeof module === 'object' && typeof module.exports === 'object') {
module.exports = ngModule;
}
})(function(angular){
'use strict';
return angular.module('ngDisableScrollNocss', [])
.directive('ngDisableScroll', ['$window', '$document', disableScroll]);
function disableScroll ($window, $document) {
return {
restrict: 'A',
link: function($scope, $element, $attrs) {
var currentScrollY = null;
var keys = {37: 1, 38: 1, 39: 1, 40: 1};
var eventsListening = 'DOMMouseScroll wheel mousewheel touchmove keydown';
var isListening = false;
var element = $element[0];
var touchStart = 0;
var touchMove = 0;
var deltaY = 0;
$scope.$watch($attrs.ngDisableScroll, function(shouldDisable) {
if (shouldDisable) {
disableScroll();
} else {
if (isListening) {
unbindHandler();
}
}
});
$scope.$on('$destroy', unbindHandler);
function scrollHandler(event) {
event = event || $window.event;
var isScrollAllowed = scrollAllowed(event);
if (element.scrollTop + element.offsetHeight >= element.scrollHeight && (event.deltaY > 0 || deltaY > 0))
{
isScrollAllowed = false;
}
if (element.scrollTop == 0 && (event.deltaY < 0 || deltaY < 0))
{
isScrollAllowed = false;
}
if (!isScrollAllowed) {
event.returnValue = false;
event.preventDefault();
}
// Also prevent scroll of main window
$window.onscroll = function () {
$window.scrollTo(0, currentScrollY);
}
}
function preventDefaultForScrollKeys(event) {
if (event.target.type === 'text') {
return true;
}
if (keys[event.keyCode]) {
return false;
}
return true;
}
function disableScroll() {
currentScrollY = $window.scrollY;
$document.bind(eventsListening, scrollHandler);
isListening = true;
$document.bind('touchstart', getTouchStart);
$document.bind('touchmove', getTouchMove);
}
function getTouchStart(event) {
touchStart = event.touches[0].clientY;
}
function getTouchMove(event) {
touchMove = event.touches[0].clientY;
deltaY = touchMove < touchStart ? 1 : -1;
console.log(deltaY);
}
function unbindHandler() {
$document.unbind(eventsListening, scrollHandler);
$window.onscroll = null;
isListening = false;
}
function scrollAllowed(event) {
if (event.type === 'keydown') {
return preventDefaultForScrollKeys(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);
}
}
};
}
});