首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >页面加载时Picturefill 2 IMG高度跳跃

页面加载时Picturefill 2 IMG高度跳跃
EN

Stack Overflow用户
提问于 2014-08-07 18:35:53
回答 1查看 119关注 0票数 0

当使用Picturefill 2时,我会在页面加载时得到图像上的布局跳转,例如,当你没有在正常图像上指定高度时。这是Picturefill所期望的吗?有没有解决这个问题的方法?谢谢。

EN

回答 1

Stack Overflow用户

发布于 2015-07-14 00:09:38

您所描述的问题不一定仅限于Picturefill。为了避免这个问题,您可以计算图像的高度与宽度的比率。即使为响应站点加载图像,尽管您可能因为屏幕大小而不知道图像尺寸,但此比例将是相同的。例如,高度为200px,宽度为300px:

代码语言:javascript
复制
200px / 300px * 100 = 66.66666667

在Picturefill元素周围创建一个容器div

代码语言:javascript
复制
<div class="image-container">
   <picture>
     <source srcset="examples/images/extralarge.jpg" media="(min-width: 1000px)">
     <source srcset="examples/images/art-large.jpg" media="(min-width: 800px)">
     <img srcset="examples/images/art-medium.jpg" alt=" ">
    </picture>
</div>

并使用以下CSS

代码语言:javascript
复制
.image-container {
  position: relative;
  padding-bottom: 66.66666667%; /* ratio of image height to width */
  height: 0;
  overflow: hidden;
}

.image-container img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
}

这应该行得通。有关更多信息,请查看此blog post和此CodePen示例。

编辑

如果响应图像加载的图像宽度和高度的比率不同,则此方法不起作用。我刚遇到这种情况,所以先提个醒。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25180289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档