Created
August 21, 2014 06:32
-
-
Save RyoSugimoto/6d8869693c0a7e00d726 to your computer and use it in GitHub Desktop.
リスト要素を分割するjQueryプラグイン。分割されたすべてのリストの要素セットを返す。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function (window, document, $) { | |
$.fn.devideList = function (options) { | |
options = $.extend({}, { | |
capacity: 5, // 1つのリスト内のアイテムの数 | |
listClass: 'divided-list', | |
itemClass: 'divided-item', | |
/** | |
* リストアイテムがひとつ配置される度に実行する。 | |
* @param i {number} アイテムのインデックス | |
* @param $item {jQuery Object} 配置されたアイテム | |
* @param nombre {number} アイテムが配置されたページ番号(ノンブル)(1~) | |
*/ | |
afterSetOne: function (i, $item, nombre) { | |
$.noop(); | |
}, | |
/** | |
* リストの分割が終了したときに実行する。 | |
* @param $elm {jQuery Object} 分割されたすべてのリストを含むオブジェクト | |
*/ | |
afterSetAll: function ($elm) { | |
$.noop(); | |
} | |
}, options), | |
$returned = $(); | |
this.each(function (i) { | |
var $elm = $(this), | |
$item = $elm.find('> li'), | |
listClass = $elm.attr('class'), | |
$lastList = $elm, | |
itemLength = $item.length, | |
listType = this.tagName.toLowerCase(), | |
nombre = 1; | |
$returned = $returned.add($elm); | |
$elm.addClass(options.listClass); | |
if ($elm.attr('id')) { | |
var originalId = $elm.attr('id'); | |
$elm.attr('id', originalId + '-' + nombre); | |
} | |
$item.each(function (i) { | |
var currentNombre = nombre; | |
$(this).addClass(options.itemClass); | |
if (i >= options.capacity) { | |
$lastList.append($(this)); | |
} | |
if ((i + 1) % options.capacity === 0) { | |
nombre++; | |
$lastList = $('<' + listType + ' class=' + listClass + '></'+ listType + '>').addClass(options.listClass).insertAfter($lastList); | |
if (originalId) { | |
$lastList.attr('id', originalId + '-' + nombre); | |
} | |
$returned = $returned.add($lastList); | |
} | |
if (typeof options.afterSetOne === 'function') { | |
options.afterSetOne(i, $(this), currentNombre); | |
} | |
}); | |
if (typeof options.callback === 'function') { | |
afterAll($elm); | |
} | |
}); | |
return $returned; | |
}; | |
}(window, document, jQuery, undefined)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment