简体   繁体   English

等高并排浮动DIV(jQuery)

[英]Equal height side-by-side floating DIVs (jQuery)

I have a really weird thing happening on a site I'm working on... can't figure out what's going on. 我正在处理的网站上发生了一件非常奇怪的事情……无法确定发生了什么。 There's definitely something funky with my javascript that's causing this, but I can't for the life of me figure it out. 我的javascript肯定有一些时髦的原因,但是我一辈子都无法弄清楚。

Here's what's happening: 这是正在发生的事情:

I have several rows of divs that float left take the height of their container using jQuery. 我有几行的div左浮动,它们使用jQuery占据容器的高度。 The problem is, when I go to the page directly all the containers shrink down and hide part of their contents. 问题是,当我直接转到页面时,所有容器都会缩小并隐藏其内容的一部分。 Then, when I click the link to go to the homepage and then click the link back to the page in question, it all works properly. 然后,当我单击链接转到主页,然后单击链接回到有问题的页面时,一切正常。

See images below 看下面的图片

Page showing up incorrectly: 页面显示不正确: 页面显示不正确


Navigate back to homepage and then back to page in question 导航回到主页,然后回到有问题的页面

Page showing up correctly now: 页面现在正确显示: 页面现在正确显示

Here's my javascript: 这是我的JavaScript:

    $(document).ready(function() {
    var pageWidth = $(window).width();
    if (pageWidth >= 1048) {
       (function($) {
            $.fn.eqHeights = function() {
                var el = $(this);
                if (el.length > 0 && !el.data('eqHeights')) {
                    $(window).bind('resize.eqHeights', function() {
                        el.eqHeights();
                    });
                    el.data('eqHeights', true);
                }
                return el.each(function() {
                    var curHighest = 0;
                    $(this).children().each(function() {
                        var el = $(this),
                        elHeight = el.height('auto').height()-3;
                        if (elHeight > curHighest) {
                            curHighest = elHeight;
                        }
                    }).height(curHighest);
                });
            };
            $('.section').eqHeights();
        }(jQuery)); 
    };
});

And here's my HTML and CSS: 这是我的HTML和CSS:

<!--1 Column Photo-->
    <div class="section group content">
        <div class="col span_12_of_12">
            <img src="images/placeholder_1col.jpg" alt=""/>
        </div>
    </div>
    <!--2 Column Text Photo-->
    <div class="section group content">
        <div class="v-table col span_6_of_12">
            <div class="v-cell text-col">
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas quis adipiscing dui, id tristique sapien. In hac habitasse platea dictumst. Nam molestie venenatis augue nec condimentum.Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aenean eget mi id urna tincidunt rhoncus. Nam rutrum egestas neque quis semper. Vestibulum iaculis elit lacus, eget dignissim purus.</p>
            </div>
        </div>
        <div class="col span_6_of_12">
            <img src="images/placeholder_3col_1.jpg" alt=""/>
        </div>
    </div>
    <!--3 Column Photo-->
    <div class="section group content">
        <div class="col span_4_of_12">
            <img src="images/placeholder_3col_1.jpg" alt=""/>
        </div>
        <div class="col span_4_of_12">
            <img src="images/placeholder_3col_2.jpg" alt=""/>
        </div>
        <div class="col span_4_of_12">
            <img src="images/placeholder_3col_3.jpg" alt=""/>
        </div>
    </div>
    <!--2 Column Photo Text-->
    <div class="section group content">
        <div class="col span_6_of_12">
            <img src="images/placeholder_3col_1.jpg" alt=""/>
        </div>
        <div class="v-table col span_6_of_12">
            <div class="v-cell text-col">
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas quis adipiscing dui, id tristique sapien. In hac habitasse platea dictumst. Nam molestie venenatis augue nec condimentum.Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aenean eget mi id urna tincidunt rhoncus. Nam rutrum egestas neque quis semper. Vestibulum iaculis elit lacus, eget dignissim purus.</p>
            </div>
        </div>
    </div>
    <!--2 Column Photo Photo-->
    <div class="section group content">
        <div class="col span_6_of_12">
            <img src="images/placeholder_2col_2.jpg" alt=""/>
        </div>
        <div class="col span_6_of_12">
            <img src="images/placeholder_2col_1.jpg" alt=""/>
        </div>
    </div>
    <!--2 Column Text Text-->
    <div class="section group content">
        <div class="v-table col span_6_of_12">
            <div class="v-cell text-col">
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas quis adipiscing dui, id tristique sapien. In hac habitasse platea dictumst. Nam molestie venenatis augue nec condimentum.Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aenean eget mi id urna tincidunt rhoncus. Nam rutrum egestas neque quis semper. Vestibulum iaculis elit lacus, eget dignissim purus.</p>
            </div>
        </div>
        <div class="v-table col span_6_of_12">
            <div class="v-cell text-col">
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas quis adipiscing dui, id tristique sapien. In hac habitasse platea dictumst. Nam molestie venenatis augue nec condimentum.Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aenean eget mi id urna tincidunt rhoncus. Nam rutrum egestas neque quis semper. Vestibulum iaculis elit lacus, eget dignissim purus.</p>
            </div>
        </div>
    </div>

.section {
    width:100%;
    clear: both;
    padding: 0px;
    margin: 0px;
    overflow:hidden;
}

/*  COLUMN SETUP  */
.col {
    display: block;
    float:left;
    margin:0;
}
.col:first-child { margin-left: 0; }

.col img{
    width:100%;
}


/*  GROUPING  */

.group:after {
    clear:both;
}
.group {
    zoom:1; /* For IE 6/7 */
}


/*  GRID OF TWELVE  */
.span_12_of_12 {
    width: 100%;
}

.span_11_of_12 {
    width: 91.66%;
}
.span_10_of_12 {
    width: 83.33%;
}

.span_9_of_12 {
    width: 75%;
}

.span_8_of_12 {
    width: 66.66%;
}

.span_7_of_12 {
    width: 58.33%;
}

.span_6_of_12 {
    width: 50%;
}

.span_5_of_12 {
    width: 41.66%;
}

.span_4_of_12 {
    width: 33.33%;
}

.span_3_of_12 {
    width: 25%;
}

.span_2_of_12 {
    width: 16.66%;
}

.span_1_of_12 {
    width: 8.333%;
}

I hope that makes sense... thanks in advance! 我希望这是有道理的……预先感谢!

Here the height of the section will depend on the image height. 在这里,截面的高度将取决于图像的高度。 If the height calculation logic is called on window.load() event it must work fine. 如果在window.load()事件上调用了高度计算逻辑,则它必须工作正常。 Once you navigate back to homepage and then back to page in question the images are cached hence it works fine.Try this 一旦您导航回到主页,然后再回到有问题的页面,图像就会被缓存,因此可以正常工作。

$(window).load(function(){
 $('.section').eqHeights();
});

The window load event is called after all the images are loaded. 加载所有图像后,将调用窗口加载事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM