使用jQuery和CSS进行SlideUp效果

时间:2016-02-03 14:03:44

标签: javascript jquery html css

我正在努力修复我的滑动效果。

我希望黄色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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

这是你在找什么?

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

我已将<div class="two">移至一个。

&#13;
&#13;
var clicked=true;
$(".one").on('click', function(){
    if(clicked)
    {
        clicked=false;
        $(".two").css({"top": 0});
    }
    else
    {
        clicked=true;
        $(".two").css({"top": "40px"});
    }
});
&#13;
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;
}*/
&#13;
<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>
&#13;
&#13;
&#13;