jquery.lettering.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*global jQuery */
  2. /*!
  3. * Lettering.JS 0.7.0
  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 text = t.text()
  16. , a = text.split(splitter)
  17. , inject = '';
  18. if (a.length) {
  19. $(a).each(function(i, item) {
  20. inject += '<span class="'+klass+(i+1)+'" aria-hidden="true">'+item+'</span>'+after;
  21. });
  22. t.attr('aria-label',text)
  23. .empty()
  24. .append(inject)
  25. }
  26. }
  27. var methods = {
  28. init : function() {
  29. return this.each(function() {
  30. injector($(this), '', 'char', '');
  31. });
  32. },
  33. words : function() {
  34. return this.each(function() {
  35. injector($(this), ' ', 'word', ' ');
  36. });
  37. },
  38. lines : function() {
  39. return this.each(function() {
  40. var r = "eefec303079ad17405c889e092e105b0";
  41. // Because it's hard to split a <br/> tag consistently across browsers,
  42. // (*ahem* IE *ahem*), we replace all <br/> instances with an md5 hash
  43. // (of the word "split"). If you're trying to use this plugin on that
  44. // md5 hash string, it will fail because you're being ridiculous.
  45. injector($(this).children("br").replaceWith(r).end(), r, 'line', '');
  46. });
  47. }
  48. };
  49. $.fn.lettering = function( method ) {
  50. // Method calling logic
  51. if ( method && methods[method] ) {
  52. return methods[ method ].apply( this, [].slice.call( arguments, 1 ));
  53. } else if ( method === 'letters' || ! method ) {
  54. return methods.init.apply( this, [].slice.call( arguments, 0 ) ); // always pass an array
  55. }
  56. $.error( 'Method ' + method + ' does not exist on jQuery.lettering' );
  57. return this;
  58. };
  59. })(jQuery);