columnlist.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * Column list Plugin
  3. * - Breaks up a list into separate columns
  4. * - src: https://github.com/weblinc/jquery-columnlist
  5. *
  6. Copyright (c) 2012 WebLinc LLC
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. */
  16. (function($){
  17. jQuery.fn.columnlist = function( options ) {
  18. options = $.extend( {}, $.fn.columnlist.defaults, options );
  19. return this.each(function () {
  20. var $list = $( this ),
  21. size = options.size || $list.data( 'columnList' ) || 1,
  22. $children = $list.children('li'),
  23. perColumn = Math.ceil( $children.length / size ),
  24. $column;
  25. for ( var i = 0; i < size; i++ ) {
  26. // Set up column wrapper
  27. $column = $('<ul />');
  28. for ( var j = 0; j < perColumn; j++ ) {
  29. if ( $children.length > i * perColumn + j ) {
  30. // Add class to column wrapper
  31. $column.addClass(options['class']).addClass(options.incrementClass + i).append( $children[ i * perColumn + j ]);
  32. }
  33. }
  34. // Add new columns
  35. $list.parent().append( $column );
  36. }
  37. // Remove original list
  38. $list.remove();
  39. });
  40. };
  41. // Defaults
  42. $.fn.columnlist.defaults = {
  43. 'class' : 'nav-menu',
  44. incrementClass : 'nav-menu-'
  45. };
  46. })(jQuery);