循环旋转木马水平javascript/jquery



我想知道是否有人可以帮我写这个旋转木马的循环?目前,旋转木马每3秒向右滚动一次,然后再向左滚动一次,然后重置自己,我只是想让它无限循环,这样看起来更干净,谁能给我指出正确的方向或帮助我?我知道它更简单,但我不是一个js开发人员!(这是谷歌网站的HTML框,否则我会使用jquery插件)

<style>
 .carousel {
  width: 1080px;
  height: 220px;
  position: relative;
  overflow: hidden;
  background-color:white;
  margin-bottom: 20px;
  margin-top: 20px;
  margin-left: 70px;
 }
 .items {
  width: 1080px;
  position: absolute;
 }
 .items > div {
  font-size: 20px;
  display: table-cell;
  vertical-align: middle;
  text-align: center;
 }
 .items > div > img {
  padding: 10px;
 }
 .nav {
  position: absolute;
  bottom: 5px;
  right: 15px;
 }
 .button {
  cursor: pointer;
  font-weight: bold;
  color: #fff;
 }
</style>
<div class="carousel" style="display:none;">
 <div class="items">
  <div>
    <img src="http://i59.tinypic.com/etisye.png" border="0" alt="Alkamai Logo">
  </div>
  <div>
   <img src="http://i59.tinypic.com/ouukxu.png" border="0" alt="AWS Logo">
  </div>
  <div>
    <img src="http://i61.tinypic.com/16k3t43.png" border="0" alt="cover-it-live">
  </div>
  <div>
    <img src="http://i60.tinypic.com/23wljxh.png" border="0" alt="escenic">
  </div>
  <div>
    <img src="http://i58.tinypic.com/sbiqu1.png" border="0" alt="Livefire">
  </div>
  <div>
    <img src="http://i58.tinypic.com/do9wep.jpg" border="0" alt="ooyala">
  </div>
  <div>
    <img src="http://i61.tinypic.com/24werue.png" border="0" alt="varnish">
  </div>
  <div>
    <img src="http://i60.tinypic.com/2ij14rd.png" border="0" alt="wordpress">
  </div>
 </div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"> </script>
<script>
 var current_slide = 0; // zero-based
 var slide_count = 4;
 var slide_size = 1080;
 var Direction = {
  LEFT: -1,
  RIGHT: 1
 };
 /**
 * Moves to the next slide using the direction (dx) parameter.
 */
 var nextSlide = function(dx) {
  current_slide = (current_slide + slide_count + dx) % slide_count;
  // Calculate the new value for css 'left' property and animate.
  var left_offset = '-' + (current_slide * slide_size) + 'px';
  $('.items').animate({'left': left_offset}, 1080);
 };
 $('.carousel').show();
 setInterval(function(){
    nextSlide(Direction.RIGHT);
  }, 3000);
</script>

对当前脚本稍加修改就可以使其连续向前运行。

变化如下:

  1. current_slide始终为1(以便始终只向前移动)
  2. 当我们将.items X像素向左移动时,我们将相应数量的项目移动到末尾(X像素宽度内的数量)

更新的演示:http://jsfiddle.net/techfoobar/dWy9R/4/

代码:

var parent = $('.items');
var nextSlide = function (dx) {
    // NOTE: always move forward only
    current_slide = 1; //(current_slide + slide_count + dx) % slide_count;
    // Calculate the new value for css 'left' property and animate.
    var ileft_offset = current_slide * slide_size,
        left_offset = '-' + ileft_offset + 'px',
        iWidth = 0;
    parent.animate({
        'left': left_offset
    }, 'slow', function() { // called when animation is done
        iWidth = parent.find('> div:first').width();
        while(ileft_offset > iWidth) {
            parent.find('> div:first').appendTo(parent);
            ileft_offset -= iWidth;
            parent.css('left', '-' + ileft_offset + 'px');
        }
    });
};

修改后的版本,中间不停顿。

演示:http://jsfiddle.net/techfoobar/dWy9R/5/

var nextSlide = function () {
    parent.animate({
        'left': '-' + slide_size + 'px'
    }, 4000, 'linear', function() { // called when animation is done
        var ileft_offset = slide_size,
            iWidth = parent.find('> div:first').width();
        while(ileft_offset > iWidth) {
            parent.find('> div:first').appendTo(parent);
            ileft_offset -= iWidth;
            parent.css('left', '-' + ileft_offset + 'px');
            iWidth = parent.find('> div:first').width();
        }
        nextSlide();
    });
};
nextSlide(); // start it off!

最新更新