【发布时间】:2016-03-11 04:02:16
【问题描述】:
我知道,以前有人问过。答案是不充分的,尽管它们被标记为正确。
我需要通过显示图像的大部分、居中并保持其比例来用图像填充容器的东西。应该是 ajquery-plugin、角度指令或普通 JS。
<div ratio-fill>
<img>
</div>
当然应该足够了,然后脚本会采取行动。
兴趣解决方案:
<div ratio-fill="http://img.url"></div>
CSS
*[ratio-fill] { background-size: cover;
background-position: center; }
脚本 (jQuery)
/* img-ratio-fill */
$(window).load(function(){
/* ratio-fill-directive */
$('*[ratio-fill]').each(function() {
var $this = $(this),
imgUrl = $this.attr( "ratio-fill" );
$this.css( "background-image", "url('" + imgUrl + "')" );
});
})
【问题讨论】:
-
你能用
background-size: cover代替吗? -
您能否具体说明哪些答案不充分以及为什么?
标签: javascript jquery html