【问题标题】:jQuery get dynamic width of multiple elements on browser resizejQuery在浏览器调整大小时获取多个元素的动态宽度
【发布时间】:2012-01-26 19:28:48
【问题描述】:

标题几乎解释了我的大部分问题。我会提供一些背景信息。

我有这样的东西。 (简体)

<ul>
<li><img src="image1.jpg"/></li>
<li><img src="image2.jpg"/></li>
<li><img src="image3.jpg"/></li>
</ul>

每个里都是水平平铺的。像这样:

 [   ][   ][   ][   ]

所以我已经有了这个脚本来拉伸图像高度并保持纵横比。 但现在我需要一个脚本,即使在调整浏览器大小后,它也能动态获取所有图像(或 li)的宽度。 据我了解,我需要诸如事件冒泡或某种委托之类的东西。我不知道如何继续这个 jQuery 之旅。

图片链接到不同的浏览器设置

正常浏览器高度

http://f.cl.ly/items/1O3w3M3l3b3a0z2B0U1w/image1.jpg

降低浏览器高度

http://f.cl.ly/items/3y3K38243J063J1U2f1P/image2.jpg

【问题讨论】:

    标签: jquery


    【解决方案1】:

    您必须挂钩resize 事件:

    $(window).resize(function(){
        $('li img').each(function(){
            console.log( $(this).width() );
        });
    });
    

    如果您希望它在页面加载时也触发,请自行触发 resize 事件:

    $(window).resize(function(){
        $('li img').each(function(){
            console.log( $(this).width() );
        });
    })
    .resize();
    

    这是小提琴:http://jsfiddle.net/39JFQ/


    如果您需要所有宽度的总和,请使用:

    $(window).resize(function(){
        var totalWidths = 0;
    
        $('li img').each(function(){
            totalWidths += $(this).width();
        });
    
        console.log(totalWidths);
    })
    .resize();
    

    这是小提琴:http://jsfiddle.net/39JFQ/2/


    您显然应该用更具体的东西替换 li img 选择器。

    【讨论】:

    • 嗨,我非常感谢小提琴!但是我想一起计算所有宽度,而不仅仅是第一个。也许我不够清楚。我道歉
    • @SebastianGraz - 我发布了一个解决方案。
    • 是的,totalWidths 做到了。除了最后一个“.resize();”之外,我什么都做对了
    【解决方案2】:

    您需要为窗口调整大小事件创建一个事件处理程序:

    $(window).resize(function(){
      $('li').each(function(){
        var width = $(this).width();
        if(width > large){
          $(this).find('img').attr('src', 'largeImage.jpg');
        }else{
          $(this).find('img').attr('src', 'smallImage.jpg');
        }
      });
    });
    

    您是否考虑过为此的媒体查询? http://www.w3.org/TR/css3-mediaqueries/

    【讨论】:

    • 我的错误。我很快就把它删掉了。
    • 变量“宽度”会随着每次迭代而改变。您需要在循环中使用它,将其推送到数组或其他东西。
    【解决方案3】:

    你确定你需要 JavaScript 吗?为什么不直接使用responsive CSS 以便图像适应:

    img {
      width: inherit;  /* This makes the next two lines work in IE8. */
      max-width: 100%; /* Add !important if needed. */
      height: auto;    /* Add !important if needed. */
    }
    

    并在其容器上使用百分比宽度。

    要在 jQuery 中执行此操作,您需要将处理程序附加到 resize 事件。使用ways described here 之一或下面使用Response 的示例。我会使用$.data 来存储宽度数据。

    Response.action( function () {
        // Code in here runs on ready and on resize:
        $.each($('img'), function() {
            // Get width of element each element:
            var currentWidth = $(this).width(); 
            // Store for access where you need it:
            $.data( this, 'lastWidth', currentWidth ); 
        });//each
    });//action
    

    也请参阅:responsejs.com/labs/dimensions/。

    【讨论】:

    • 感谢您的意见。 Response JS 看起来像一个很棒的插件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 1970-01-01
    相关资源
    最近更新 更多