javascript - 在 jQuery 中制作转入/转出幻灯片

标签 javascript jquery css jquery-animate slideshow

我正在尝试在 jQuery/JavaScript 中创建转入/转出幻灯片。 我的问题是,它需要重复。现在,当它重新开始时,图片不再来自右侧 :(

我创建 slideLeft 函数的原因是,之后我需要创建 2 个函数,用户可以在其中手动按向左或向右按​​钮中断幻灯片放映。

这是我得到的:

<script class="jsbin" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>

<div style='background: #c4c4c4; border: 1px solid #7b7b7b; height: 220px; overflow: hidden; padding: 5px; position: absolute; width: 590px;'>
  <div id='slider-image-1' style='left: 5px; background: red; height: 216px; padding: 2px; position: absolute; top: 5px; width: 586px;'></div>
  <div id='slider-image-2' style='left: 600px; background: yellow; height: 216px; padding: 2px; position: absolute; top: 5px; width: 586px;'></div>
  <div id='slider-image-3' style='left: 600px; background: green; height: 216px; padding: 2px; position: absolute; top: 5px; width: 586px;'></div>
  <div id='slider-image-4' style='left: 600px; background: blue; height: 216px; padding: 2px; position: absolute; top: 5px; width: 586px;'></div>
</div>

<script type='text/javascript'>
  $(document).ready(function() {
    var amount = 0;
    var nextamount = 1;
    setInterval(function() {
      amount++;
      nextamount++;
      if (nextamount === 5) nextamount = 1;
      if (amount === 5) amount = 1;
        slideLeft(amount, nextamount);
      }, 2000);
    });

  function slideLeft(i, j) {
    var $theItem = $('#slider-image-' + i);
    $theItem.animate({
      left: parseInt($theItem.css('left'), 10) == 5 ? -$theItem.outerWidth() : 5
    }, 500);

    var $theItem = $('#slider-image-' + j);
    $theItem.animate({
      left: parseInt($theItem.css('left'), 10) == 5 ? $theItem.outerWidth() + 10 : 5
    }, 500);

  };
</script>

最佳答案

您需要准备要滚入的元素,使其位于右侧。

function slideLeft(i, j) {
  var $theItem = $('#slider-image-' + i);
  $theItem.animate({
    left: parseInt($theItem.css('left'), 10) == 5 ? -$theItem.outerWidth() : 5
  }, 500);

  var $theItem = $('#slider-image-' + j);
  $theItem.css('left', '600px'); // moves in item to the right before animation
  $theItem.animate({
    left: parseInt($theItem.css('left'), 10) == 5 ? $theItem.outerWidth() + 10 : 5
  }, 500);

};

我想你已经用你的 parseInt 试过了,但它不起作用,所以你可以去掉它。

function slideLeft(i, j) {
  var $outItem = $('#slider-image-' + i);
  $outItem.animate({ left: -$outItem.outerWidth() }, 500);

  var $inItem = $('#slider-image-' + j);
  $inItem.css('left', '600px');
  $inItem.animate({ left: 5 }, 500);
}

关于javascript - 在 jQuery 中制作转入/转出幻灯片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10858163/

相关文章:

javascript - 如何使用 Prometheus 测量请求的不同时间

html - 计算重叠的 rgba 值

javascript - 使用单个 URL 创建多页面设计

html - 在html中隐藏网页内容和侧边栏内容的导航栏

javascript - JavaScript 函数中的默认参数值

javascript - Laravel 5.6 Javascript 输入总和

javascript - 使默认值适用于对象参数的各个字段

javascript - jQuery EasyUI : confirmation on close window

javascript - SuperSized 的替代品

javascript - 单击时,添加类直到复选框/标签被单击