I know, it has been asked before. The answers are insufficient although they are marked as correct.
我知道,之前有人问过。虽然它们被标记为正确,但答案还不够。
I need something that fills a container with an image by showing the most of the image, centers it and preserves its ratio. Should be ajquery-plugin, a angular-directive or plain JS.
我需要通过显示图像的大部分来填充带有图像的容器,使其居中并保留其比例。应该是ajquery-plugin,angular-directive或普通的JS。
<div ratio-fill>
<img>
</div>
Of course should be enough where the script takes action then.
当然,在脚本采取行动的地方应该足够了。
Solution for interest:
兴趣解决方案:
<div ratio-fill="http://img.url"></div>
CSS
*[ratio-fill] { background-size: cover;
background-position: center; }
Script (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 + "')" );
});
})
1 个解决方案
#1
2
with css on the DIV background-size: cover; background-position: center;
与DIV背景大小的css:封面;背景位置:中心;
#1
2
with css on the DIV background-size: cover; background-position: center;
与DIV背景大小的css:封面;背景位置:中心;