使用jQuery和CSS实现SlideUp效果

3
我正在努力修复我的滑动效果。
我想让黄色的div向上移动200px。现在它只能向下移动40px:(
有人可以帮忙吗? http://jsfiddle.net/m3nwm63x/

var clicked=true;
$(".one").on('click', function(){
    if(clicked)
    {
        clicked=false;
        $(".two").css({"top": 0});
    }
    else
    {
        clicked=true;
        $(".two").css({"top": "-40px"});
    }
});
h1 {
  font-size:72px;
  margin:100px 0;
}

.container {
    overflow:hidden;
    height: 60px;
    float:left;
}
.one {
    position: relative;
    bottom: 0;
    background-color: lightblue;
    z-index: 1;
    cursor:pointer;
}
.two {
    position: relative;
    bottom: 40px;
    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: 0px;
}*/
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<h1>
Let's test this!
</h1>

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

1个回答

2

这是您要找的内容吗?

http://jsfiddle.net/m3nwm63x/4/

我已将 <div class="two"> 移动了一个位置。

var clicked=true;
$(".one").on('click', function(){
    if(clicked)
    {
        clicked=false;
        $(".two").css({"top": 0});
    }
    else
    {
        clicked=true;
        $(".two").css({"top": "40px"});
    }
});
h1 {
  font-size:72px;
  margin:100px 0;
}

.container {
    overflow:hidden;
    height: 60px;
    float:left;
}
.one {
    position: relative;
    bottom: 0;
    background-color: lightblue;
    z-index: 1;
    cursor:pointer;
    overflow: hidden;
    height: 40px;
}
.two {
    position: relative;
    bottom: 40px;
    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: 0px;
}*/
<h1>
Let's test this!
</h1>

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


非常感谢您详细的回复。一旦“打开”了,是否可以再次向下滑动而不是向上滑动? - michaelmcgurk
1
@michaelmcgurk 我也刚刚编辑了,将蓝色 div 的高度设置为 40px 以完全隐藏黄色 div。如果这是你要寻找的答案,请点击绿色勾勾 :) - Sauced Apples
太棒了 - 非常感谢!一个快速的问题,为什么第一次点击会立即显示而不是逐渐显示? - michaelmcgurk
1
我不能百分之百确定,如果您继续点击,只有第一个动画会快速播放,而每个后续的都是相同较慢的速度。 - Sauced Apples

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接