jQuery SlideDown 和 Float 问题
当我向下滑动的对象的内容浮动时,我遇到了 jQuery SlideDown 问题。
发生的情况是,div 向下滑动超过内容的高度,然后“捕捉”到正确的高度。
我认为(只是一个理论)div 会根据内部内容不浮动时的高度向下滑动。例如,如果我有 4 个浮动项目,则 div 似乎会比我有 2 个浮动项目时扩大两倍。
无论用clearfix hacks包含多少浮动似乎都无法解决任何问题。
我过去使用的解决方案(也是推荐的解决方案)是在隐藏项目之前通过 CSS 设置项目的高度。这样,slidedown 就知道要走多远。
问题是我正在嵌套使用向下滑动的面板。我无法预先设置父级的高度,因为它将根据已经展开的嵌套隐藏面板进行计算。
我可以递归地调用设置所有切换面板的函数...找到最里面的嵌套面板,设置高度,折叠我想要默认隐藏的面板,然后沿着层次结构向上移动。但如果要遍历这么多,性能就会受到影响。
任何人都知道一种方法来修复浮动内容的滑动,而无需事先通过CSS固定高度,而不必先手动遍历dom设置高度?
更新:
示例代码在这里:
(点击页面看到它滑动)
这是基于我正在使用的确切样式,在该特定组合中,您会看到问题。请注意,设置为 SlideDown 的 DIV 具有粉红色背景。它会比其内容物滑得更远,然后快速弹回到正确的位置。
I'm having an issue with jQuery slideDown when the contents of the object I'm sliding down is floated.
What happens is that that the div slides down WAY past the height of the contents, and then 'snaps' to the proper height.
I think (merely a theory) that the div is sliding down based on the height it would be if the contents inside weren't floated. For instance, if I have 4 floated items, the div seems to expand twice as far than when I have 2 floated items within.
No amount of containing the floats with clearfix hacks seems to fix anything.
The solution I've used in the past (and one that is recommended) is to set the height of item via CSS BEFORE hiding it. That way, slidedown knows how far to go.
The catch is that I'm nesting panels that use slide down. I can't set the height of the parent before hand as it would be calculated based on the nested hidden panels being expanded already.
I could recursively call that function that sets up all my toggle panels...find the innermost nested ones, set the height, collapse the ones I want hidden by default, and then move my way up the hierarchy. But that'd be a performance hit having to traverse so much.
Anyone know of a way to fix this slideDown of floated content without fixing the height via CSS before hand without having to traverse the dom setting heights manually first?
UPDATE:
sample code is here:
(click the page to see it sliding)
This is based on the exact styles I am using and in that particular combination, you'll see the issue. Note the DIV being set to slideDown has a pink background. It will slide down FURTHER than its contents and then quickly snap back into the proper place.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我无法重现该问题。请提供一些代码。
这按预期工作:
HTML
CSS
JS
I can't reproduce the problem. Please provide some code.
This works as expected:
HTML
CSS
JS
已解决:
中删除
margin-bottom:10px;
从http://jsbin.com/ 阿约卡/11
solved:
remove
margin-bottom:10px;
from yourhttp://jsbin.com/ajoka/11
我刚刚遇到了同样的问题,在阅读这篇文章时突然想起我几年前就已经遇到过这个问题。
这是一个解决方案,虽然不是最佳的,但可以在大多数情况下完成工作。
将
padding-bottom: 1px;
添加到您的包装器中。就我而言,包装器是动画 div 的父级。希望有帮助。
I just came across the same issue, and while reading this post suddenly remembered I already had this issue years ago.
Here's a solution that, though not optimal, gets the job done for most situations.
Add
padding-bottom: 1px;
to your wrapper. In my case, the wrapper is the parent to the animated div.Hope that helps.
我也面临着同样的问题。两个文件具有相同的 HTML 代码,但其中一个文件显示了此问题。作为解决方案,我计算了使用 jQuery 滑动的 div 的高度:
var somevar=$('#contentBody').outerHeight();
然后我将其存储在变量中,并再次应用它在应用 SlideDown 效果之前的 div:
$('#contentBody').height(somevar);
这个技巧解决了这个问题。
I was also facing the same issue. Two files having same HTML code but one of it was showing this issue. As a solution I calculated the height of the div which was sliding with jQuery:
var somevar=$('#contentBody').outerHeight();
Then I stored it in a variable, and again applied it that div before applying the slideDown effect:
$('#contentBody').height(somevar);
This trick solved the issue.