简体   繁体   English

尝试使用 - 顶部定位和jquery动画在点击时使div向下和向上滑动

[英]trying to make a div slide down and up from top of page using - top positioning and jquery animate upon click

basically when a user clicks the .selector element the div .dropDown should slide up -100px and when they click again it should slide down to top: 0px. 基本上当用户点击.selector元素时,div .dropDown应向上滑动-100px,当它们再次单击时,它应向下滑动到顶部:0px。

$(document).ready(function(){
    var orig = $(".dropDown").outerHeight(); // 104
    var top = $(".dropDown").css("top");
if(top == "0px"){
    $(".selector").on("click", function(e){
        $(".dropDown").animate({top : "-100px"}, 400, 
            function(){
var top = $(".dropDown").css("top");
alert(top);                 
        })
    })
}
// else{
//  $(".selector").on("click", function(e){
//      $(".dropDown").animate({top : "0px"}, 400);
//      $("body").css({"background-color" : "green"})
//  })
// }
if($(".dropDown").css("top") == "-100px"){
    $(".selector").on("click", function(e){
        $(".dropDown").animate({top : "0px"}, 400);
        $("body").css({"background-color" : "green"})

    })
}

});

logic: if the dropDown div's top position is zero that means that the div is open(visible). 逻辑:如果dropDown div的顶部位置为零,则意味着div是打开的(可见)。 when the user clicks the button to hide the dropDown div the div goes to -100px(hidden). 当用户单击按钮隐藏dropDown div时,div将转到-100px(隐藏)。 then if the user wants to see the div again they click the button and the div goes back down to top: 0. 然后,如果用户想再次看到div,他们点击按钮,然后div返回到顶部:0。

Im having problem when the top is at -100px and when i click the button the dropdown doesnt slide down. 当顶部处于-100px时我遇到问题,当我点击按钮时,下拉列表不会向下滑动。 please help with that. 请帮忙。 Thanks. 谢谢。

while I was setting up the jsfiddle I realised that what I have so far works in FF but not in chrome. 当我设置jsfiddle时,我意识到我到目前为止在FF中工作但在chrome中没有。 that is weird to me, if you can help me solve that problem too that would be also great. 这对我来说很奇怪,如果你能帮助我解决这个问题也会很棒。

You can achieve this by laying out your div as it should appear while expanded, and set display:none , but take the clickable tab out as a child element so that it is always visible. 您可以通过布置展开时应显示的div来实现此目的,并设置display:none ,但将可单击的选项卡作为子元素输出,以便始终可见。 Then you can simplify your javascript quite a bit by using slideToggle . 然后你可以使用slideToggle简化你的javascript。 The 300 value just specifies how fast you want it to slide. 300值仅指定您希望它滑动的速度。

Example: 例:

$(document).ready(function(){
     $('.selector').click(function () {
         $('.dropDown').slideToggle(300);
     });
});

updated jsFiddle 更新了jsFiddle

Edit 编辑

Maintaining the border at this point is just styling, you can add a container div that holds your Information tab, and just give that a top-border. 此时维护边框只是样式,您可以添加一个包含“信息”选项卡的容器div,并将其设置为顶部边框。 Here is a further updated jsFiddle . 这是一个进一步更新的jsFiddle

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

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