简体   繁体   English

Javascript 调整大图像大小仅在 F5 后有效

[英]Javascript resize big image only works after F5

I use this javascript to resize, to a max of 800px, a big (5Mb) image on a page:我使用此 javascript 将页面上的大(5Mb)图像调整为最大 800 像素:

 <script type="text/javascript">
 $(document).ready(function () {
     $('.detailImg').each(function () {
         var maxWidth = 800; // Max width for the image
         var maxHeight = 800;    // Max height for the image
         var ratio = 0;  // Used for aspect ratio
         var width = $(this).width();    // Current image width
         var height = $(this).height();  // Current image height

         // Check if the current width is larger than the max
         if (width > maxWidth) {
             ratio = maxWidth / width;   // get ratio for scaling image
             $(this).css("width", maxWidth); // Set new width
             $(this).css("height", height * ratio);  // Scale height based on ratio
             height = height * ratio;    // Reset height to match scaled image
             width = width * ratio;    // Reset width to match scaled image
         }

         // Check if current height is larger than max
         if (height > maxHeight) {
             ratio = maxHeight / height; // get ratio for scaling image
             $(this).css("height", maxHeight);   // Set new height
             $(this).css("width", width * ratio);    // Scale width based on ratio
             width = width * ratio;    // Reset width to match scaled image
         }
     });
 });

When page is loaded the image is not resized.加载页面时,图像不会调整大小。 No errors on FireBug console. FireBug 控制台上没有错误。 If I try to refresh the page with F5, the image get resized, If I try Ctrl + F5, images return at original size!如果我尝试用 F5 刷新页面,图像会调整大小,如果我尝试 Ctrl + F5,图像会返回原始大小!

Where is the problem?问题出在哪里? Why this javascript only works when the page is refreshed?为什么这个 javascript 只在页面刷新时有效?

Your images aren't loaded when your document.ready() runs.当您的document.ready()运行时,您的图像不会被加载。 document.ready() fires on load of the DOM, but at that point the images may still be loading -- especially if they're 5MB, Because of this. document.ready()在加载 DOM 时触发,但此时图像可能仍在加载——特别是如果它们是 5MB,因此。 the image height and width aren't available at the point your script is running.图像高度和宽度在您的脚本运行时不可用。 I'd guess it's working on F5 because the images are still cached, so are then loaded fast enough for their dimensions to be known when your script runs.我猜它在F5上工作,因为图像仍在缓存中,因此加载速度足够快,以便在脚本运行时知道它们的尺寸。 Ctrl - F5 clears the image cache, so after that, you're back where you started. Ctrl - F5清除图像缓存,所以在那之后,你又回到了你开始的地方。

As I noted in my comment, using the load() event of an image may help, but even then, sometimes it's not triggered properly.正如我在评论中指出的那样,使用图像的load()事件可能会有所帮助,但即便如此,有时它也没有被正确触发。 For example, some browsers will never fire the load() event for an image that's already in the browser cache (as, technically, it's not been loaded...)例如,某些浏览器永远不会为已经在浏览器缓存中的图像触发load()事件(因为从技术上讲,它没有被加载......)

There are a few different workarounds for this doing the rounds -- Paul Irish's plugin might be useful;有几种不同的解决方法可以循环使用——Paul Irish 的插件可能有用; include the plugin code and then use something like:包括插件代码,然后使用类似的东西:

 $('.detailImg',this).imagesLoaded(function() {
   // Your existing function code
 });

Alternatively, if you know the image size in advance, just specify it in your img element, eg <img src="whatever" height="1200" width="1600" alt="whatever" /> .或者,如果您事先知道图像大小,只需在您的img元素中指定它,例如<img src="whatever" height="1200" width="1600" alt="whatever" /> That way jQuery will be able to tell the width and height whether the image is loaded or not.这样 jQuery 将能够判断图像是否加载的宽度和高度。

Put your JS code just before the "body close tag".将您的 JS 代码放在“正文关闭标记”之前。

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM