简体   繁体   English

页面加载时从另一个 div 顶部向上滑动 div

[英]Slide up div from another divs top when page loads

So I have 2 div as shown in below code.所以我有 2 个 div,如下面的代码所示。

What I just want is to slide up the second div (box-2) from the top of the first div.我只想从第一个 div 的顶部向上滑动第二个 div (box-2)。 The real problem is真正的问题是

  • First div will remain as it is and second div will slide from it's back side.第一个 div 将保持原样,第二个 div 将从其背面滑动。
  • I want to keep the second div hidden and let it slide and revel it self as it slides ie only the portion that slides up should be visible.我想隐藏第二个 div 并让它滑动并在它滑动时自我陶醉,即只有向上滑动的部分应该是可见的。

Not sure how to do it, tried multiple options but no luck.不知道怎么做,尝试了多种选择,但没有运气。 Really appreciate if anyone can guide.如果有人可以指导,真的很感激。

 $('.box-2').animate({'margin-bottom': '83px'}, 3000);
 .box-1 { height: 30px; width: 100px; background-color: blue; color: white; position: fixed; bottom: 0px; }.box-2 { height: 30px; width: 500px; background-color: blue; color: white; position: fixed; bottom: 0px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <div class="box-1">div 1</div> <div class="box-2">div 2 - the one that will slide up from box-1's Top.</div>

The easiest way, considering your starting point, is to simply use the callback function exposed inside of the animate() method, which will be executed once the initial animation is completed:考虑到您的起点,最简单的方法是简单地使用暴露在animate()方法内部的回调 function,一旦初始 animation 完成,就会执行该回调:

 // your initial jQuery, which selects the '.box-2' element(s) and // passes that collection to the animate() method: $('.box-2').animate({ // here rather than quote (just to show the example), we camel case // the CSS 'margin-bottom' property to the (unquoted) 'marginBottom', // and pass in the new dimension to which the method will animate: marginBottom: '83px' // we then take advantage of the completion callback, which is called // when the first animation is complete: }, 1500, function() { // here we take the 'this' from the collection passed to the outer // animate() call in which this callback function is wrapped: $(this).animate({ // here we then animate the 'width' to its new dimension of // '500px': width: '500px' }, 1500); });
 .box-1 { height: 30px; width: 100px; background-color: blue; color: white; position: fixed; bottom: 0px; }.box-2 { height: 30px; width: 100px; background-color: blue; color: white; position: fixed; bottom: 0px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <div class="box-2">Div that will slide up from box-1's Top.</div> <div class="box-1">Static div</div>

Note that, because of the two animations running sequentially I divided your initial time of 3000ms to have each animation take 1500ms, so that the overall time taken is the same but allowing the animation to run in stages.请注意,由于两个动画顺序运行,我将您的初始时间划分为 3000 毫秒,让每个 animation 花费 1500 毫秒,这样总时间相同,但允许 animation 分阶段运行。

References:参考:

The $( ".box-2" ).hide(0); $( ".box-2" ).hide(0); method is used to hide the second container after the animation.方法用于隐藏 animation 之后的第二个容器。 z-index: -1; style applied to make the box-2 container appear at the bottom during animation.应用样式以使box-2容器在 animation 期间出现在底部。

 $(document).ready(function() { /* The animation moves the second container from the bottom to a height of 83px. */ $('.box-2').animate({'margin-bottom': '83px'}, 3000, function(){ /* The first container appears when the animation is finished. */ $( ".box-1" ).css("display", "inline"); /* The second container is stored after the animation. */ $( ".box-2" ).hide(0); }); });
 .box-1 { height: 30px; width: 100px; background-color: red; color: white; position: fixed; bottom: 0px; /* The first container is initially hidden. */ display: none; }.box-2 { height: 30px; width: 500px; background-color: blue; color: white; position: absolute; bottom: 0px; /* Implemented to make the container appear at the bottom during animation. */ z-index: -1; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <div class="box-1">Static div</div> <div class="box-2">Div that will slide up from box-1's Top.</div>


References参考

I'm not quite sure I understand your question completely.我不太确定我是否完全理解你的问题。

But if you want to reveal the box-2 from behind box-1, don't you just have to switch their positions in the html?但是如果你想从box-1后面露出box-2,难道你只需要在html中交换它们的位置吗? So that box-1 is always in front of box-2所以 box-1 总是在 box-2 的前面

 $('.box-2').animate({'margin-bottom': '83px'}, 3000);
 .box-1 { height: 30px; width: 100px; background-color: blue; color: white; position: fixed; bottom: 0px; }.box-2 { height: 30px; width: 500px; background-color: blue; color: white; position: fixed; bottom: 0px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <div class="box-2">Div that will slide up from box-1's Top.</div> <div class="box-1">Static div</div>

Use z-index:1;使用z-index:1; on css of .box-1.box-1

 $('.box-2').animate({'margin-bottom': '83px'}, 3000);
 .box-1 { height: 30px; width: 100px; background-color: blue; color: white; position: fixed; bottom: 0px; z-index:1; }.box-2 { height: 30px; width: 500px; background-color: blue; color: white; position: fixed; bottom: 0px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <div class="box-1">div 1</div> <div class="box-2">div 2 - the one that will slide up from box-1's Top.</div>

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

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