Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我试图相对于图像元素定位 div。问题是,图像需要一段时间才能加载,我正在获取图像替代文本的偏移坐标。我使用 jquery 的 offset 方法来获取图像的位置,使用 jquery 的 ready() 方法调用。
我尝试使用绑定到图像的 load() 方法(并删除在 ready() 方法上所做的定位),但 div 没有正确定位,甚至没有按照图像的 alt 文本的坐标定位。
有任何想法吗?帮助?
试试这个。在设置 div 宽度的函数中,创建 n 个图像对象,如下所示:
var img = new Image(); img.src = "http://path.to/image.png"; // TODO: always use pngs... img.onload = function(){ // your existing code here }
然后,当加载图像时,代码将执行。你也不应该有两次加载图像的问题。其中一个(以最后加载的为准)将基于缓存的版本。