这段代码有什么问题 [英] 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')
返回一个字符串,你需要将它转换为int才能用它来减去 newTop
$('friends').css('top')
returns a string, you'll need to convert this to an int before you can use it to subtract from newTop
parseInt($('friends').css('top'), 10)
可以做到这一点
您还需要在jQuery选择器中使用ID或类标识符,' #friends'
或'。friends'
但我想你正在寻找有朋友ID的东西
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
试试这个
width: "+="+((newTop - parseInt($('#friends').css('top'), 10))*3),
这篇关于这段代码有什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!