简体   繁体   English

将新的div滑到另一个div下方

[英]Sliding down new div below another div

I want to have a new/hidden div slide down from below another div. 我想要一个新的/隐藏的div从另一个div下方向下滑动。 The idea is that I have a input field and a add-button. 我的想法是我有一个输入字段和一个添加按钮。 When the add-button is clicked, more form elements are revealed (slide out below). 单击添加按钮后,将显示更多表单元素(滑出下方)。 The form-part of this is not important for my problem, so I just let the first div be the text "hover me to reveal new div" and the new sliding-down-div be some random text. 对于我的问题,其形式部分并不重要,因此我只将第一个div作为文本“悬停以显示新的div”,将新的slide-down-div变为一些随机文本。

So far I have this html: 到目前为止,我有这个html:

<div class="one">Hover me to reveal new div</div>
<div class="two">I slid!<br>And I am higher than the div before me...</div>    

And this css: 而这个CSS:

.one {
    position: relative;
    top: 100px;
    background-color: lightblue;
    z-index: 1;
}

.two {
    position: absolute;
    top: 60px;
    background-color: yellow;
    z-index: -1;
    -webkit-transition: top 1s;
    -moz-transition: top 1s;
    -o-transition: top 1s;
    transition: top 1s;
    }

.one:hover + .two {
    top: 100px;
}

See jsfiddle: http://jsfiddle.net/8ZFMJ/ 参见jsfiddle: http : //jsfiddle.net/8ZFMJ/

This kind of works. 这种作品。 But the second div is higher than the first div, so it is visible above the first div, I do not want that. 但是第二个div高于第一个div,因此它在第一个div上方可见,我不希望这样。 How can I make it slide down the first div in the way that I want? 如何使它按照我想要的方式滑下第一格? If I in the process end up not having the first div to have to know the position of the first div, that would be good too... 如果我在此过程中最终没有第一个div必须知道第一个div的位置,那也将很好...

Put the two divs in a container (With the height of the two divs combined.), and put your divs to be at the very top of the container. 将两个div放到一个容器中(两个div的高度合计。),然后将div放到容器的顶部。 Then set the container to overflow:hidden; 然后将容器设置为overflow:hidden;

Something like this: http://jsfiddle.net/8ZFMJ/2/ 像这样的东西: http : //jsfiddle.net/8ZFMJ/2/

Regards 问候

(Edited to use css3 animations instead of jQuery's slideDown() ) (已编辑为使用css3动画而不是jQuery的slideDown()

This solution works without having the additional white space below the hovered element. 此解决方案可以在悬停元素下方没有额外的空白的情况下工作。 In addition, you don't have to specify the height for the containing element. 另外,您不必指定包含元素的高度。

We have to animate the container's max-height here instead of height (see How can I transition height: 0; to height: auto; using CSS? ). 我们必须在此处为容器的max-height height (而非height设置动画(请参见如何将height:0;转换为height:auto;使用CSS? )。 See this JsFiddle for a working version where the underlying element is shown on hover and closed on clicking it. 请参阅此JsFiddle以获取工作版本,该基础元素在悬停时显示,在单击时关闭。

The downside of this approach is that you have to specify a max-height attribute to the sliding element so that the attribute's value is greater than the element's content will ever be. 这种方法的缺点是,您必须为滑动元素指定max-height属性,以使该属性的值大于该元素的内容。 Since the animation works on the max height of the element, it also cannot be ridiculously large (see example below). 由于动画在元素的最大高度上工作,因此动画也不能过大(请参见下面的示例)。

An example : The opened max-height is set to 500px , the actual content height is 100px , and the transition animation lasts for 5 seconds. 示例 :打开的max-height设置为500px ,实际内容高度为100px ,过渡动画持续5秒钟。 The first second of the opening animation is used for showing the contents, and the next four seconds are not visible to the user. 开头动画的第一秒用于显示内容,而接下来的四秒对用户不可见。 Note that this works in both ways, so that the closing animation starts four seconds after clicking the element. 请注意,这在两种方式下均有效,因此,结束动画在单击该元素后四秒钟开始。

More javascript :) 更多javascript :)

It is indeed possible to get the needed height for the inner elements by using jQuery's functions. 通过使用jQuery的函数,确实有可能获得内部元素所需的高度。 The following example demonstrates the use of css3 animations with jQuery to avoid the problem described above. 下面的示例演示了如何将css3动画与jQuery配合使用来避免上述问题。

The key is to wrap the elements of the sliding down element to a div and calculate its needed height on hover. 关键是将下滑元素的元素包装到div并在悬停时计算其所需的高度。 See the working Js Fiddle 正在工作的小提琴手

var contentHeight = $('.two').children('div').outerHeight();
$('.two').css('max-height', contentHeight + 'px')

The above solution probably solves all your needs. 上述解决方案可能会解决您的所有需求。 However, it is unclear to which extent you are willing to use javascript. 但是,尚不清楚您愿意使用javascript的程度。

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

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