公司有在做一个类似qq空间的开发,发表说说避免不了的要有图片展示。

产品提出的空间缩略图的展示类似*信朋友圈那种效果——图片不变形、能看到中间部分。

这里给出3种解决方案(jsbin地址失效时可复制代码到jsbin.com看效果):

1、 img + position + translate

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style>
.img_thum,.img_thum2,.img_thum3{
position:relative;
width:500px;
height:500px;
overflow:hidden;
border:1px solid red;
}
.img_thum img,
.img_thum2 img,
.img_thum3 img{
position:absolute;
top:50%;
left:50%;
transform: translate(-50%,-50%);
min-width: 100%; /* 针对小图标 */
min-height: 100%; /* 针对小图标 */
max-width: 200%; /* 针对太宽的图 -可能变形 */
max-height: 200%; /* 针对太高的图 -可能变形 */
}
</style>
</head>
<body>
<div class="img_thum">
<img src="http://img.88tph.com/subject/20181010/1258053ed87b46ef9341d3b72a2da682.jpg!/fw/300" alt=""> /* 300*235 */
</div>
<div class="img_thum2" style="margin-top:20px;">
<img src="http://img.88tph.com/homepage/20181029/4a856635a175482f9cc068d2c1e8b585.jpg!fw1200" alt=""> /* 1200*320 */
</div>
<div class="img_thum3" style="margin-top:20px;">
<img src="http://img.taopic.com/uploads/allimg/140724/235063-140H410412090.jpg" alt=""> /* 1000*1000 */
</div>
</body>
</html>

jsbin地址:https://jsbin.com/dakenupoqu/edit?html,output

可以看出,img和img_out大小差不多时显示符合要求,但img像素过大时,看到的缩略图就有点“管中窥豹”了...所以这种方案慎用!

2、background-imae + background-size + background-center

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>background-imae+background-size+background-center</title>
<style>
.img_thum,.img_thum2,.img_thum3{
position:relative;
width:500px;
height:500px;
overflow:hidden;
border:1px solid red;
background-size: cover;
background-position: center;
}
.img_thum{
background-image: url('http://img.88tph.com/subject/20181010/1258053ed87b46ef9341d3b72a2da682.jpg!/fw/300');
}
.img_thum2{
background-image: url('http://img.88tph.com/homepage/20181029/4a856635a175482f9cc068d2c1e8b585.jpg!fw1200');
}
.img_thum3{
background-image: url('http://img.taopic.com/uploads/allimg/140724/235063-140H410412090.jpg');
}
</style>
</head>
<body>
<div class="img_thum">
/* 300*235 */
</div>
<div class="img_thum2" style="margin-top:20px;">
/* 1200*320 */
</div>
<div class="img_thum3" style="margin-top:20px;">
/* 1000*1000 */
</div>
</body>
</html>

jsbin地址:https://jsbin.com/xamowokaki/edit?html,output

对比第一种方案,img和img_out只要比例差不多时显示就符合要求,不要求图片大小和显示区域大小差不多。但img像素过大,同时比例差太多时,看到的缩略图也会出现“管中窥豹”的现象。

这种方案算是最完美的实现了,但如果你有语义化强迫症,觉得缩略图属于内容,一定要用img标签,那就推荐第三种实现方式:

3、object-fit

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style>
.img_thum,.img_thum2,.img_thum3{
position:relative;
width:500px;
height:500px;
overflow:hidden;
border:1px solid red;
}
.img_thum img,
.img_thum2 img,
.img_thum3 img{
width:100%; /* 必须 */
height:100%; /* 必须 */
object-fit: cover;
}
</style>
</head>
<body>
<div class="img_thum">
<img src="http://img.88tph.com/subject/20181010/1258053ed87b46ef9341d3b72a2da682.jpg!/fw/300" alt=""> /* 300*235 */
</div>
<div class="img_thum2" style="margin-top:20px;">
<img src="http://img.88tph.com/homepage/20181029/4a856635a175482f9cc068d2c1e8b585.jpg!fw1200" alt=""> /* 1200*320 */
</div>
<div class="img_thum3" style="margin-top:20px;">
<img src="http://img.taopic.com/uploads/allimg/140724/235063-140H410412090.jpg" alt=""> /* 1000*1000 */
</div>
</body>
</html>

jsbin地址:https://jsbin.com/vulumexabo/edit?html,output

这种方案兼容性不是很好,效果类似第二种方案。

不知道object-fit是啥?链接送上:https://www.zhangxinxu.com/wordpress/2015/03/css3-object-position-object-fit/

兼容参考:https://blog.csdn.net/bigbear00007/article/details/80103109

最后补充一点,当图片的比例和规范相差很大时,是没有办法实现这2点需求的。所以,在作图时要注意了!

