【问题标题】:Zurb Foundation Block Grid with different image size?具有不同图像大小的Zurb Foundation Block Grid?
【发布时间】:2013-07-25 12:01:52
【问题描述】:

这是我来自 Block Grid 示例的代码 http://foundation.zurb.com/docs/components/block-grid.html

<ul class="small-block-grid-2 my-grid">
  <li><img src="http://static4.businessinsider.com/image/50098bda69beddb65e000008-480/iphone-example-camera-photo.jpg"></li>
  <li><img src="http://foundation.zurb.com/docs/img/demos/demo2.jpg"></li>
  <li><img src="http://foundation.zurb.com/docs/img/demos/demo3.jpg"></li>
  <li><img src="http://foundation.zurb.com/docs/img/demos/demo4.jpg"></li>
</ul>

链接:http://jsfiddle.net/qhoc/ckMEN/1/

我尝试处理的问题是图像大小不同。我的后端将强制使用 min width = xmin height = y 但如何处理布局以便:

  1. 如果宽度
  2. 如果宽度 > 高度:裁剪并居中宽度

所有图像必须具有相同的li 大小。还有当窗口大小发生变化时如何处理,会不会让裁剪变得凌乱?

我的另一种选择是完全避免使用block-grid,而只使用li固定大小。那也可以。所以基本上li会根据屏幕大小改变wxh。

但如果可行的话,我更喜欢玩这个把戏。

请帮忙。谢谢!

【问题讨论】:

    标签: css html zurb-foundation


    【解决方案1】:

    通过将图像设置为background-images,您可以更好地控制&lt;li&gt; 元素中图像的裁剪和定位。

    这样做的一个缺点是您需要固定&lt;li&gt; 元素的高度。尽管一些简单的 javascript 或媒体查询可以纠正这个问题。

    HTML

    <div class="row">
      <div class="small-12 small-centered columns">
        <ul class="small-block-grid-4 large-block-grid-4 my-grid">
          <li>
              <div style="background-image:url(http://static4.businessinsider.com/image/50098bda69beddb65e000008-480/iphone-example-camera-photo.jpg);"></div>
          </li>
          <li>
              <div style="background-image:url(http://foundation.zurb.com/docs/img/demos/demo2.jpg);"></div>
          </li>
          <li>
              <div style="background-image:url(http://foundation.zurb.com/docs/img/demos/demo3.jpg);"></div>
          </li>
          <li>
              <div style="background-image:url(http://foundation.zurb.com/docs/img/demos/demo4.jpg);"></div>
          </li>
        </ul>
      </div>
    </div>
    

    CSS

    .my-grid li {
        display:block;
        height:150px;
        overflow:hidden;
    }
    
    .my-grid li > div {
        height:100%;
        width:100%;
        background-size:cover;
        background-position:center center;
    }
    

    编辑

    添加了一些 jQuery 以根据 &lt;li&gt; 的宽度调整其高度。

    $(document).ready(function(){
    
        function reheight() {
            $('.my-grid > li').each(function() {
                $(this).height($(this).width() * 0.75);
            });
        }
    
        reheight();
    
        $(window).resize(function(){
            reheight();
        });
    
    });
    

    这是full example

    【讨论】:

    • 不知道 javascript 可以这么快... :)
    【解决方案2】:

    你可以将每张图片插入到一个 div 中:

    overflow:hidden;
    

    并将图像定位在 div 的中心, 那么:

    $("img").each(function(){
    
        var width = $(this).width();
        var height = $(this).height();
    
        if(height < width){
    
            $(this).height($("#div").height()); //if the width > height, set the image height equal to the div height.. 
                //let's assume that this image is into the div with id="div"
        }
        else{
            $(this).width($("#div").width());
        }
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-19
      • 1970-01-01
      相关资源
      最近更新 更多