forked from MohammadYounes/jquery-scrollLock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery-scrollLock.js
74 lines (69 loc) · 2.57 KB
/
jquery-scrollLock.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
/*!
* Scroll Lock v1.1.1
* https://github.com/MohammadYounes/jquery-scrollLock
*
* Copyright (c) 2014 Mohammad Younes
* Licensed under the MIT license.
*/
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {
var eventName = "onmousewheel" in window ? (("ActiveXObject" in window) ? "wheel" : "mousewheel") : "DOMMouseScroll";
var eventNamespace = ".scrollLock";
var old = $.fn.scrollLock;
$.fn.scrollLock = function (toggle, selector, force) {
if (typeof selector !== 'string')
selector = null;
if ((toggle !== undefined && !toggle) || toggle === 'off')
return this.each(function () {
$(this).off(eventNamespace);
});
else
return this.each(function () {
$(this).on(eventName + eventNamespace, selector, function (event) {
//allow zooming
if (!event.ctrlKey) {
var $this = $(this);
if (force === true || hasVerticalScroll($this)) {
//Support for nested scrollable blocks (see https://github.com/MohammadYounes/jquery-scrollLock/issues/4)
event.stopPropagation();
var scrollTop = $this.scrollTop(),
scrollHeight = $this.prop('scrollHeight'),
clientHeight = $this.prop('clientHeight'),
delta = event.originalEvent.wheelDelta || (-1 * event.originalEvent.detail) || (-1 * event.originalEvent.deltaY),
deltaY = 0
;
if (event.type === "wheel") {
var ratio = $this.height() / $(window).height();
deltaY = event.originalEvent.deltaY * ratio;
}
if (delta > 0 && scrollTop + deltaY <= 0 || delta < 0 && scrollTop + deltaY >= scrollHeight - clientHeight) {
event.preventDefault();
if (deltaY)
$this.scrollTop(scrollTop + deltaY);
}
}
}
});
});
};
function hasVerticalScroll($element) {
var clientWidth = $element.prop('clientWidth'),
offsetWidth = $element.prop('offsetWidth'),
borderRightWidth = parseInt($element.css('border-right-width'), 10),
borderLeftWidth = parseInt($element.css('border-left-width'), 10)
;
return clientWidth + borderLeftWidth + borderRightWidth < offsetWidth;
}
// no conflict
$.fn.scrollLock.noConflict = function () {
$.fn.scrollLock = old;
return this;
};
}));