Chrome 中的流体图像:如何避免重绘?
我正在一个有很多图像的摄影网站上工作,它们没有固定的高度和宽度,因为我希望这个网站 100% 流畅:你如何解决丑陋的 Chrome 重新绘制图像的问题? (即图像首先以零高度显示,然后在整个布局中移动到最终尺寸)
我已经尝试了几乎所有方法,我的最后一个选择是用黑色 div 隐藏图像重绘,然后将其不透明度设置为 0当图像加载完成时(顺便说一句,我已经通过 (document).ready 调用尝试过此操作,但似乎太早了:你会怎么做?)
I'm working on a photography site with a lot of images and they have no fixed height and width as I want this site to be 100% fluid: how do you work around the ugly Chrome repaint of the images? (i.e. Images are first displayed at zero height and then rescaled to their final size moving around the entire layout)
I've tried pretty much everything and my last option is to hide the image repaint with a black div and then set its opacity to 0 when images are finished loading (BTW, I've tried this with a (document).ready call but it seems too soon: how would you do it?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
指定图像的高度和宽度属性/尺寸。
这有助于浏览器避免第二次布局页面,并优化页面加载! <代码>:)
Specify your image's height and width attribute / its dimensions.
This helps the browser avoid a second pass to layout your page and it optimizes page load as well!
:)
Chrome(或任何浏览器)无法避免这种“重新绘制”,因为它们不知道您的图像的大小。
因此,您需要在图像宽度和高度属性本身中或通过 CSS 显式指定图像的大小。
Chrome (or any browser really) cannot avoid this 'repainting', since they don't know on forehand what size your images will be.
Thus, you will need to explicitly specify the sizes of your images, either in the image width and height properties itself, or via CSS.
我知道我迟到了两年多,但是建议的做法怎么样
另外,评论部分的一个重要评论需要注意并改进原始技术:
我希望有人会觉得这很有用。
I know I am more than two years late, but how about the practice suggested here?
Also, an important remark from the comments section to pay attention to, and improve upon the original technique:
I hope someone will find this useful.
这很难测试,但
如果你希望图像是全宽的,你可以尝试在 CSS 中设置宽度/高度。这可能会阻止整页重新绘制,但当然,无论图像加载如何,都会进行一些重新绘制。您还可以调查 Chrome 的
--show-paint-rects
希望有帮助
It’s hard to test, but you could try setting width/height in CSS
if you want the images to be full-width. This might prevent a full-page repaint, but of course there’ll be some repaint regardless as images load. You can also investigate what’s happening with Chrome’s
--show-paint-rects
Hope that helps