我正在为孩子们制作动画,但发现在连续循环中它会猛拉。在尝试使用边距后,我重新访问了 jquery 站点和 animate 的定义,我意识到 animate 的默认效果是“摇摆”,这就是导致我抽搐的原因。因此,我将代码修改为:
animateItem: function (self, speed) {
var $item = $(self.settings.itemClass).first();
var width = $item.outerWidth(true);
var left = parseInt($item.css("left"));
if (left < 0) {
self.moveItem(self, speed, 1);
} else {
$item.animate({ "left": "-" + width + "px" }, {
duration: speed,
easing: "linear",
step: function (now, fx) {
$(self.settings.itemClass + ":gt(0)").css("left", now);
}
});
}
},
moveItem: function (self, speed, count) {
if (count > 0) {
var $item = $(self.settings.itemClass).first();
$item.hide(speed, "linear", function () {
$(this).appendTo(self.settings.itemContainerClass).show(speed, "linear");
self.moveItem(self, speed, count - 1); // Repeat
});
}
},
这只是为单个对象设置动画,以了解对象的外观,这里有一些 html :)
<div id="carousel" class="carousel">
<div class="carousel-container">
<div class="carousel-item yellow">
<div class="carousel-photo">
<img src="http://www.csupomona.edu/~ieee/profile/2012/uman.png" />
</div>
<p>Hicks, Bill</p>
</div>
<div class="carousel-item green">
<div class="carousel-photo">
<img src="http://www.csupomona.edu/~ieee/profile/2012/uman.png" />
</div>
<p>Jeremy, Ron</p>
</div>
<div class="carousel-item blue">
<div class="carousel-photo">
<img src="http://www.csupomona.edu/~ieee/profile/2012/uman.png" />
</div>
<p>Little, John</p>
</div>
<div class="carousel-item violet">
<div class="carousel-photo">
<img src="http://www.csupomona.edu/~ieee/profile/2012/uman.png" />
</div>
<p>Smith, Will</p>
</div>
<div class="carousel-item purple">
<div class="carousel-photo">
<img src="http://www.csupomona.edu/~ieee/profile/2012/uman.png" />
</div>
<p>Shakespear, William</p>
</div>
<div class="carousel-item pink">
<div class="carousel-photo">
<img src="http://www.csupomona.edu/~ieee/profile/2012/uman.png" />
</div>
<p>Jameson, Jenny</p>
</div>
</div>
</div>
为了不断地制作动画,我创建了这个函数:
animateWheel: function (self, speed, count) {
var $item = $(self.settings.itemClass).first();
var width = $item.outerWidth(true);
$item.animate({ "left": "-" + width + "px" }, {
duration: speed,
easing: "linear",
step: function (now, fx) {
$(self.settings.itemClass + ":gt(0)").css("left", now);
},
complete: function () {
self.moveItem(self, speed, count);
}
});
},
我的选项如下所示:
defaults = {
itemsPerPage: 4.5,
itemContainerClass: '.carousel-container',
itemClass: '.carousel-item',
startButton: '#start',
carouselPage: '#carouselPage',
contentElement: '.ui-content'
};
希望这会让其他有同样问题的人知道该怎么做:) 我的下一个阶段是在动画停止之前尝试减慢动画(对于连续循环)