Div点击另一个div,从页脚上方滑动

时间:2014-11-27 07:16:32

标签: javascript jquery html css css3

我在从页面底部滑动div时遇到了麻烦。

这是我的JSFIDDLE

当我点击蓝色框(箭头悬停)时,div会向上滑动(box-main)。

但是为了再次隐藏div(box-main),我必须在box-main div中单击。

相反,当我再次点击蓝色框(箭头悬停)时,我想要隐藏div。我尝试了很多,但没有成功。

而且我也使用了固定在两个盒子上的位置,所以当我放大页面时,盒子是固定的,它隐藏了页面的其余部分。如果我提供绝对或相对位置Div(box-main)在页脚下方可见,这看起来不太好。有没有其他方法可以做到这一点。

这是我的代码

HTML:

 <div id="container">
<div id="box-main"></div>
<div id="arrow-hover"></div>
    <footer></footer>
</div>

CSS:

    #container
{
    margin:0;
    width:100%;
    height:100%;
}

#box-main{
    position: fixed;
    left:150px;
    bottom: -150px;
    width: 250px;
    height: 150px;
    background: #000;
    z-index: 100;
}
#arrow-hover{
    position: fixed;
    bottom: 50px;
    left: 250px;
    width: 50px;
    height: 50px;
    background: blue;
    z-index: 100;

}
footer
{
    width:100%;
    background:green;
    height:50px;
    bottom:0;
    left:0;
    position:absolute;
    z-index:500;
}

Jquery的:

 $('body').on('click','#arrow-hover',function(){
    $('#arrow-hover').animate({
        bottom: '200px'
    },250);
    $('#box-main').animate({
        bottom: '50px'
    },250);
});



$('body').on('click','#box-main',function(){
    $('#arrow-hover').animate({
        bottom: '50px'
    },250);
    $('#box-main').animate({
        bottom: '-150px'
    },250);
});

而且我想在(箭头悬停)div中放一个向上的箭头,所以当点击div向上移动然后箭头应该向下,所以当再次点击时div隐藏。有没有办法在Jquery或javascript或css3中执行此操作。

任何帮助将不胜感激,非常感谢。

3 个答案:

答案 0 :(得分:2)

您可以维护一个布尔变量并检查其值并相应地显示或隐藏div。

See updated fiddle

var clicked = false;

$('body').on('click','#arrow-hover',function(){
    if(!clicked){
        $('#arrow-hover').animate({
            bottom: '200px'
        },250);
        $('#box-main').animate({
            bottom: '50px'
        },250);
        clicked = true;
    }
    else{
        $('#arrow-hover').animate({
            bottom: '50px'
        },250);
        $('#box-main').animate({
            bottom: '-150px'
        },250);
        clicked = false;
    }
});

希望这有帮助。

答案 1 :(得分:2)

因为我不依赖变量,所以我只在主盒中添加了一个类:&#34;可见&#34;

这个类在两个动画中都被切换,因此当你显示它时它被设置,当你隐藏它时被移除:

更新:要在可点击的div中添加一些箭头,我们添加一个&#34;向上箭头&#34;类到html中的#arrow-hover元素:

<div id="container">
  <div id="box-main"></div>
  <div id="arrow-hover" class"arrow-up"></div>
  <footer></footer>
</div>

然后我们将toggleClass函数添加到你的箭头悬停点击处理程序,以在箭头向上和向下箭头类之间切换

$('body').on('click','#arrow-hover',function(){
if($('#box-main').hasClass("visible"))
{
    $('#arrow-hover').animate({
        bottom: '50px'
    },250).toggleClass("arrow-up arrow-down");
    $('#box-main').animate({
        bottom: '-150px'
    },250).toggleClass("visible");
}else
{ 
    $('#arrow-hover').animate({
        bottom: '200px'
    },250).toggleClass("arrow-up arrow-down");
    $('#box-main').animate({
        bottom: '50px'
    },250).toggleClass("visible");
}
});

现在要做的就是在你的CSS中定义向上箭头和向下箭头的类并设置背景图像。

出于这些目的,我使用图标字体&#34; font awesome&#34; http://jsfiddle.net/hapttwf6/28/ - &GT;这是它的样子;)

答案 2 :(得分:1)

看到我更新的小提琴:

&#13;
&#13;
$('body').on('click','#arrow-hover',function(){
    $('#arrow-hover').animate({
        bottom: '200px'
    },250);
      $('#box-main').animate({
        bottom: '50px'
    },250);
    if($('#arrow-hover').css('bottom')!='50px'){
    $('#arrow-hover').animate({
        bottom: '50px'
    },250);
        $('#box-main').animate({
        bottom: '-150px'
    },250);
    }
});
&#13;
#container
{
    margin:0;
    width:100%;
    height:100%;
}

#box-main{
    position: fixed;
    left:150px;
    bottom: -150px;
    width: 250px;
    height: 150px;
    background: #000;
    z-index: 100;
}
#arrow-hover{
    position: fixed;
    bottom: 50px;
    left: 250px;
    width: 50px;
    height: 50px;
    background: blue;
    z-index: 100;

}
footer
{
    width:100%;
    background:green;
    height:50px;
    bottom:0;
    left:0;
    position:absolute;
    z-index:500;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="container">
<div id="box-main"></div>
<div id="arrow-hover"></div>
    <footer></footer>
</div>
&#13;
&#13;
&#13;

http://jsfiddle.net/hapttwf6/27/

相关问题