gpt4 book ai didi

javascript - 长宽比填充 - 正确的方法

转载 作者:行者123 更新时间:2023-12-02 15:20:36 29 4
gpt4 key购买 nike

我知道,以前有人问过。尽管答案被标记为正确,但答案仍不充分。

我需要一些东西,通过显示图像的大部分,将其居中并保留其比例,从而用图像填充容器。应该是 ajquery-plugin、一个 Angular-directive 或普通 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 + "')" );

});

})

最佳答案

DIV 上有 css背景大小:封面;背景位置:中心;

关于javascript - 长宽比填充 - 正确的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34118535/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com