Posted By

doowron on 10/26/11


Tagged

typography Lettering


Versions (?)

Lettering.js


 / Published in: JavaScript
 

URL: http://daverupert.com

  1. /*global jQuery */
  2. /*!
  3. * Lettering.JS 0.6.1
  4. *
  5. * Copyright 2010, Dave Rupert http://daverupert.com
  6. * Released under the WTFPL license
  7. * http://sam.zoy.org/wtfpl/
  8. *
  9. * Thanks to Paul Irish - http://paulirish.com - for the feedback.
  10. *
  11. * Date: Mon Sep 20 17:14:00 2010 -0600
  12. */
  13. (function($){
  14. function injector(t, splitter, klass, after) {
  15. var a = t.text().split(splitter), inject = '';
  16. if (a.length) {
  17. $(a).each(function(i, item) {
  18. inject += '<span class="'+klass+(i+1)+'">'+item+'</span>'+after;
  19. });
  20. t.empty().append(inject);
  21. }
  22. }
  23.  
  24. var methods = {
  25. init : function() {
  26.  
  27. return this.each(function() {
  28. injector($(this), '', 'char', '');
  29. });
  30.  
  31. },
  32.  
  33. words : function() {
  34.  
  35. return this.each(function() {
  36. injector($(this), ' ', 'word', ' ');
  37. });
  38.  
  39. },
  40.  
  41. lines : function() {
  42.  
  43. return this.each(function() {
  44. var r = "eefec303079ad17405c889e092e105b0";
  45. // Because it's hard to split a <br/> tag consistently across browsers,
  46. // (*ahem* IE *ahem*), we replaces all <br/> instances with an md5 hash
  47. // (of the word "split"). If you're trying to use this plugin on that
  48. // md5 hash string, it will fail because you're being ridiculous.
  49. injector($(this).children("br").replaceWith(r).end(), r, 'line', '');
  50. });
  51.  
  52. }
  53. };
  54.  
  55. $.fn.lettering = function( method ) {
  56. // Method calling logic
  57. if ( method && methods[method] ) {
  58. return methods[ method ].apply( this, [].slice.call( arguments, 1 ));
  59. } else if ( method === 'letters' || ! method ) {
  60. return methods.init.apply( this, [].slice.call( arguments, 0 ) ); // always pass an array
  61. }
  62. $.error( 'Method ' + method + ' does not exist on jQuery.lettering' );
  63. return this;
  64. };
  65.  
  66. })(jQuery);

Report this snippet  

You need to login to post a comment.