Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.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
Html 跨浏览器(chrome/firefox)试图以像素的百分比定义顶级位置_Html_Css_Google Chrome_Firefox - Fatal编程技术网

Html 跨浏览器(chrome/firefox)试图以像素的百分比定义顶级位置

Html 跨浏览器(chrome/firefox)试图以像素的百分比定义顶级位置,html,css,google-chrome,firefox,Html,Css,Google Chrome,Firefox,我在跨浏览器输出时遇到了一个问题,我试图获取div的左上方css属性,但是firefox给了我确切的像素位置,Chrome给了我百分比 例如: CSS jQuery css top: $("#mix").css("top") + " <br/>css left: " + $("#mix").css("left") css-top:$(“#mix”).css(“top”)+”css-left:+$(“#mix”).css(“left”) 输出 Firefox(和IE8): css

我在跨浏览器输出时遇到了一个问题,我试图获取div的左上方css属性,但是firefox给了我确切的像素位置,Chrome给了我百分比

例如:

CSS

jQuery

css top: $("#mix").css("top") + " <br/>css left: " + $("#mix").css("left")
css-top:$(“#mix”).css(“top”)+”
css-left:+$(“#mix”).css(“left”)
输出

Firefox(和IE8): css顶部:10px css左:267.3px

铬: css顶部:10px 左:45%


有没有办法让两个(所有)浏览器得到相同的结果?我更希望在不进行任何主要计算的情况下获得像素值

使用
位置
偏移
方法,具体取决于您希望的位置是相对于偏移父对象还是相对于文档

例如:

var p = $("#mix").position();
alert('top: ' + p.top + 'px, left: ' + p.left + 'px');
var p = $("#mix").position();
alert('top: ' + p.top + 'px, left: ' + p.left + 'px');