使用原型继承模式的随机移动动画
Random moving animation using prototypal inheritance pattern
我正在制作一个动画,每个列表项将在其包装器内随机浮动。我试着用原型继承模式组合在一起,但它没有这样做。我认为有几个地方可能是导致问题的原因,但我需要一些建议来继续进行。
function Menu($item) {
this.item = $item;
};
Menu.prototype.makeNewPosition = function ($container) {
var h = $container.height() - 50;
var w = $container.width() - 50;
var nh = Math.floor(Math.random() * h);
var nw = Math.floor(Math.random() * w);
return [nh, nw];
};
Menu.prototype.move = function () {
$.each(this.item, function(index, value) {
var newq = this.makeNewPosition(value.parent());
value.animate({
top: newq[0],
left: newq[1]
}, 400, function() {
this.move();
});
});
};
var $menu = new Menu($('.nav li'));
$menu.move();
在move
函数中,有animate
,在这个函数中,我再次调用move
来无限期地运行动画。如果在原型结构中调用自己,可以用this
来调用吗?
不确定$.each
在move
函数中的使用。
这是一个更简洁的版本
- 使用self代替-我使用
that
,大多数人使用self
-我不同:p - set
$value = $(value)
…干
.
function Menu($item) {
this.item = $item;
};
Menu.prototype.makeNewPosition = function ($container) {
var h = $container.height() - 50;
var w = $container.width() - 50;
var nh = Math.floor(Math.random() * h);
var nw = Math.floor(Math.random() * w);
return [nh, nw];
};
Menu.prototype.move = function () {
var self = this;
$.each(this.item, function(index, value) {
var $value = $(value);
var newq = self.makeNewPosition($value.parent());
$value.animate({
top: newq[0],
left: newq[1]
}, 400, function() {
self.move();
});
});
};
var $menu = new Menu($('.nav li'));
$menu.move();
这里是所有的错误修复,而不仅仅是第一个下面的原始答案-包含更改的解释
function Menu($item) {
this.item = $item;
};
Menu.prototype.makeNewPosition = function ($container) {
var h = $container.height() - 50;
var w = $container.width() - 50;
var nh = Math.floor(Math.random() * h);
var nw = Math.floor(Math.random() * w);
return [nh, nw];
};
Menu.prototype.move = function () {
var that = this; // added that because the this isn't the this you want in the $.each function
$.each(this.item, function(index, value) {
var newq = that.makeNewPosition($(value).parent());
// ^^^^ ^^ ^
// that instead of this
// $(value) instead of value
$(value).animate({
// ^^ ^
// $(value) instead of value
top: newq[0],
left: newq[1]
}, 400, function() {
that.move();
//^^^^ another this to that change
});
});
};
var $menu = new Menu($('.nav li'));
$menu.move();
有更多的方法来清理代码,但这些更改应该至少使您能够工作
小提琴的工作变化http://jsfiddle.net/wzmh63xb/16/
这是你的错误。试试这个。
Menu.prototype.move = function () {
var self = this;
$.each(this.item, function(index, value) {
var newq = self.makeNewPosition($(value).parent());
$(value).animate({
top: newq[0],
left: newq[1]
}, 400, function() {
self.move();
});
});
};
演示相关文章:
- 如何在JS中随机化backgroundPosition而不使用鼠标移动来制作万花筒的动画
- 随机jquery移动/角度内部错误与安卓浏览器
- 如何在Javascript中在桌子周围随机移动图像
- 使用单选按钮使 Div 在页面上随机移动
- 如何让 Div 使用单选按钮随机移动
- 不透明度+随机移动的随机计时
- 使用JavaScript和html将图像和超链接从一个位置随机移动到另一个位置
- 鼠标下移页面X,页面Y和鼠标移动页面X后页面Y不一样,随机发生
- 一些潜水器的起始位置和随机移动
- 如何让数组对象随机移动
- 用鼠标移动随机放置的盒子
- 如何移动一个图像随机当你点击另一个图像使用html &javascript
- 谷歌地图API移动到远标记在每次渲染(似乎随机)
- 使用原型继承模式的随机移动动画
- Kineticjs:画布中图像的随机移动动画
- 精灵图像的随机移动位置
- 使图像随机移动,点击
- 从Javascript中的坐标随机移动
- 随机移动的图像被屏幕的左上角吸引
- 在固定区域中随机移动图像