2012-11-14 40 views
0

我竭力拿出这一个干净的解决方案:清洁数学的解决方案来遍历圆形转盘

  • 转盘有7个项目,所以0 - 6
  • 指数3是中间的一个
  • 如果例如第二个项目(索引1)被点击,则每个项目需要向右移动2个位置。如果最后一项被点击(索引6),则需要将3个位置移动到左侧。
function centerCarouselOn(index, callback) { 
    var items = $('li', carousel); 
    var middleIdx = Math.floor(items.length/2); 
    var direction = null; 
    var iterCount = 0; 

    if(index === middleIdx) return; 

    if(index > middleIdx) { 
    direction = 'left'; 
    iterCount = (index - middleIdx); 
    } 
    else { 
    direction = 'right'; 
    iterCount = (middleIdx - index); 
    } 

    $('li', carousel).each(function(k, v) { 
    var li = $(v); 

    // Here I need to iterate n places to the left or right 
    // e.g: 
    // direction = left, iterCount = 3 
    // Then each li by index would need this sequence: 
    // 0: 6, 5, 4 
    // 1: 0, 6, 5 
    // 2: 1, 0, 6 
    // 3: 2, 1, 0 
    // 4: 3, 2, 1 
    // 5: 4, 3, 1 
    // 6: 5, 4, 3 (this one moves to center - index 3) 
    }); 

}
+0

您可以在这里查看我的方法:http://github.com/mgechev/jqcarousel。这是一个演示:http://carousel.mgechev.com/ –

+0

“然后每个索引都需要这个序列”。为什么是序列? –

+0

因为传送带是不同形状和大小的人,所以有一个默认的动画路径,但是然后每个人也可以覆盖此路径(合并到默认路径)以使其看起来更好。所以为了看起来很好,每个人都必须通过这些路径。 +谢谢@MinkoGechev,我无法解决我需要的更简单,不太通用的解决方案,但仍然是一些漂亮的代码! –

回答

1

这不包括对于动画的任何代码,并且还假定carousel元件是父的<li> S的<ul>

应该对于每个“移动”都不需要改变每个<li>的标记。你真正需要做的是将最后一个元素移动到第一个位置,或者将第一个元素移动到最后一个位置(取决于你移动的方向)。我还添加了1秒的timeout,所以你应该能够看到它逐步完成。

function centerCarouselOn(index, callback) { 
    var items = $('li', carousel); 
    var middleIdx = Math.floor(items.length/2); 
    var direction = null; 
    var iterCount = 0; 

    if(index === middleIdx) return; 

    // if iterCount is positive, we are going right; else, we are going left 
    iterCount = middleIdx - index; 

    // this funciton gets called recursively until all moves are complete 
    function moveCarousel() { 
    if (iterCount===0) return; 

    if (iterCount > 0) { 
     // take the last element, prepend it to the carousel 
     $('li', carousel).last().prependTo(carousel); 
     iterCount--; 
    } else if (iterCount < 0) { 
     // take the first element, append it to the carousel 
     $('li', carousel).first().appendTo(carousel); 
     iterCount++; 
    } 

    // execute callback to apply css changes at each step 
    callback(); 

    // set a delay, then repeat. 
    window.setTimeout(moveCarousel, 1000); 
    } 

    // start moving the carousel 
    moveCarousel(iterCount); 
} 
+0

谢谢我感谢你花时间回答这个问题。不幸的是,我确实需要逐步移动,因为在每个阶段都有一组特定的CSS,每个元素都必须经过!编辑 –

+0

以将调用添加到您传入“centerCarouselOn”的回调方法中。我认为这是你应用增量CSS更改的地方?我相信这应该起作用。 – madlee

+0

我没有使用你的代码,但事实证明,动画看起来好多了,如果它没有在两者之间动画,但一路走到它应该去的地方,所以我会选择它作为答案 –

0

感谢大家的关注,这是我做的到底:

function centerCarouselOn(index, callback) { 
    var items = $('li', carousel); 
    var numItems = carousel.children().length; 
    var middleIdx = Math.floor(items.length/2); 
    var direction = null; 
    var iterCount = 0; 

    if(index === middleIdx) return; 

    if(index > middleIdx) { 
    direction = 'left'; 
    iterCount = (index - middleIdx); 
    } 
    else { 
    direction = 'right'; 
    iterCount = (middleIdx - index); 
    } 

    $('li', carousel).each(function(k, v) { 
    var li = $(v); 

    // Here I need to iterate n places to the left or right 
    // e.g: 
    // direction = left, iterCount = 3 
    // Then each li by index would need this sequence: 
    // 0: 6, 5, 4 
    // 1: 0, 6, 5 
    // 2: 1, 0, 6 
    // 3: 2, 1, 0 
    // 4: 3, 2, 1 
    // 5: 4, 3, 1 
    // 6: 5, 4, 3 (this one moves to center - index 3) 

    if(direction === 'right') { 
     for(var i = k; i < (k + iterCount); i++) { 
     var thisIter = i; 
     var nextIter = (++thisIter >= numItems) ? (thisIter - numItems) : thisIter; 

     console.log(k + ': ' + nextIter); 

     } 
    } 
    else { 
     for(var i = k; i > (k - iterCount); i--) { 
     var thisIter = i; 
     var nextIter = (--thisIter < 0) ? (numItems + thisIter) : thisIter; 

     console.log(k + ': ' + nextIter); 
     } 
    } 
    }); 
} 

原来动画看起来废话这样所以不是我只是计算最终位置(这是作为奖励效率更高):

$('li', carousel).each(function(k, v) { 
    var li = $(v); 

    var nextIdx = 0; 

    if(direction === 'right') { 
    nextIter = ((k + iterCount) > (numItems - 1)) ? ((k + iterCount) - numItems) : (k + iterCount); 
    } 
    else { 
    nextIter = (k - iterCount) >= 0 ? (k - iterCount) : numItems - (iterCount - k); 
    } 

    _animateTo(li, nextIter, direction); 
});
相关问题