[英]What is wrong with this code
function moveit() {
var newTop = Math.floor(Math.random()*350);
var newLeft = Math.floor(Math.random()*1024);
var newDuration = 9000
$('#friends').animate({
top: newTop,
left: newLeft,
!!! --> width: "+="+((newTop-$('friends').css('top'))*3),
}, newDuration, function() {
moveit();
});
}
$(document).ready(function() {
moveit();
});
It is supposed to make an image fly around (works). 它应该使图像飞来飞去(工作)。 I added the line marked with the "!!! -->" that should make the image get bigger the closer it is to the bottom of the page. 我添加了标有“!!! - >”的行,它应该使图像越接近页面底部。
What did I do wrong? 我做错了什么? The code doesn't throw any errors. 代码不会抛出任何错误。
$('friends').css('top')
returns a string, you'll need to convert this to an int before you can use it to subtract from newTop
$('friends').css('top')
返回一个字符串,你需要将它转换为int才能用它从newTop
减去
parseInt($('friends').css('top'), 10)
Would do the trick 会做的伎俩
Also you need to use an ID or class identifier in your jQuery selector, either '#friends'
or '.friends'
but I imagine you're looking for something with the ID of friends 你还需要在jQuery选择器中使用ID或类标识符, '#friends'
或'.friends'
'#friends'
,但我想你正在寻找具有朋友ID的东西
Try this 尝试这个
width: "+="+((newTop - parseInt($('#friends').css('top'), 10))*3),
Don't you mean $('#friends')
instead of just plain $('friends')
? 你不是指$('#friends')
而不仅仅是简单的$('friends')
? Your statement is looking for an element tag "friends" and trying to get it's top
CSS property value. 您的声明正在寻找元素标签“friends”并试图获得它的top
CSS属性值。
I'd suggest you also cache the $('friends')
selector so that you don't have to select it twice for the .animate()
call. 我建议您也缓存$('friends')
选择器,这样您就不必为.animate()
调用选择它两次。
Pretty sure your problem is .css("top")
will return a string value like 100px
. 很确定你的问题是.css("top")
将返回一个像100px
这样的字符串值。 You want to use .position().top
instead to get the integer value instead. 您想使用.position().top
代替获取整数值。
I think your issue is in how you're getting the top position. 我认为你的问题在于如何获得最高职位。 May not be exactly what you want, but I got it working here... 可能不是你想要的,但我在这里工作......
http://jsfiddle.net/shaneblake/YXMbh/ http://jsfiddle.net/shaneblake/YXMbh/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.