【问题标题】:Clean math solution to iterate circular carousel清洁数学解决方案以迭代循环轮播
【发布时间】:2012-11-02 23:35:43
【问题描述】:

我正在努力为此想出一个干净的解决方案:

  • 轮播有 7 个项目,所以 0 - 6。
  • 索引 3 是中间的索引
  • 例如,如果单击第二个项目(索引 1),则每个项目需要向右移动 2 个位置。如果单击最后一项(索引 6),则需要向左移动 3 个位置。
function centerCarouselOn(index, callback) {
  var items = $('li', carousel);
  var middleIdx = Math.floor(items.length / 2);
  变种方向=空;
  var iterCount = 0;

  if(index === middleIdx) 返回;

  如果(索引> middleIdx){
    方向='左';
    iterCount = (index - middleIdx);
  }
  别的 {
    方向='右';
    iterCount = (middleIdx - 索引);
  }

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

    // 这里我需要向左或向右迭代n个地方
    // 例如:
    // 方向 = 左,iterCount = 3
    // 然后每个 li 按索引将需要这个序列:
    // 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(这个移动到中心 - 索引 3)
  });

}

【问题讨论】:

  • 你可以在这里查看我的方法:github.com/mgechev/jqcarousel。这是一个演示:carousel.mgechev.com
  • “那么索引的每个 li 都需要这个序列”。为什么是序列?
  • 因为轮播是不同形状和大小的人,所以有一个默认的动画路径,但是每个人也可以覆盖这个路径(合并到默认路径),让它看起来更好。因此,为了看起来不错,每个人都必须通过这些路径。 + 感谢@MinkoGechev,我无法为我的更简单、更通用的解决方案提取所需的内容,但仍然是一些漂亮的代码!
  • 所以基本上,在你的例子中,有人点击了第 6 个人,与旋转木马的前 li 相比,它恰好在右边 3 个位置,你想将每个人移动 3 次 1 个位置到左边,右边?
  • @EvrenKuzucuoglu 是的

标签: javascript for-loop carousel


【解决方案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);
}

【讨论】:

  • 感谢您花时间回答这个问题。不幸的是,我确实需要逐步移动,因为每个元素必须通过的每个阶段都有一组特定的 css!
  • 编辑添加对回调方法的调用,您传递到centerCarouselOn。我假设这是您应用增量 CSS 更改的地方?我相信这应该可行。
  • 我没有使用你的代码,但事实证明,如果动画中间没有动画效果会更好,但会一直到应该去的地方,所以我会选择它作为答案
【解决方案2】:

感谢大家的关注,我最后是这样的:

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;

  如果(方向==='对'){
    nextIter = ((k + iterCount) > (numItems - 1)) ? ((k + iterCount) - numItems) : (k + iterCount);
  }
  别的 {
    nextIter = (k - iterCount) >= 0 ? (k - iterCount) : numItems - (iterCount - k);
  }

  _animateTo(li, nextIter, 方向);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    相关资源
    最近更新 更多