【发布时间】:2014-02-19 23:34:28
【问题描述】:
我正在使用一些 javascript 来检查我所有的图像的宽度并根据需要添加一个类。
看起来像这样:
$(document).ready(function(){
// check each image in the .blogtest divs for their width. If its less than X make it full size, if not its poor and keep it normal
var box = $(".blogtest");
box.find("img.buildimage").each(function() {
var img = $(this), width = img.width();
if (width >= 700) {
img.addClass("buildimage-large");
} else if (width < 700) {
img.addClass("buildimage-small");
}
});
});
问题是,当您第一次访问页面时,图像没有添加类,而是仅在您刷新页面时才起作用。
有什么解决办法吗?
【问题讨论】:
-
@zer02 媒体查询对图像宽度没有帮助
标签: javascript jquery css image