使用 JQuery 在 Chrome 中调整图像大小
我今天刚刚遇到了最奇怪的错误。我不确定这是否是 Chrome 中的错误或我可以解决的问题,但这里是。
我构建了一个 JQuery 函数来调整论坛上发布的一组图像的大小:
$(".post-message img.post-image").load(function(){
$(this).each(function() {
var maxWidth = 200;
if($(this).width() > maxWidth)
{
var factor = $(this).width() / maxWidth;
var width = $(this).width();
var height = $(this).height();
$(this).css('width', width / factor);
$(this).css('height', height / factor);
}
});
});
问题是,这似乎只在我刷新页面时才起作用。当您按上一个或链接到该页面时,它不起作用。
在 chrome 中,当函数不起作用时,$(img).width()
属性在两种情况下都会返回 0。
此函数在 IE9 和 FF3 中按预期执行
我该如何解决此奇怪的行为?
I just ran into the weirdest of bugs today. I'm not sure if it's a bug in Chrome or something that I can work around but here goes.
I built a JQuery function to resize a set of images that are posted on a forum:
$(".post-message img.post-image").load(function(){
$(this).each(function() {
var maxWidth = 200;
if($(this).width() > maxWidth)
{
var factor = $(this).width() / maxWidth;
var width = $(this).width();
var height = $(this).height();
$(this).css('width', width / factor);
$(this).css('height', height / factor);
}
});
});
The problem is that this only seems to work when I refresh the page. It doesn't work when you press previous or when you get linked to the page.
In chrome the $(img).width()
property returns 0 in both cases when the function doesn't work.
This function performs as expected in IE9 and FF3
What can I do to fix this odd behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
很可能是因为图像是从浏览器缓存中提取的,并且
load
事件没有触发。解决此问题的方法是,如果已设置图像的complete
属性,则手动触发load
:Most probably because the images are being pulled up from the browser cache, and the
load
event is not triggering. The way around this is to manually triggerload
if the images'scomplete
properties have been set:卡敏在这里是正确的。几年前我遇到了这个问题,最终只是不依赖 img.load。他手动触发加载事件的解决方法应该有效。
但是......
在这种情况下,开发人员应该在 CSS 中设置 max-width 或 height。事实上,在使用 JavaScript 进行操作之前先使用 CSS 进行操作是一种很好的编程习惯。
此外,如果要继续使用此解决方案,则应将
var width
和var height
放置在 if 语句之外,靠近var maxWidth
,并在任何调用$(this).width()
的地方使用(包括第 4 行的初始检查)。现在,代码每次都不必要创建一个新的 jQuery 对象来获取高度,而此时它应该存储并使用第一次检查的值。Karmin is correct here. I ran into this problem a few years ago and ended up just not relying on img.load. His workaround for manually triggering the load event should work.
However...
Developers should do max-width or height in CSS in this scenario. In fact, it is good programming practice to do what one can in CSS before doing them in javascript.
Additionally, if one were to keep going with this solution,
var width
andvar height
should be placed outside of the if statement next tovar maxWidth
, and used wherever$(this).width()
is called (including the initial check on line 4). Right now the code is unnecessarily creating a new jQuery object to get the height each time when it should have stored and used the value from the first check.感谢各位的贡献。堆栈上给出的先前答案 - 今天下午我显然找不到 jQuery && Google Chrome - 解决了我的问题!
该代码必须与 karim79 提供的代码一起在
$(window).load()
上执行。Thanks for the contributions guys. A previous answer given on stack - that I apparently couldn't find this afternoon jQuery && Google Chrome - solved my problem!
The code has to be executed on
$(window).load()
together with the provided code by karim79.