简体   繁体   English

如何使用jQuery使此div从顶部向下滑动?

[英]How can I make this div slide down from top with jQuery?

So I have 2 divs children of a display block parent. 因此,我有2个divs子级显示父级。 I would like to make div #2 (green) be on top of div #1 (red). 我想使div#2(绿色)位于div#1(红色)之上。 With "on top" I'm not talking about z-index, I'm talking about literally being on top of the other. 我说的“在最上面”不是在谈论z-index,而是在说真正在另一个之上。 And then I was wondering if there could be a way to make div #2 slideDown() 然后我想知道是否有办法使div#2 slideDown()

As far as I tested, jQuery slideDown() or slideUp() works differently. 据我测试,jQuery slideDown()或slideUp()的工作方式有所不同。 In the demo I made, when I run 在我制作的演示中,当我运行时

$('.item-1').slideUp();

The item 2 is sliding up instead of item 1, why is that? 项目2向上滑动而不是项目1向上滑动,为什么? I'm getting confused. 我很困惑。

Any hints would be appreciated, 任何提示将不胜感激,

Thanks in advance. 提前致谢。

 window.slide = function() { $('.item-1').slideUp(); } 
 .items-container { height: 400px; width: 240px; background-color: #c3c3c3; display: block; /* overflow: hidden; */ } .item { height: 100%; width: 240px; position: relative; font-size: 30px; text-align: center; vertical-alignment: middle; } .item-1 { background-color: red; } .item-2 { float: left; position: relative; background-color: green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button onclick="slide()"> Click me! </button> <div class="items-container"> <div class="item item-1"> 1 </div> <div class="item item-2"> 2 </div> </div> 

.slideUp() works by changing the height of the element. .slideUp()通过更改元素的高度来工作。 As that element gets shorter, following elements will move up the page. 随着该元素变短,以下元素将在页面上移。

As seen in the documentation: 如文档所示:

The .slideUp() method animates the height of the matched elements. .slideUp()方法可对匹配元素的高度进行动画处理。 This causes lower parts of the page to slide up, appearing to conceal the items. 这会使页面的下部向上滑动,似乎隐藏了项目。

In your fiddle, item1 slides up as expected and as defined by the doc : 在您的小提琴中,item1可以按预期并按照doc的定义向上滑动:

Description: Hide the matched elements with a sliding motion. 说明:滑动隐藏匹配的元素。

So your div slides up and disappears, and item2 doesn't "move", just fills the space in the DOM after item1 has been hidden. 因此,您的div会滑动并消失,并且item2不会“移动”,而是会在item1被隐藏后填充DOM中的空间。

jQuery's slideUp() and slideDown() methods animate the height of the matched elements, not position as you seemed to want: http://api.jquery.com/slideUp/ . jQuery的slideUp()slideDown()方法为匹配元素的高度设置动画,而不是您似乎想要的位置: http : //api.jquery.com/slideUp/

What you seem to want is to translate the div it so that it moves on top of the first one. 您似乎想要的是将div translate为使其在第一个div之上移动。

http://www.w3schools.com/css/css3_2dtransforms.asp http://www.w3schools.com/css/css3_2dtransforms.asp

 window.slideUp = function() { $('.item-2').addClass('slideUp'); } window.slideDown = function() { $('.item-2').removeClass('slideUp'); } 
 .items-container { height: 100px; width: 240px; background-color: #c3c3c3; display: block; /* overflow: hidden; */ } .item { height: 100%; width: 240px; position: relative; font-size: 30px; text-align: center; vertical-alignment: middle; } .item-1 { background-color: red; } .item-2 { position: relative; transition: transform linear 1s; background-color: green; } .slideUp { transform: translate(0,-100%); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button onclick="slideUp()"> SlideUp! </button> <button onclick="slideDown()"> SlideDown! </button> <div class="items-container"> <div class="item item-1"> 1 </div> <div class="item item-2"> 2 </div> </div> 

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

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