我有下一个脚本。它在img加载之前有效,你使用方法加载。如何在加载img后强制脚本工作?
$(document).ready(function() {
$('.slide-img img').load(function(){
var height = $(this).height();
var width = $(this).width();
if (width > height) {
$(this).attr('style', 'max-height: 100%');
};
if (height > width) {
$(this).attr('style', 'max-width: 100%');
};
});
});
我怎么能加载img后强制脚本工作?
http://stackoverflow.com/questions/280049/javascript-当图像被加载时回调图像 –
您可能想要查看某些图像预加载库以实现此目的。您的代码只能在DOM准备就绪时运行。 http://www.createjs.com/preloadjs可能会矫枉过正,但绝对是一种选择。 –
你的代码看起来很好。检查您的选择器是否正确。 –