This repository has been archived by the owner on May 18, 2022. It is now read-only.
/
jquery-bigTarget.js
103 lines (92 loc) · 3.06 KB
/
jquery-bigTarget.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
/**
* jquery-bigTarget.js - enlarge an anchors clickzone
* https://github.com/leevigraham/jquery-bigTarget
* Written by: Leevi Graham <http://leevigraham.com>
* Requires: jQuery v1.3.2 or later
*
* jquery-bigTarget.js takes an anchor and expands it's clickzone by adding an onclick action to a parent element (defined in the clickzone plugin option) improving user accessibility.
*
* Plugin options:
*
* clickZone: 'div:eq(0)', // parent element selector. The element will be the big target clickzone
* clickZoneClass: 'big-target-click-zone', // class added to the clickzone
* clickZoneHoverClass: 'big-target-click-zone-hover', // class add on clickzone hover
* anchorClass: 'big-target-anchor', // class added the the bigTarget anchor
* anchorHoverClass: 'big-target-anchor-hover', // class added the the bigTarget anchor on hover
* copyTitleToClickZone: true, // copy the anchors title element to the clickzone
* openRelExternalInNewWindow: true // open rel="external" in a new window / tab
*
* Example Usage:
*
* $("#example2 .big-target-link").bigTarget({
* clickZone: '#example2',
* clickZoneClass: 'custom-big-target-click-zone',
* clickZoneHoverClass: 'custom-big-target-click-zone-hover',
* anchorHoverClass: 'custom-big-target-link-hover',
* copyTitleToClickZone: false,
* openRelExternalInNewWindow: false
* });
*/
;(function($) {
var ver = '2.0';
$.fn.bigTarget = function(options) {
var settings = $.extend({}, $.fn.bigTarget.defaults, options)
return this.each(function(index) {
var $a = $(this),
href = this.href || false,
title = this.title || false;
if(!href) {
return;
}
var o = $.metadata ? $.extend({}, settings, $a.metadata()) : settings;
$a
.addClass(o['anchorClass'])
.hover(function() {
$a.toggleClass(o['anchorHoverClass']);
})
.parents(o['clickZone'])
.each(function(index) {
var $clickZone = $(this);
if(title && o['copyTitleToClickZone']) {
$clickZone.attr('title', title);
}
$clickZone
.addClass(o['clickZoneClass'])
.hover(function() {
$clickZone.toggleClass(o['clickZoneHoverClass']);
})
.click(function(e) {
if ( ! (e.metaKey || e.ctrlKey))
{
if(getSelectedText() == "") {
($a.is('[rel*=external]') && o["openRelExternalInNewWindow"])
? window.open(href)
: window.location = href;
}
}
});
});
});
}
function getSelectedText(){
var t = false;
if(window.getSelection) {
t = window.getSelection().toString();
} else if(document.getSelection) {
t = document.getSelection();
} else if(document.selection) {
t = document.selection.createRange().text;
}
return t;
}
$.fn.bigTarget.ver = function() { return ver; };
$.fn.bigTarget.defaults = {
clickZone : 'div:eq(0)',
clickZoneClass : 'big-target-click-zone',
clickZoneHoverClass : 'big-target-click-zone-hover',
anchorClass : 'big-target-anchor',
anchorHoverClass : 'big-target-anchor-hover',
copyTitleToClickZone : true,
openRelExternalInNewWindow: true
};
})(jQuery);