惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
小众软件
小众软件
Apple Machine Learning Research
Apple Machine Learning Research
Webroot Blog
Webroot Blog
T
The Blog of Author Tim Ferriss
S
Schneier on Security
罗磊的独立博客
P
Privacy & Cybersecurity Law Blog
阮一峰的网络日志
阮一峰的网络日志
F
Fortinet All Blogs
P
Palo Alto Networks Blog
月光博客
月光博客
MongoDB | Blog
MongoDB | Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Simon Willison's Weblog
Simon Willison's Weblog
量子位
NISL@THU
NISL@THU
C
CERT Recently Published Vulnerability Notes
Cisco Talos Blog
Cisco Talos Blog
Stack Overflow Blog
Stack Overflow Blog
Recorded Future
Recorded Future
K
Kaspersky official blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
L
Lohrmann on Cybersecurity
酷 壳 – CoolShell
酷 壳 – CoolShell
GbyAI
GbyAI
L
LINUX DO - 热门话题
Help Net Security
Help Net Security
B
Blog RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
I
InfoQ
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The Cloudflare Blog
H
Heimdal Security Blog
Last Week in AI
Last Week in AI
美团技术团队
云风的 BLOG
云风的 BLOG
C
Check Point Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
V
Visual Studio Blog
G
GRAHAM CLULEY
AI
AI
Security Latest
Security Latest
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
雷峰网
雷峰网
Cyberwarzone
Cyberwarzone
Microsoft Security Blog
Microsoft Security Blog
P
Proofpoint News Feed

博客园 - 令狐葱★

【备忘】12306购票必杀技 Github上最受关注的前端大牛,快来膜拜吧! Javascript知识点:IIFE - 立即调用函数 那些开源程序中让人叹为观止的代码 - 2 单例模式 那些开源程序中让人叹为观止的代码 - 1 浏览器特性判断 浏览器中F5和CTRL F5的行为区别 CR, LF, CR/LF区别与关系 利用 jQuery 克隆 Object Chrome下Failed to load resource问题居然是由于AdBlock WordPress在线安装插件 jQuery中的.height()、.innerHeight()和.outerHeight() jQuery中的attr()和prop() jQuery中的事件绑定函数.bind()、.live()、on()和.delegate() 实现ECMAScript 5中的Object.create()函数和Object.getPrototypeOf() 函数 Javascript全局变量和delete jQuery中$.proxy()的原理和使用 Javascript基类对象原型中有数组的情况 必须关注的25位知名JavaScript开发者 大坑贴:CSS浏览器兼容性问题及bug汇总
那些开源程序中让人叹为观止的代码 - 3 保持元素纵横比
令狐葱★ · 2014-04-10 · via 博客园 - 令狐葱★

本专栏尝试记录并分享一些个人在学习和使用开源程序代码的过程中,经意或者不经意间看到的个人感觉比较有参考价值的代码片段。个人感觉,并不是说能写或者能看得懂一些晦涩难懂的代码段子,就可以成为向别人炫耀的资本。本专栏无意炫技,其实也无技可炫。至于让某些牛人觉得有些小儿科,我只能说,“您老太认真了”。

保持元素纵横比

解决问题:页面尺寸变化(resize)时保持页面元素纵横比

开源程序:Bootstrap

经常,我们会遇到这样的需求,需要保持页面上某些元素在页面大小被改变重新渲染的时候,保持该元素区域的纵横比不变。最常见的场景,比如页面上显示图片的时候,希望始终保持图片的纵横比,哪怕图片以缩略图显示。或者,在页面上嵌入一段视频的时候,希望嵌入的这个元素区域始终保持16:9或者4:3的比例。

在Twitter的开源前端框架中,专门针对后边一个场景做了处理。

先看页面使用代码:

<!-- 16:9 aspect ratio -->
<div class="embed-responsive embed-responsive-16by9">
  <iframe class="embed-responsive-item" src="…"></iframe>
</div>

<!-- 4:3 aspect ratio -->
<div class="embed-responsive embed-responsive-4by3">
  <iframe class="embed-responsive-item" src="…"></iframe>
</div>

这里比较值得一提的就是样式类embed-responsive-4by3和embed-responsive-16by9的实现。看CSS代码:

.embed-responsive .embed-responsive-item,
.embed-responsive iframe,
.embed-responsive embed,
.embed-responsive object {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border: 0;
}
.embed-responsive.embed-responsive-16by9 {
  padding-bottom: 56.25%;
}
.embed-responsive.embed-responsive-4by3 {
  padding-bottom: 75%;
}

这里通过巧妙的使用padding-bottom这个属性,来保持元素的纵横比。比如embed-responsive-4by3,就是将width设置为100%,然后将padding-bottom设置为4:3对应的百分比,也就是75%就可以了。

这应该算是一个CSS Hack了。有很多人对其做过一些研究和讨论,一并列出供参考:

http://www.quora.com/Web-Development/What-are-the-most-interesting-HTML-JS-DOM-CSS-hacks-that-most-web-developers-dont-know-about

http://stackoverflow.com/questions/1495407/css-a-way-to-maintain-aspect-ratio-when-resizing-a-div

其中提到一个简单的示例程序,可以更容易看到其欲实现的效果:

http://jsbin.com/eqeseb/2/edit

示例程序