周记6——css实现类似朋友圈九宫格缩略图完美展示的更多相关文章

  1. Masonry 布局 cell 高度适应的一种方案(实现类似朋友圈简单布局)

    来源:伯乐在线 - 夏天然后 链接:http://ios.jobbole.com/89298/ 点击 → 申请加入伯乐在线专栏作者 前言: 我模仿的是微博的布局所以也就没有 评论动态刷新cell. 1 ...

  2. ios开发-日期处理(类似朋友圈,微博等的发送时间)

    ios开发中,我们经常要处理从服务器获取的时间.类似朋友圈,微博这些应用.我们经常可以看到“刚刚”,“31分钟前发表”,“昨天5点”,之类的字样. 当时我们从服务器端获取的都是那条朋友圈信息,或者微博 ...

  3. 如何使用捷映App制作朋友圈九宫格视频图片合集

    在刷朋友圈的过程中,看到九宫格的视频图片合集,带有酷炫的音效和动效,展现效果非常好,那么我们该如何制作呢?下面我为大家介绍. 第一步,我们需要在各大应用市场搜索[捷映视频制作]App,点击[安装]. ...

  4. iOS 类似朋友圈的图片浏览器SDPhotoBrowser

    SDPhotoBrowser.Demo 1.在文件SDBrowserImageView.m中有用SDWebImage到网络加载图片 需要的注释去掉即可 #import "ViewContro ...

  5. 纯html实现将网页页面分享到微信朋友圈添加缩略图图片的方法

    在分享页面中的body中最前面添加一个隐藏的div,里面放一个300*300的正方形图片即可:如下 <div style="display:none"><img ...

  6. Python 带你一键生成朋友圈超火的九宫格短视频

    1. 场景 如果你经常刷抖音和微信朋友圈,一定发现了最近九宫格短视频很火! ​从朋友圈九宫格图片,到九宫格视频,相比传统的图片视频,前者似乎更有个性和逼格 除了传统的剪辑软件可以实现,是否有其他更加快 ...

  7. css实现朋友圈照片排列布局

    纯css实现朋友圈不同数量图片不同布局 首先可以打开朋友圈观察不同图片数量的几种布局,也可参考下图示例: 可以发现 除1张图片,4张图片特殊外,其他数量图片均使用一行三列的方式排列: 假设有如下HTM ...

  8. Android NineGridLayout — 仿微信朋友圈和QQ空间的九宫格图片展示自定义控件

    NineGridLayout 一个仿微信朋友圈和QQ空间的九宫格图片展示自定义控件. GitHub:https://github.com/HMY314/NineGridLayout 一.介绍 1.当只 ...

  9. 用Python一键生成炫酷九宫格图片,火了朋友圈

随机推荐

  1. django def validate_column和validate

    VIewDemo class RegUserSet(mixins.CreateModelMixin,viewsets.GenericViewSet): serializer_class = RegUs ...

  2. WPF XamlObjectWriterException:无法创建未知类型"Grid"

    using (FileStream fs = new FileStream("UnitFile/Report2.xaml", FileMode.Open)) { rootEleme ...

  3. 描述一下 cookies,sessionStorage 和 localStorage 的区别?

    sessionStorage 和 localStorage 是HTML5 Web Storage API 提供的,可以方便的在web请求之间保存数据.有了本地数据,就可以避免数据在浏览器和服务器间不必 ...

  4. Spring jndi数据源配置方法

    xml配置: <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverMana ...

  5. 【转】如何成为一名优秀的web前端工程师(前端攻城师)?

    [转自]http://julying.com/blog/how-to-become-a-good-web-front-end-engineer/ 程序设计之道无远弗届,御晨风而返.———— 杰佛瑞 · ...

  6. Delphi中MessageBox用法

    消息框是个很常用的控件,属性比较多,本文列出了它的一些常用方法,及指出了它的一些应用场合. 1.最简单用法,不带图形 MessageBox(0,'不同意','提示',MB_OK); MessageBo ...

  7. ssh 使用密钥无法登入Linux系统

    今天测试密钥登入linux系统时 出现如下问题: root@compute01:~# ssh alicxxx@xxx.com -p -i alickicxxxxxxx.key @@@@@@@@@@@@ ...

  8. 【OCP-12c】2019年CUUG OCP 071考试题库(78题)

    78.View the exhibit and examine the structure of the CUSTOMERStable. Which two tasks would require s ...

  9. 操作日期时间类 Calendar类

    使用Calendar类可以直接创建Calendar的子类GregorianCalendar  来直接实例化, GregorianCalendar calendar = new GregorianCal ...

  10. vue 路由传参

      mode:路由的形式 用的哪种路由 1.hash 路由 会带#号的哈希值 默认是hash路由   2.history路由 不会带#的     单页面开发首屏加载慢怎么解决?单页面开发首屏加载白屏怎 ...