-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.fontsize.js
156 lines (125 loc) · 3.44 KB
/
jquery.fontsize.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
150
151
152
153
154
155
156
/*
* Part of Nitriques Solutions inc. (http://www.nitriques.com) jQuery plug-in bundle
*
* Use this plugin to dynamically modifies target elements' font-size. Compatible with
* jQuery Cookie plugin to save the user preferences
*
* Usage $('.buttons').ntr$fontSize(opts);
*
* Examples:
*
* 1- Default: toggle between 1, 1.1, 1.2, 1.3, 1.4, 0.8, 0.9 (80% to 140%) the original font size
* on <p>
* $('#plustext').ntr$fontSize();
*
* 2- With options: toggle with default sizes on selected <p> only
* $('#plustext').ntr$fontSize({
* elements: p:not(.fixed-font-size)
* });
*
* 3- With options: toggle all <p> only between 100% to 60%
* $('#plustext').ntr$fontSize({
* size: [1, 0.9, 0.8, 0.7, 0.6]
*
* OR
*
* size: function () {
* var x, a = [];
* for (x=1; x<=0.6; x-=0.1) {
* a.push(x);
* }
* return a
* }
*
* });
*
* Liscence under the The Code Project Open License (CPOL)
* http://www.codeproject.com/info/cpol10.aspx
* Name: jquery.fontsize.js
* Date: 2011-06-16
* Version: 1.0
* Pre-requisites: none;
* Optionally: jquery.cookie.js
*
* Version 1.0
* Initial version
*/
(function ($, undefined) {
var defaults = {
sizes: [1, 1.1, 1.2, 1.3, 0.8, 0.9],
elements: 'p',
useCookie: true
};
$.fn.extend({ // jQuery plugin
ntr$fontSize: function(options) {
var opts = $.extend({}, defaults, options),
t = $(this),
key = 'font-size',
key_u = 'font-size-unit',
clas = '[fontSize] ',
cookie = key + t.selector;
if (!t || !t.length) {
//console.log(clas + 'target element not found');
return;
}
// validate cookie plugin
opts.useCookie = opts.useCookie && $.cookie;
function adjustFontSize(e) {
var d = $(document).data(cookie), // retrieve current pointer
p = isNaN(d)?0:parseInt(d, 10);
// increment pointer
if (++p >= opts.sizes.length) {
p = 0;
}
// save pointer
$(document).data(cookie, p);
console.log(clas + 'index:' + p);
// for each elements
$(opts.elements).each(function() {
var t = $(this),
s = parseFloat(t.data(key)),
u = t.data(key_u),
newSize = (s * opts.sizes[p]) + u;
console.log(clas + newSize);
t.css(key, newSize);
});
if (e && opts.useCookie) {
$.cookie(cookie, p, { expires: 30, path: '/' });
}
if (e) {
e.preventDefault();
}
return false;
};
// init code
// for each elements, get their current size
$(opts.elements).each(function() {
var t = $(this),
s = t.css(key), // raw size
u = s.replace(/[0-9]*/, '') ; // unit
// get numeric value
s = parseFloat(s, 10);
//console.log(clas + s + ' ' + u);
// save this data
t.data(key, s);
t.data(key_u, u);
});
// try retreive value
if (opts.useCookie) {
var c = $.cookie(cookie);
if (c) { // prevent unnecessary parsing
c = parseInt(c, 10);
if (c >= 0 && c < opts.sizes.length) {
// save position
$(document).data(cookie, --c); // -- since the pointer will be increment in adjustFontSize
// adjust font to good size
adjustFontSize.call(t, null);
}
}
}
// bind event
t.click(adjustFontSize);
return t;
}
}); // extend
})(jQuery);