替换jQuery分离

时间:2015-07-27 19:06:38

标签: jquery detach

我正在使用Bootstrap网格工作;我已经使用jQuery实现了自定义排序。我注意到我对jQuery的detach方法的调用是我排序中最昂贵的部分,如果我有超过一定数量的行,可能会导致可见的延迟 - 我会改变位置的变化。例如,给定18行,排序需要不到400毫秒 - 超过300毫秒来自对jQuery detach的调用。这是我的代码的核心:

var rows = jQuery('.row'),
    sorted = options.sort(function (a, b) {
        var aResult = sortExtract(a), bResult = sortExtract(b), result = 0;
        if (typeof aResult == 'string') result = aResult.localeCompare(bResult);
        else if (aResult == bResult) result = 0;
        else result = (aResult < bResult ? -1 : 1);
        return result * (ascending ? 1 : -1);
    }),
    wrapper = rows.first().parent();
sorted.stop().each(function () {
    this.dataset.top = jQuery(this).position().top;
}).detach();// TODO: Detach takes a chunk of time
wrapper.append(sorted).children().each(function () {
    var row = jQuery(this).css('top', ''),
        top = 0;
    try { top = parseInt(this.dataset.top) - row.position().top; }
    catch (e) { }// Just in case
    row.css({top: top});
}).animate({top: ''});

wrapper仅包含可排序的行。为什么detach需要这么长时间?我该如何解决它?

0 个答案:

没有答案
相关问题