-
Notifications
You must be signed in to change notification settings - Fork 6
/
jquery.izilla.touchMenuHover.js
67 lines (55 loc) · 1.77 KB
/
jquery.izilla.touchMenuHover.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
/*!
* Izilla touchMenuHover jQuery plugin v1.7
* Allows ULs (or any element of your choice) that open on li:hover to open on tap/click on mobile platforms such as iOS, Android, WP7, WP8, BlackBerry, Bada, WebOS, 3DS & WiiU
*
* Copyright (c) 2014 Izilla Partners Pty Ltd
*
* http://izilla.com.au
*
* Licensed under the MIT license
*/
;(function($) {
$.fn.touchMenuHover = function(options) {
var settings = $.extend({
childTag: 'ul',
closeElement: '',
forceiOS: false,
openClass: 'tmh-open'
}, options);
var $a = $(this).find('a'),
devices = '3ds|android|bada|bb10|hpwos|iemobile|kindle fire|opera mini|opera mobi|opera tablet|rim|silk|wiiu',
devicesRX,
closeStr = 'html',
$close;
if (settings.childTag.toString().toLowerCase() !== 'ul' || settings.forceiOS)
devices += '|ipad|ipod|iphone';
devicesRX = new RegExp(devices, 'gi');
if ($a.length > 0 && devicesRX.test(navigator.userAgent)) {
$a.each(function() {
var $this = $(this),
$parent = $this.parent('li'),
$siblings = $parent.siblings().find('a');
if ($this.next(settings.childTag).length > 0)
$parent.attr('aria-haspopup', true);
$this.click(function(e) {
var $this = $(this);
e.stopPropagation();
$siblings.removeClass(settings.openClass);
if (!$this.hasClass(settings.openClass) && $this.nextAll(settings.childTag).length > 0) {
e.preventDefault();
$this.addClass(settings.openClass);
}
});
});
if (settings.closeElement.length > 1)
closeStr += ',' + settings.closeElement;
$close = $(closeStr);
if ('ontouchstart' in window)
$close.css('cursor', 'pointer');
$close.click(function() {
$a.removeClass(settings.openClass);
});
}
return this;
};
})(jQuery);