【问题标题】:Aspect Ratio Fill - the right way纵横比填充 - 正确的方法
【发布时间】: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 + "')" );

    });

  })

【问题讨论】:

标签: javascript jquery html


【解决方案1】:

在 DIV 上使用 css 背景尺寸:封面;背景位置:中心;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    相关资源
    最近更新 更多