简体   繁体   English

JQuery:当元素隐藏时阻止div崩溃()

[英]JQuery: Prevent div from collapsing when element hides()

When my form is submitted, the form "slides out" and using $.get() , I load another page and slide that in. In between the hiding of the form and the showing of the next page, the div collapses and all my footer stuff briefly ends up where the form was. 当我的表单被提交时,表单“滑出”并使用$.get() ,我加载另一个页面并将其滑入。在隐藏表单和下一页的显示之间,div崩溃和我的所有页脚的东西简要地结束了表单的位置。

Then, when the "next page" finishes loading, the footer goes back to the bottom (under the new page content). 然后,当“下一页”完成加载时,页脚返回到底部(在新页面内容下)。 because the size of the form can vary a lot, i'm trying to avoid specifying a height for the DIV. 因为表单的大小可能会有很大变化,我试图避免为DIV指定高度。

Is there any way to keep the div from changing? 有没有办法防止div改变?

Code: 码:

$("#MyForm").hide("slide", { direction: "left" }, 250, function () {
    $.get("../NewPage.html", function (data) {
        $("#NewPagePlaceholder").html(data);
        $("#NewPagePlaceholder").show("slide", { direction: "right" }, 250);
    });
});

Are you using CSS? 你在用CSS吗? I'm gonna make an assumption (bad I know but I can't see any code) you use display:none; 我会做一个假设(我知道但是我看不到任何代码),你使用display:none; to hide the div, instead you should try using visibility:hidden . 要隐藏div,请尝试使用visibility:hidden If you're using JQuery maybe you could add a css() method to the selector and set the visibility to hidden this way. 如果你正在使用JQuery,你可以在选择器中添加一个css()方法,并将可见性设置为隐藏。

What @user1394965 said. @ user1394965说的是什么。 Here's a quick example: 这是一个简单的例子:

The .hide() will make the object's css property be display: none which will remove the space it takes up on the page. .hide()将使对象的css属性为display: none ,这将删除它在页面上占用的空间。 The visibility: hidden property will make it hidden but still keep its space on the page. visibility: hidden属性会使其隐藏但仍保留页面上的空间。

So, instead of doing: 所以,而不是做:

$('#elementId').hide();

do the following: 请执行下列操作:

$('#elementId').css('visibility', 'hidden');

The other options above are likely better, you do have the option of setting the hieght based on the child div but it is hard to know if this is a good idea without seeing any code. 上面的其他选项可能更好,您可以选择根据子div设置hieght,但很难知道这是不是看到任何代码是一个好主意。

For example (untested): 例如(未经测试):

var height = $('#childdiv').height();
$('#childdiv').parent().css('height', height);

Specify a height for the div during the animation only. 仅在动画期间指定div的高度。 That is, before you slide out the old form, change the div to the same height. 也就是说,在滑出旧表单之前,将div更改为相同的高度。 After the new form slides in, remove this set height. 在新表单滑入后,删除此设置高度。

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

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