【问题标题】:Each not encapsulating blocks每个不封装块
【发布时间】:2015-09-13 20:58:21
【问题描述】:

我正在构建一个将内容动态定位在一组部分中间的网站。

带有图像背景的 Div(全宽图像 - FWI)

带有图像背景的Div(不同高度)

我的问题是即使每个选择器第一个 div 用于指示其下方任何其他 div 的高度,我显然错过了一些非常基本的东西

jQuery

jQuery(window).on("resize", function() {
  jQuery('.fwi').each(function() {
    jQuery('.image-outer').height(jQuery('.fwi-image').height());
  });
}).resize();


jQuery(".fwi-image img").load(function() {
  jQuery('.fwi').each(function() {
    jQuery('.image-outer').height(jQuery('.fwi-image').height());
  });
});

HTML

<div class="fwi">
    <div class="relative">      
          <div class="fwi-image full-width">
                <img width="1920" height="1080" src="">
          </div>  
          <div class="outer image-outer" style="height: 1080px;">
          my content which is dynamically positioned in the center vertically in my div
          </div>
   </div>
 </div>


<div class="fwi">
    <div class="relative">      
          <div class="fwi-image full-width">
                <img width="1920" height="1200" src="">
          </div>  
          <div class="outer image-outer" style="height: 1080px;">
         will take height from previous image-outer not its parent - it should be 1200
          </div>
   </div>
 </div>

【问题讨论】:

  • 这里是网站:你可以看到第二个图片块的文字太低了,因为它占用了第一个图片块的大小prestlaundry.com

标签: javascript jquery html css resize


【解决方案1】:

把它放在你的文档中。

function adjustImageOuterHeight () {
    var fwiImage = jQuery(this).parent(".fwi-image");
    var imageOuter = fwiImage.next(".image-outer");
    imageOuter.height(fwiImage.height());
}

jQuery(document).ready(function () {
    jQuery(".fwi-image img")
        .load(adjustImageOuterHeight)
        .each(function () {
            if (this.complete) adjustImageOuterHeight.call(this);
        });
});

jQuery(window).resize(function () {
    jQuery(".fwi-image img").each(adjustImageOuterHeight);
});

松开与.fwi-image 相关的任何其他 jQuery 内容。并且可以选择在 window 对象上放松对 .resize() 的显式调用。

【讨论】:

  • 这并不能解决我的问题,因为这些部分中的每一个都可能位于我的 DOM 中的不同位置 - 由于不同的页面布局,是否可以使用 ('this') 选择器?跨度>
  • 哦,我明白了,图像的load 事件处理程序在加载后注册得太晚了。
  • 完美,很抱歉有人设法将恶意软件带到我的生产服务器上,我一直在清理它。你是明星!
猜你喜欢
  • 2012-10-23
  • 1970-01-01
  • 2011-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-11
  • 1970-01-01
相关资源
最近更新 更多