1
后,我在与jQuerys offset()
偏移()元素的前面加上另一个元素
我有一些<img>
文档中的一个问题。在文档准备就绪后,我将该图像封装到一个div中并添加一些文本。然后我需要图像偏移。
如何在预先完成后得到图像的偏移量?
这里是一个示例代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<title>Image Offset Problem!?</title>
<script type="text/javascript">
$(document).ready(function(){
$img = $('#demoimg');
// I hope this has nothing to do with it... it's just too big
$img.width(800);
var $prependDiv = $('<div>Some text</div>');
var $wrap = $img.wrap('<div id="wrapper">').parent();
//$wrap.prepend($prependDiv);
// Here I need to use the FINAL offset, the one with the value that shows when we click the button.
// Instead, I get something else!
var imgOffset = $img.offset();
console.log('offset().top = ' + imgOffset.top);
});
</script>
</head>
<body>
<img id="demoimg" src="demoimg.jpg" />
<input type="button" value="View image offset().top" onclick="calculateOffset()" />
</body>
<script type="text/javascript">
function calculateOffset() {
console.log('offset().top = ' + $('#demoimg').offset().top);
};
</script>
我需要访问offset()
.TOP那里的$(document).ready()
功能。
有没有办法做到这一点? 谢谢。
我重新打开你的问题。请添加您的解决方案作为答案。 –