-
Notifications
You must be signed in to change notification settings - Fork 0
/
touch-modal.js
149 lines (141 loc) · 5.09 KB
/
touch-modal.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
/**
* Created by yangyiming on 15-1-12.
*/
(function (plugin, window) {
var factory = function () {
return plugin(window);
}
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(factory);
} else if (typeof exports === 'object') {
// Node/CommonJS style for Browserify
module.exports = factory;
} else {
// Browser globals
factory();
}
}(function (window) {
$.fn.modals = function (option) {
var defaultOption = {
callBack:function(){
},
before:function(){
},
effect:{
in:"in",
out:"fade"
}
}
return this.each(function () {
var tapEvent = "ontouchend" in document ? "tap" : "click";
var $element = $(this);
var $dialog = $element.find(".modal-dialog");
var options = $.extend({},defaultOption,option);
var callBack = options.callBack;
var openBefore = options.before;
var $backdrop = $(".modal-backdrop");
var $close = $element.find(".close");
$element.css("visibility", "hidden").css("display", "block");
var $dialog = $element.find(".modal-dialog");
var $dialogBody = $dialog.find(".modal-body");
var modalsHeight = $dialog.height();
var duration = options.duration || 300;
var closeEvent = options.close;
if (options.effect != null) {
var effectIn = options.effect.in;
var effectOut = options.effect.out;
} else {
var effectIn = "";
var effectOut = "";
}
var _startX, _startY, _curX, _curY, _moveX, _moveY;
$element.on("open", show);
$close.on(tapEvent, close);
$element.on("close", close);
$element.on("hide", hide);
$element.addClass(effectOut);
$dialogBody.on("touchstart", function (e) {
fnTouches(e);
fnTouchstart(e);
})
$dialogBody.on("touchmove", function (e) {
fnTouches(e);
fnTouchmove(e);
})
// touches
function fnTouches(e) {
if (!e.touches) {
e.touches = e.originalEvent.touches;
}
}
// touchstart
function fnTouchstart(e) {
_startX = e.touches[0].pageX;
_startY = e.touches[0].pageY;
}
//touchmove
function fnTouchmove(e) {
e.preventDefault();
_curX = e.touches[0].pageX;
_curY = e.touches[0].pageY;
_moveX = _curX - _startX;
_moveY = _curY - _startY;
var scrollTop = $dialogBody.scrollTop();
$dialogBody.scrollTop(_moveY + scrollTop);
}
function show(e, data) {
disableScrollBar();
$backdrop.css("display", "block").addClass("in");
if (openBefore) {
openBefore($element, data);
}
$element.trigger("openBefore");
$dialog.css({
marginTop: -modalsHeight / 2
});
$element.css({
visibility: "visible",
display: "block"
});
$element.addClass(effectIn);
if (callBack) {
callBack($element, data);
}
$("body").data("modals", true);
}
function hide() {
effectIn ? $element.removeClass(effectIn) : "";
$element.css({ "display": "block", visibility: "hidden" });
}
function close() {
if (effectIn) {
$element.removeClass(effectIn);
$backdrop.removeClass("in");
setTimeout(function () {
$element.css({ "display": "block", visibility: "hidden" });
$backdrop.css("display", "none");
enableScrollBar();
if (closeEvent) {
closeEvent();
}
}, duration);
} else {
$backdrop.removeClass("in");
$element.css({ "display": "block", visibility: "hidden" });
$backdrop.css("display", "none");
enableScrollBar();
if (closeEvent) {
closeEvent();
}
}
}
function disableScrollBar() {
$('body').on('touchmove.body', function (e) { e.preventDefault() }).css("overflow", "hidden");
}
function enableScrollBar() {
$('body').off('touchmove.body').removeAttr("style");
}
})
}
}, this));