-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhoriz-split-box.js
42 lines (35 loc) · 1 KB
/
horiz-split-box.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
(function ($) {
$.fn.horizSplitBox = function() {
var $box = $(this);
var $cover = $box.find('.cover');
var $toggle = $box.find('.toggle-handle');
var mouseX, toggleLeft, draggable = false;
function getPageX(e) {
if (typeof e.pageX == 'undefined' ) {
return e.touches[0].pageX;
}
return e.pageX;
}
$(document).on('mousemove touchmove', function (e) {
if (!draggable) {
return;
}
var left = toggleLeft + getPageX(e) - mouseX;
var width = $toggle.parent().width() - $toggle.width();
left = Math.max(Math.min(left, width), 0);
$toggle.css('left', left);
$cover.css('width', left);
});
$(document).on('mouseup touchend', function (e) {
draggable = false;
$box.css('user-select', '');
});
$toggle.on('mousedown touchstart', function (e) {
draggable = true;
mouseX = getPageX(e);
toggleLeft = $toggle.position().left;
$box.css('user-select', 'none');
});
return $box;
}
})(jQuery);