【问题标题】:Slide up div from another divs top when page loads页面加载时从另一个 div 顶部向上滑动 div
【发布时间】:2021-12-31 12:22:59
【问题描述】:

所以我有 2 个 div,如下面的代码所示。

我只想从第一个 div 的顶部向上滑动第二个 div (box-2)。 真正的问题是

  • 第一个 div 将保持原样,第二个 div 将从其背面滑动。
  • 我想隐藏第二个 div,让它滑动并在它滑动时自我陶醉,即只有向上滑动的部分应该是可见的。

不知道怎么做,尝试了多种选择,但没有运气。 如果有人可以指导,真的很感激。

$('.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>

【问题讨论】:

  • 如果你有div-1div-2,并且你需要在div-2上方显示div-1,你应该在css中使用z-index..见w3schools.com/cssref/pr_pos_z-index.asp
  • 不,我想要的是不同的,最初 box-2 应该被隐藏,然后它应该从 box-1 的顶部向上滑动并且只有滑动的部分应该是可见的。

标签: javascript html jquery css


【解决方案1】:

考虑到您的起点,最简单的方法是简单地使用 animate() 方法内部公开的回调函数,该函数将在初始动画完成后执行:

// 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>

请注意,由于两个动画顺序运行,我将您的初始时间 3000 毫秒划分为每个动画需要 1500 毫秒,这样总时间相同,但允许动画分阶段运行。

参考资料:

【讨论】:

  • 谢谢大卫。似乎这就是方式。让我试试看。
  • 很高兴能帮上忙;试一试,如果有任何问题,请告诉我们。
  • 这是我的功能看起来像jsfiddle.net/9joycrb8/1。在您的帮助下,我能够实现所需的功能,但只是想确认有什么方法可以保持背景透明或在不使用背景 div 的情况下实现相同的功能。 (检查有红色背景的#chat-symbole-container)谢谢!
【解决方案2】:

.box-1的css上使用z-index: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>

【讨论】:

    【解决方案3】:

    我不太确定我是否完全理解您的问题。

    但是如果你想从box-1后面显示box-2,你不只需要在html中切换它们的位置吗?所以 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>

    【讨论】:

    • 主要是box-2最初应该被隐藏,然后它应该从box-1的顶部向上滑动并且只有滑动的部分应该是可见的。
    • “只有幻灯片应该可见的部分”是什么意思?整个盒子应该从底部滑入,不是吗?也许您可以解释用例以更好地理解这里的目标
    • 第二个框应该从第一个框的顶部开始向上滑动。最初它应该被隐藏,然后当它滑动时,它应该变得可见。有意义吗?
    猜你喜欢
    • 2019-03-23
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多