-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouch-hover-to-link.js
56 lines (51 loc) · 1.62 KB
/
touch-hover-to-link.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
/*!
* jQuery touch-hover-to-link plugin
* Original author: @mwitekdesign
* Further changes, comments: https://github.com/red-green-refactor/touch-hover-to-link
* Licensed under the MIT license
*/
var $, isTouchDevice;
$ = jQuery;
isTouchDevice = function() {
return "ontouchstart" in document.documentElement;
};
$.fn.extend({
touchHoverToLink: function(options) {
var $item, bindCloseHoverToBodyClick, bindTouchLinkClickEvent, item, setupTouchLinkItem, _dataLinkSet;
_dataLinkSet = false;
item = this;
$item = $(item);
setupTouchLinkItem = function() {
$item.attr('data-href', $item.attr('href'));
return $item.attr('href', "#touch_click_link");
};
bindTouchLinkClickEvent = function() {
return $item.on('touchstart', function(event) {
event.preventDefault();
if ($(event.currentTarget).attr('data-linkSet') === "true") {
window.location.href = $(event.currentTarget).attr('data-href');
_dataLinkSet = false;
} else {
$item.attr('data-linkSet', "false");
$(event.currentTarget).attr('data-linkSet', "true");
_dataLinkSet = true;
}
});
};
bindCloseHoverToBodyClick = function() {
return $('body').on('touchstart', function(event) {
if (!$(event.target).hasClass(item.selector.substring(1))) {
if (_dataLinkSet === true) {
$item.attr('data-linkSet', "false");
_dataLinkSet = false;
}
}
});
};
if (isTouchDevice()) {
setupTouchLinkItem();
bindTouchLinkClickEvent();
bindCloseHoverToBodyClick();
}
}
});