Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中从css添加位置_Javascript_Css_Add - Fatal编程技术网

在javascript中从css添加位置

在javascript中从css添加位置,javascript,css,add,Javascript,Css,Add,我试图编写一个小游戏,因此试图在某个区域移动一个对象。为了获得某种程度的边界,我正在尝试累积位置。然而,我将为您简化这一点,这是不起作用的:在JS中 parseInt( $('#w'+w ).css( 'top' ) + $('#w'+w ).css( 'height' ) ) 这应该只是将顶部位置与此元素的高度相加。当我把它打印出来时,它会告诉我它是100。但是,当我打印height和top而不将它们相加时,它的height=500和top=100在css代码中就是这样。 如果我交换高度和顶

我试图编写一个小游戏,因此试图在某个区域移动一个对象。为了获得某种程度的边界,我正在尝试累积位置。然而,我将为您简化这一点,这是不起作用的:在JS中

parseInt( $('#w'+w ).css( 'top' ) + $('#w'+w ).css( 'height' ) )
这应该只是将顶部位置与此元素的高度相加。当我把它打印出来时,它会告诉我它是100。但是,当我打印height和top而不将它们相加时,它的height=500和top=100在css代码中就是这样。
如果我交换高度和顶部,加起来,结果是500,所以第一个元素。我得到了类似的计算结果,这很合适。有什么建议我的代码出了什么问题吗?

您可能正在连接字符串:

// $('#w' + w).css('top') returns the string "100px"
// $('#w' + w).css('height') returns the string "500px"
parseInt("100px" + "500px") // 100
parseInt("500px" + "100px") // 500
我建议您将代码更改为:

parseInt($('#w' + w).css('top')) + parseInt($('#w' + w).css('height'))
对于顶部使用位置:

var position = $('#w'+w ).position();
console.log( "left: " + position.left + ", top: " + position.top );
对于高度或宽度,请使用jQuery宽度和高度:

console.log( "width: " +$('#w'+w ).width() + ", height: " + $('#w'+w ).height() );

感谢parseInt-parseInt$'w'+w.css'top'+parseInt$'w'+w.css'height'帮助我们感谢您。是否每个css订单都有一个“函数”?