-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfluidtext.js
More file actions
54 lines (53 loc) · 1.91 KB
/
fluidtext.js
File metadata and controls
54 lines (53 loc) · 1.91 KB
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
(function ($, window) {
"use strict";
var els = $([]),
numEls = 0,
methods = {
init: function (options) {
return this.each(function () {
var $this = $(this),
opts = {
forceRedraw: false
},
fluidData;
$.extend(opts, options);
fluidData = {
initSize: opts.initSize || parseInt($this.css("font-size")),
initWidth: opts.initWidth || $this.parent.width()
};
$this.data("fluidData", fluidData);
els = els.add($this);
if (opts.forceRedraw === true) {
redraw();
}
});
}
};
function redraw() {
var wDiff = 0,
el, fluidData, fSize, parentW, $parent, $this;
els.each(function () {
$this = $(this);
$parent = $this.parent()
if (typeof $parent === "undefined") {
return;
}
fSize = $this.css("font-size");
parentW = $parent.width();
fluidData = $this.data("fluidData");
wDiff = fluidData.initWidth - parentW;
fSize = fluidData.initSize - (fluidData.initSize/fluidData.initWidth) * wDiff;
$this.css("font-size", fSize);
});
}
$.fn.fluidText = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === "object" || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.fluidtext');
}
};
$(window).resize(redraw);
}(jQuery, window));