我正在构建一个在服务器上运行良好的 jQuery 插件。但是,如果我尝试在本地使用它,它会失败,因为宽度和高度返回零。违规代码是:
$("#off-screen").load(function() {
var naturalWidth = this.width;
var naturalHeight = this.height;
});
...应该获取 ID 为 off-screen 的动态加载的离视口(viewport)图像的宽度和高度。我正在使用 this.width 而不是 width();因为我在某处阅读它可能有助于缓存图像,但两种方式都不适用于本地。
在服务器上:http://kthornbloom.com/smoothzoom/
本地(从 Github 下载,您将看到图像无法在 Chrome 中正确缩放):https://github.com/kthornbloom/Smoothzoom
为什么完全相同的代码可以在服务器上运行,但不能在本地运行?
最佳答案
与其将 load
处理程序设置为不执行任何操作的匿名函数,不如将依赖于正在加载的图像的代码放入处理程序中。
这适用于我的机器:
$("#off-screen").load(function() {
$('#lightwrap img').css({
width: width,
height: height,
top: (offset.top - amountScrolled),
left: offset.left
});
imageSizer();
$('#lightbg').fadeIn();
event.preventDefault();
});
关于javascript - 为什么 jQuery 可以在服务器上计算宽度,但在本地运行时却不能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21194587/