-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.headliner.js
81 lines (63 loc) · 2.16 KB
/
jquery.headliner.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
/*
* Headliner.js 0.1.2
*
* Copyright 2014, Russell Wilson romeo-whiskey.com
* Released under the WTFPL license
* http://sam.zoy.org/wtfpl/
*
* Date: Mon May 09 19:38:00 2015 -0700
*/
/*global jQuery */
(function ($) {
"use strict";
$.fn.headliner = function (options) {
var settings = $.extend({
max_font_size: Number.POSITIVE_INFINITY,
animate: false
}, options);
return this.each(function () {
var container = $(this),
font_size = 0,
font_unit = "px",
parent = $(this).parent(),
max_width = parent.innerWidth() - parseInt(parent.css("padding-left"), 10) - parseInt(parent.css("padding-right"), 10),
current_font_size = font_size;
if (container.css("display") !== "inline-block") {
container.css({display: "inline-block"});
}
settings.initial = parseInt(container.css("font-size"), 10);
// Make sure we have text in there
if (container.text() === "") {
return;
}
// set our initial font size
container.css({
fontSize: font_size + font_unit
});
// adjust font size
while (true) {
current_font_size = parseInt(container.css("font-size"), 10);
font_size = font_size + 1;
if (current_font_size === settings.max_font_size) {
break;
}
if (container.outerWidth() >= max_width) {
font_size = font_size - 2;
container.css({
fontSize: font_size + font_unit
});
break;
}
container.css({
fontSize: font_size + font_unit
});
}
if (settings.animate) {
container.css({
fontSize: settings.initial + font_unit
});
container.animate({fontSize: font_size + font_unit});
}
});
};
}(jQuery));