forked from freqdec/slabText
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
371 lines (368 loc) · 22.7 KB
/
index.html
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<!DOCTYPE html>
<html lang="en-GB">
<head>
<meta charset="utf-8" />
<title>slabText – a jQuery plugin for creating big, bold & responsive headlines</title>
<!-- slabtext stylesheet -->
<link href="./css/slabtext.css" rel="stylesheet" type="text/css" />
<!-- demo styles inline -->
<style>
/* Note: I've not added the fontface files to the git repo */
@font-face
{
font-family: 'LeagueGothicRegular';
src: url('League_Gothic-webfont.eot');
src: url('League_Gothic-webfont.eot?#iefix') format('embedded-opentype'),
url('League_Gothic-webfont.woff') format('woff'),
url('League_Gothic-webfont.ttf') format('truetype'),
url('League_Gothic-webfont.svg#LeagueGothicRegular') format('svg');
font-weight: normal;
font-style: normal;
}
html,
body
{
background:#fcfcfc;
color:#444;
}
body
{
font:16px/1.8 "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
width:80%;
padding:20px 0;
max-width:960px;
margin:0 auto;
}
/* http://css-tricks.com/simple-styles-for-horizontal-rules/ */
hr
{
border:0;
height:1px;
background-image: -webkit-linear-gradient(left, rgba(0,0,0,0), rgba(0,0,0,0.75), rgba(0,0,0,0));
background-image: -moz-linear-gradient(left, rgba(0,0,0,0), rgba(0,0,0,0.75), rgba(0,0,0,0));
background-image: -ms-linear-gradient(left, rgba(0,0,0,0), rgba(0,0,0,0.75), rgba(0,0,0,0));
background-image: -o-linear-gradient(left, rgba(0,0,0,0), rgba(0,0,0,0.75), rgba(0,0,0,0));
}
.col-1
{
width:47.5%;
margin:0 2.5% 0 0;
float:left;
}
.col-2
{
width:47.5%;
margin:0 0 0 2.5%;
float:left;
}
.col-1 p,
.col-2 p
{
color:#888;
font-size:80%;
text-align:center;
}
a
{
color:#111;
}
h1 a
{
text-decoration:none;
}
h1 a:hover
{
color:#a82222;
}
p
{
margin:0 0 1.5em 0;
line-height:1.5em;
}
dt
{
font-family:monospace;
}
pre
{
line-height:1.2;
}
footer
{
border-top:3px double #aaa;
padding-top:1em;
}
footer section
{
border-bottom:3px double #aaa;
padding-bottom:1em;
margin-bottom:1em;
}
sup a
{
text-decoration:none;
}
#h4
{
clear:both;
}
.amp
{
font-family:Baskerville,'Goudy Old Style',Palatino,'Book Antiqua',serif;
font-style:italic;
font-weight:lighter;
}
/* Set font-sizes for the headings to be given the slabText treatment */
h1
{
text-align:left;
font-family:'LeagueGothicRegular', "Impact", Charcoal, Arial Black, Gadget, Sans serif;
text-transform: uppercase;
line-height:1;
color:#222;
font-size:300%;
/* Remember to set the correct font weight if using fontface */
font-weight:normal;
}
/* Smaller font-size for the side-by-side demo */
.col-1 h1,
.col-2 h1
{
font-size: 32px;
}
h2
{
font-size: 25px;
}
/* Adjust the line-height for all headlines that have been given the
slabtext treatment. Use a unitless line-height to stop sillyness */
.slabtexted h1
{
line-height:.9;
}
/* Target specific lines in the preset Studio One demo */
.slabtexted #studio-one span:nth-child(2)
{
line-height:.8;
}
.slabtexted #studio-one span:nth-child(3)
{
line-height:1.1;
}
/* Fun with media queries - resize your browser to view changes. */
@media screen and (max-width: 960px)
{
body
{
padding:10px 0;
min-width:20em;
}
.col-1,
.col-2
{
float:none;
margin:0;
width:100%;
}
h1
{
font-size:36px;
}
h2
{
font-size:22px;
}
}
@media screen and (max-width: 460px)
{
h1
{
font-size:26px;
}
h2
{
font-size:18px;
}
}
</style>
<!--[if lte IE 7]>
<style>
body
{
width:960px;
margin:0 auto;
}
</style>
<![endif]-->
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<h1 id="h1">slabText – a jQuery plugin for producing big, bold & responsive headlines</h1>
<p>I’ve been wanting to attempt a port of Erik Loyer’s <a href="http://erikloyer.com/index.php/blog/the_slabtype_algorithm_part_1_background/">slabtype algorithm</a> for quite some time now and seeing Paravel’s <a href="http://fittextjs.com/">fittext</a> jQuery plugin, in combination with a gloriously hassle-free lunch hour gave me the impetus to attempt it. This is the result – resize the browser viewport to see the effect in action.</p>
<h2 id="h2">So what does the script do again?</h2>
<p>Put simply, the script splits headlines into rows before resizing each row to fill the available horizontal space. The ideal number of characters to set on each row is calculated by dividing the available width by the pixel font-size – the script then uses this <em>ideal character count</em> to split the headline into word combinations that are displayed as separate rows of text. Many, many <a href="#h12">more examples</a> can be viewed further down the page.</p>
<h2 id="h3">Calculated and preset word combinations</h2>
<p>While the script does an admirable job at automating the creation of the individual rows, there are certainly situations in which you would choose to control the word combinations used to split the headline.</p>
<p>This can be achieved by presetting the word combinations (using <code><span class="slabtext"></code> wrappers) within the markup<sup><a href="#fn1" id="r1">[1]</a></sup>. Should the script detect that the headline has been preset, it will not attempt to dynamically create the word combinations and simply fallback to resizing the preset rows to fit the available horizontal space.</p>
<p>Adding the <code>span</code> elements to the page markup in this way gives you absolute control over the word combinations used to split the headline text and also enables the targetting of specific rows within the CSS (in order to tweak the line height, change the font family etc).</p>
<p>Presetting the headline does have its drawbacks though – at smaller sizes and if the rows have wildly varying letter-counts, jagged right edges may be displayed. The script will adjust the <code>word-spacing</code> or <code>letter-spacing</code> in an attempt to rectify over-shoots or under-shoots in line length like this.</p>
<p>The following demo showcases two versions of the same headline, the first has rows (word combinations) dynamically calculated by the script while the second has rows preset within the markup (and therefore only resized-to-fit by the script).</p>
<div class="col-1">
<p>The following word combinations are calculated by the script</p>
<h1>For one night only Jackie Mittoo with special Studio One guests Dillinger <span class="amp">&</span> Lone Ranger</h1>
</div>
<div class="col-2">
<p>The following word combinations are preset within the markup</p>
<h1 id="studio-one"><span class="slabtext">For one night only</span> <span class="slabtext">Jackie Mittoo</span> <span class="slabtext">with special Studio One guests</span> <span class="slabtext">Dillinger <span class="amp">&</span> Lone Ranger</span></h1>
</div>
<h2 id="h4">Headers containing links</h2>
<p>If the original header element contains a link (or should itself have an href set), the generated spans will be wrapped in a link that uses the same href value. This can be seen in action in the last demo below “<a href="#dracula">Dracula</a>”.</p>
<h2 id="h5">Being responsibly responsive</h2>
<p>You may wish to remove the slabtext treatment entirely should either the viewport or the header element resize to below a certain, predefined width; for example, if the viewport width drops to below 380 pixels. This can be achievied by using the <code>viewportBreakpoint</code> and <code>headerBreakpoint</code> plugin options, <a href="#h6">detailed below</a>.</p>
<p>This demo page has set <code>viewportBreakpoint</code> to be 380 pixels. Resizing the browser window to anything below this should remove the slabtext treatment from the headlines altogether.</p>
<h2 id="h6">Behind the scenes</h2>
<p>In an attempt to keep the plugin as fast as possible, a very basic <code>font-size</code> calculation is ran whenever the <code>window.resize</code> event fires. Headlines that have been preset only fire steps 1, 2, 6 and, if required, 7 – skipping steps 3 through 5 completely. To save extra cpu cycles, the resize event has been throttled to 300ms.</p>
<ol>
<li>The headlines pixel width is calculated.</li>
<li>The headlines pixel <code>font-size</code> is then calculated.</li>
<li>The ideal number of “characters per line” is derived by dividing the width (calculated in step 1) by the <code>font-size</code> (calculated in step 2) which itself is multiplied by the font scale ratio (which defaults to 0.78) e.g. for a parent container width of 800 pixels and a font-size of 45 pixels the ideal number of characters per line would be calculated using <code>Math.floor(800 / (45 × 0.78)) = 22</code>.</li>
<li>The headline text is then split into words (by splitting on the space character) and these words arranged into combinations containing <= the number of ideal characters per line (calculated in step 3).</li>
<li>Each word combination is then wrapped in a <code><span class="slabtext"></code> element and the headline markup updated with the new content.</li>
<li>Each individual <code>span</code> then has its <code>font-size</code> adjusted by multiplying the current <code>font-size</code> by the ratio difference between the parent containers width (calculated in step 1) and the width of the <code>span</code>.</li>
<li>If required, each individual <code>span</code> then has either its <code>letter-spacing</code> (if the line contains only one word) or its <code>word-spacing</code> (if the line contains multiple words) adjusted in an attempt to prod the browser into rectifying over-shoots or under-shoots in line length caused by the previous <code>font-size</code> adjustment.</li>
</ol>
<h2 id="h7">Plugin options</h2>
<p>The following options can currently be passed to the plugin. The first three listed (<code>fontRatio</code>, <code>forceNewCharCount</code> and <code>wrapAmpersand</code>) are only used whenever the plugin has to dynamically create the word combinations i.e. if <code><span class="slabtext"></code> elements are <em>not</em> already present within the markup.</p>
<dl>
<dt>fontRatio</dt>
<dd>A floating point value that is used when calculating the ideal number of “characters per line”. Defaults to 0.78.</dd>
<dt>forceNewCharCount</dt>
<dd>A Boolean value that instructs the script to recalculate the ideal number of “characters per line” and reinsert the spans every time the resize event fires (TRUE) or only whenever the parent containers <code>font-size</code> changes (FALSE). Setting this value to FALSE will inevitably save CPU cycles but is only really of use in modern browsers that respond to media queries (or older browsers have been patched to respond to media queries). Defaults to TRUE.</dd>
<dt>wrapAmpersand</dt>
<dd>A Boolean value that instructs the script to wrap ampersands (&) in a <code><span class="amp"></code> (TRUE) or not (FALSE). Defaults to TRUE.</dd>
<dt>maxFontSize</dt>
<dd>An Integer value that indicates the maximum pixel font-size that the script can set. Defaults to 999.</dd>
<dt>viewportBreakpoint</dt>
<dd>An Integer value that indicates the minimum pixel width the viewport may have before the slabtext treatment is removed. There is no default value.</dd>
<dt>headerBreakpoint</dt>
<dd>An Integer value that indicates the minimum pixel width the header may have before the slabtext treatment is removed. There is no default value.</dd>
<dt>noResizeEvent</dt>
<dd>A Boolean value that instructs the script to ignore the <code>window.resize</code> event (TRUE) or not (FALSE). Useful for those of you using a fixed width layout. Defaults to FALSE.</dd>
<dt>resizeThrottleTime</dt>
<dd>An Integer value that indicates the number of milliseconds the <code>window.resize</code> will be throttled to. Defaults to 300.</dd>
<dt>precision</dt>
<dd>An Integer value that indicates the decimal precision to use when setting the CSS values <code>line-height</code>, <code>word-spacing</code> and <code>font-size</code>. Defaults to 3.</dd>
<dt>postTweak</dt>
<dd>A Boolean value that instructs the script to tweak the lineheight or word spacing after the fontsize calculation has been ran (TRUE) or not (FALSE). Defaults to TRUE.</dd>
<dt>minCharsPerLine</dt>
<dd>An optional Integer value that indicates the minimum number of characters to set per line. Headlines that have a character count smaller than this value are not given the slabText treatment.</dd>
</dl>
<p>It’s also worth noting that any non-breaking space character used to prevent widowed words within the original, non-adjusted headline will also be included within the adjusted headline.</p>
<h2 id="h8">A note on the CSS</h2>
<p>The plugin requires the following CSS rules are made available:</p>
<pre><code>.slabtexted .slabtext
{
display:-moz-inline-box;
display:inline-block;
white-space:nowrap;
}
.slabtextinactive .slabtext
{
display:inline;
white-space:normal;
font-size:1em !important;
letter-spacing:inherit !important;
word-spacing:inherit !important;
*letter-spacing:0 !important;
*word-spacing:0 !important;
}
.slabtextdone .slabtext
{
display:block;
}
</code></pre>
<p>The <code>span</code> elements added to the headline are given the class <code>slabtext</code> and the document <code>body</code> given the class <code>slabtexted</code>.</p>
<p>Using a double-whammy className inheritance trick like this means that you can safely add the <code><span class="slabtext"></code> elements into the markup but they won’t actually get styled until the script gets called and the <code>slabtexted</code> classname added to the <code>body</code>.</p>
<p>The <code>span</code> elements are set as <code>display:inline-block</code> during the font-size calculation. This is to take advantage of the <code>inline-block</code> shrink-wrap effect that enables the script to determine the width of each row.</p>
<p>Unfortunately, one side-effect of using <code>inline-block</code> means that the injected <code>span</code> elements react to surrounding whitespace (which, amongst other things, augments the vertical spacing between rows) – this is remedied by giving the header a classname of <code>slabtextdone</code> whenever the <code>font-size</code> calculation is complete, which sets a <code>display:block</code> style and avoids the whitespace and styling issues associated with <code>inline-block</code>.</p>
<p>Sharp-eyed readers may have noticed that the value of zero is passed to Internet Explorer using the star-hack. This is because IE < 8 doesn’t understand the <code>inherit</code> property and so we reset to zero as a failsafe.</p>
<p>The CSS file is miniscule though and you may be better off pasting the contents of the minified version bundled with the download into your main CSS file to avoid an unnecessary HTTP request.</p>
<h2 id="h9">Buyer beware</h2>
<p>Here are a few things to remember when using the plug-in:</p>
<ol>
<li>Headlines with lots of horizontal space to fill are more gracefully displayed across browsers.</li>
<li>Headlines with little horizontal space tend to have jagged right edges, especially if the rows have been preset within the markup and each row has a wildly varying letter-count. Firefox appears to be the best at resizing-to-fit the word combinations and some fonts tend to be better than others at being resized-to-fit.</li>
<li>The element to be given the slabText treatment has its (inner)HTML replaced entirely (by using the jQuery <code>.html</code> method), which means that all images and links contained within will disappear without a trace.</li>
<li>Unlike the original <a href="http://erikloyer.com/index.php/blog/the_slabtype_algorithm_part_1_background/">slabtype algorithm</a>, vertical space is not taken into consideration at all.</li>
<li>I’ve no idea how the script behaves in a right-to-left environment.</li>
<li>If the header contains multiple links, only the first link located is taken into account.</li>
<li>Internet Explorer 6, due to its non-support of <code>inline-block</code>, cannot scale <em>down</em> the text when the browser viewport is reduced in width. This will not be an issue if you serve a fixed width design to IE6 and fluid width design to other, more capable browsers.</li>
<li>Internet Explorer < 8 does not support the CSS value of <code>inherit</code> which means that the <code>letter-spacing</code> and <code>word-spacing</code> have to get reset to zero whenever the slabText treatment is removed by the script.</li>
<li>Always call the script <em>after</em> all <code>fontface</code> fonts have downloaded. I’ve hacked this for the demo to enable you to see the headline transformation as the script kicks-in but you should always use google WebFont loaders <code>active()</code> and <code>inactive()</code> callbacks to launch the slabText treatment (or a similar "font loaded" callback feature from another font provider).</li>
</ol>
<h2 id="h10">Credit where credit is due</h2>
<p>Based on the nice, shiny <a href="http://fittextjs.com/">fittext</a> jQuery plugin by <a href="http://paravelinc.com/">Paravel</a> & the wonderful <a href="http://erikloyer.com/index.php/blog/the_slabtype_algorithm_part_1_background/">slabtype algorithm</a> by Erik Loyer. Zach Leatherman has also written a jQuery plugin named <a href="http://www.zachleat.com/web/bigtext-makes-text-big/">BigText</a> with similar but not exact functionality which is well worth a visit.</p>
<h2 id="h11">Grab the code</h2>
<p>The code can be <a href="http://github.com/freqDec/slabText/">downloaded from github</a>. Both minified and unminified versions are included within the bundle. The minified version currently clocks-in at 3k – this drops to around 2k when gzipped.</p>
<h2 id="h12">A few more examples</h2>
<p>As way of example, here’s a random assortment of book titles (Note: I’ve left widowed words in all of them).</p>
<!-- Yeah, yeah - multiple h1's etc. It's horrible but not the end of the world. Apologies to those offended. -->
<h1>The Sisters Brothers</h1>
<hr />
<h1>The curious incident of the dog in the night</h1>
<hr />
<h1>Something Happened</h1>
<hr />
<h1>The sad tale of the brothers Grossbart</h1>
<hr />
<h1>The Windup Girl</h1>
<hr />
<h1>When Gravity Fails</h1>
<hr />
<h1>Psychotic Reactions and Carburetor Dung</h1>
<hr />
<h1>Mortal Engines</h1>
<hr />
<h1>Mansfield Park</h1>
<hr />
<h1>The Importance of being Earnest</h1>
<hr />
<h1>The Scarlet Letter</h1>
<hr />
<h1 id="dracula"><a href="http://en.wikipedia.org/wiki/Dracula" title="The Dracula wikipedia page">Dracula</a></h1>
<footer>
<section>
<p id="fn1"><a href="#r1">(1)</a> Of course, you can always use Javascript to inject the <code><span class="slabtext"></code> elements into the headline before calling the plugin – which would keep the markup squeaky clean and cruft free. Here’s a quick example of how it might be done:</p>
<pre><code>var stS = "<span class='slabtext'>",
stE = "</span>",
txt = [
"For one night only",
"Jackie Mittoo",
"with special Studio One guests",
"Dillinger & Lone Ranger"];
$("#myHeader").html(stS + txt.join(stE + stS) + stE).slabText();</code></pre>
</section>
</footer>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="./js/jquery.slabtext.min.js"></script>
<script>
// Function to slabtext the H1 headings
function slabTextHeadlines() {
$("h1").slabText({
// Don't slabtext the headers if the viewport is under 380px
"viewportBreakpoint":380
});
};
// Called one second after the onload event for the demo (as I'm hacking the
// fontface load event a bit here)
// Please do not do this in a production environment - you should really use
// google WebFont loader events (or something similar) for better control
$(window).load(function() {
// So, to recap... don't actually do this, it's nasty!
setTimeout(slabTextHeadlines, 1000);
});
</script>
</body>
</html>