$( document ).ready(function() {
$('body').append('<div id="tester2"></div>');
$('#tester2').css({
position:'absolute',
background:'blue',
width: 10,
height:10
});
setInterval(function(){
var x = $('#tester')[0].getBoundingClientRect();
$('#tester-pos').text('top: ' + x.top + ', left:' + x.left);
$('#tester2').css({
top:x.top,
left:x.left
});
}, 1000);
$('#jquery-version').text('jquery version: ' + $.fn.jquery);
});
#tester{
position:absolute;
top:50px;
left:50px;
width:10px;
height:10px;
background:red;
}
#page{
min-height:200px;
}
body{
border:2px solid green;
transform: scale(1) translate(20px, 40px);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="tester">
</div>
<div id="page">
getBoundingClientRect on red tester returned:
<span id="tester-pos"></span>
<div id="jquery-version"></div>
</div>
我需要div
在现有的div
. 现有div
的包含在具有 CSS 转换属性集的 HTML 文档的正文中。我需要div
在文档渲染和转换后放置新的。
当我调用我需要隐藏的东西(附加小提琴中的红色方块)时,我得到了错误的顶部/左侧getBoundingClientRect()
。div
我将蓝色方块的顶部/左侧设置为输出,getBoundingClientRect()
它们不重叠。
setInterval(function(){
var x = $('#tester')[0].getBoundingClientRect();
$('#tester-pos').text('top: ' + x.top + ', left:' + x.left);
$('#tester2').css({
top:x.top,
left:x.left
});
}, 1000);
如何解决?