jquery等高插件添加边距

发布于 2024-12-12 02:00:46 字数 786 浏览 0 评论 0原文

我如何添加到下面的代码来计算 DIV 是否有任何 margin-top/margin-bottom?我目前的情况是,我有 2 列(DIV),其中 1 列有顶部边距,另一列没有,这会导致列无法正确排列。

抱歉,代码已被缩小,我丢失了原始代码。

$.fn.equalHeight = function (a) {
    var b = {
        delay: 100,
        minusHeight: 0
    };
    var a = $.extend(b, a);
    var c = 0;
    var d = 0;
    var e = $(this);
    setTimeout(function () {
        e.each(function () {
            c = $(this).outerHeight();
            d = c > d ? c : d
        });
        return e.each(function () {
            var b = $(this);
            var c = d - (b.outerHeight() - b.height()) - a.minusHeight;
            var e = jQuery.browser.msie && jQuery.browser.version < 7 ? "height" : "min-height";
            b.css(e, c + "px")
        })
    }, a.delay)
};

How would I add to the code below to work out if a DIV has any margin-top/margin-bottom? The situation I have at the moment is that I have 2 columns (DIV's) 1 have a margin-top and the other doesn't which causes the columns not to line up correctly.

Sorry the code has been minified and I have lost the original.

$.fn.equalHeight = function (a) {
    var b = {
        delay: 100,
        minusHeight: 0
    };
    var a = $.extend(b, a);
    var c = 0;
    var d = 0;
    var e = $(this);
    setTimeout(function () {
        e.each(function () {
            c = $(this).outerHeight();
            d = c > d ? c : d
        });
        return e.each(function () {
            var b = $(this);
            var c = d - (b.outerHeight() - b.height()) - a.minusHeight;
            var e = jQuery.browser.msie && jQuery.browser.version < 7 ? "height" : "min-height";
            b.css(e, c + "px")
        })
    }, a.delay)
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三岁铭 2024-12-19 02:00:46

您应该将 true 作为参数添加到 .outerHeight() 以便在计算中包含边距。

但是你的代码还有另一个问题..你不能从超时中返回任何内容,并且你似乎已经将返回代码放在那里了..

这意味着你正在破坏 jQuery 链接系统..

你的代码可能应该是

$.fn.equalHeight = function(a) {
    var b = {
        delay: 100,
        minusHeight: 0
    };
    var a = $.extend(b, a);
    var c = 0;
    var d = 0;

    this.each(function() {
        c = $(this).outerHeight(true);
        d = c > d ? c : d;
    });
    return this.each(function() {
        var b = $(this);
        var c = d - (b.outerHeight(true) - b.height()) - a.minusHeight;
        var e = (jQuery.browser.msie && jQuery.browser.version < 7) ? "height" : "min-height";
        setTimeout( function(){
            b.css(e, c + "px");
        }, a.delay);
    });
};

演示 http://jsfiddle.net/gaby/DXHtk/

You should add true as a parameter to .outerHeight() so that it includes the margin in the calculations.

But there is another issue with your code.. You cannot return anything from a timeout, and you seem to have placed your return code in there..

This means you are breaking the jQuery chaining system..

Your code should probably be

$.fn.equalHeight = function(a) {
    var b = {
        delay: 100,
        minusHeight: 0
    };
    var a = $.extend(b, a);
    var c = 0;
    var d = 0;

    this.each(function() {
        c = $(this).outerHeight(true);
        d = c > d ? c : d;
    });
    return this.each(function() {
        var b = $(this);
        var c = d - (b.outerHeight(true) - b.height()) - a.minusHeight;
        var e = (jQuery.browser.msie && jQuery.browser.version < 7) ? "height" : "min-height";
        setTimeout( function(){
            b.css(e, c + "px");
        }, a.delay);
    });
};

Demo at http://jsfiddle.net/gaby/DXHtk/

池木 2024-12-19 02:00:46

您可以使用 jQuery 的 css() 函数检查 div 的边距。不幸的是,不支持像 margin 这样的简写 css 属性,您必须以 css('marginTop')css('marginBottom ')等等。然后,您可以有条件地修改每个 div,使其对齐。

You can check the div for margins using the css() function of jQuery. Unfortunately shorthand css properties like margin aren't supported and you'll have to do it in the form of css('marginTop'), css('marginBottom') and so on. You can then conditionally modify each div so that it will line up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文