Div从顶部滑动而不是滑动以显示

时间:2017-04-22 08:23:09

标签: javascript jquery html css

我希望div的内容从顶部向下滑动。我尝试过的所有方法似乎都“揭示”几乎是静态的内容。我想从屏幕边缘向下滑动并推送内容。我该怎么做? Jsfiddle here

HTML

<div id="dolphin">
<div id="lizard"><img src="http://www.beardeddragon.co/image/cache/data/blackhat-500x500.jpg"></div>
</div>
<div id="content"></div>

CSS

#lizard {
padding:50px;
display:none;
}

#dolphin {
  position: fixed;
  width: 100%;
  background-color: green;
}

#content {
  height: 2000px;
}

JS

$(document).ready(function(){
  $("#dolphin").click(function(){
        $("#lizard").stop().slideToggle("fast");
  });
});

1 个答案:

答案 0 :(得分:1)

您可以移动具有负边距的内容来创建幻灯片效果而不是显示效果。

这是一个非常粗略的示例,但您可以通过将margin-top: -100%;设置为margin-top来了解它最初是如何隐藏0并显示的;

&#13;
&#13;
$(".slide-button").click(function(){
  $(".lizard").toggleClass('slideit');
});
&#13;
html, body {margin: 0; padding: 0;}
div {
  text-align: center;
}
.slide-button {
  position: fixed;
  top: 0; right: 0; left: 0;
  z-index: 10;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 20px;
  background-color: green;
  cursor: pointer;
}
.lizard {
  width: 100%;
  padding-top: 20px;
  overflow: hidden;
  background-color: green;
  transition: all 0.5s ease;
}
.lizard img {
  margin-top: -100%;
  transition: all 0.5s ease;
  opacity: 0;
}
.lizard.slideit img {
  margin-top: 10px;
  opacity: 1;
}
.content {
  height: 1000px;
  background-color: green;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="slide-button">slide it</div>
<div class="lizard">
  <img src="http://www.beardeddragon.co/image/cache/data/blackhat-500x500.jpg">
</div>
<div class="content"></div>
&#13;
&#13;
&#13;

<强>拨弄

https://jsfiddle.net/Hastig/qjfgsrL0/1/