简体   繁体   English

如何制作部分隐藏的div?

[英]How to make a partially hidden div?

I want to make a half shown div in a page, like a footer. 我想在页中显示一半的div,就像页脚一样。 When I click it I want it to slide up. 当我单击它时,我希望它向上滑动。 The div will contain information on it. div将包含有关它的信息。

I achieved this somehow, but my problem is that the div does not get really hidden it just changes the position. 我以某种方式实现了这一点,但是我的问题是div并没有真正被隐藏,它只是改变了位置。

You can find the demo here: http://jsfiddle.net/8ZFMJ/394/ 您可以在这里找到演示: http : //jsfiddle.net/8ZFMJ/394/

var clicked=false;
$(".two").on('click', function(){
    if(clicked)
    {
        clicked=false;
        $(".two").css({"bottom": -430});
    }
    else
    {
        clicked=true;
        $(".two").css({"bottom": "-200px"});
    }
});

.two must be absolute positioned inside .container that must be relative positioned. .two必须绝对放置在.container内部,并且必须相对.container Then you just change the bottom with a negative value and that will hide the footer. 然后,只需将bottom更改为负值即可隐藏页脚。

CSS: CSS:

html, body { height: 100%; }
.container {
    position: relative;
    height: 100%;
    overflow: hidden;
}
.two {
    position: absolute;
    background-color: yellow;
    width: 100%;
    height:250px;
    bottom: -200px;
    transition: bottom 1s;
}

jQuery: jQuery的:

var clicked=false;
$(".two").on('click', function(){
    if(clicked)
    {
        clicked=false;
        $(".two").css({"bottom": "-200px"});
    }
    else
    {
        clicked=true;
        $(".two").css({"bottom": 0});
    }
});

http://jsfiddle.net/8ZFMJ/398/ http://jsfiddle.net/8ZFMJ/398/

I had a similar problem a while ago, in fact I think it was my first stackoverflow question. 不久前我遇到了类似的问题,实际上我认为这是我的第一个stackoverflow问题。 Unfortunately it got poorly received. 不幸的是,它得到的好评不多。

Anyway, the problem is currently that you are just changing the position of the div - that's what .bottom does. 总之,问题是目前唯一只是改变div的位置-这就是.bottom一样。 I think what you want to do is change the height, see this JSFiddle in which I managed to switch the div between states (no animation yet). 我认为您想要做的是更改高度,请参见此JSFiddle,在其中我设法在状态之间切换div(尚无动画)。

It makes simple use of css's overflow-y: hidden; 它简单地使用了css的overflow-y: hidden; to hide the div's contents when it is small, and all the JS does is toggle between heights: 在div很小时隐藏div的内容,而JS所做的就是在高度之间切换:

if(clicked)
{
    $(".two").css("height", 10);
}
else
{
    $(".two").css("height", 250);
}
clicked = !clicked;

clicked = !clicked just flips the boolean state of the variable. clicked = !clicked只是翻转变量的布尔状态。

Now, to add the animation, we can use jQuery's .animate and produce this beautiful Fiddle 现在,要添加动画,我们可以使用jQuery的.animate并生成这个漂亮的小提琴

Basically, all we had to do in between is use animate instead of css. 基本上,我们之间需要做的就是使用动画而不是CSS。 Simple, really. 简单,真的。

TL;DR TL; DR
final JSFiddle 最终的JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM