8

I want to achieve this type of a slide down except instead of on hover I think I need some sort of script that triggers the slide down on click and then click again to trigger the reverse slide up. I will have a div thats hidden (top: -400px; above the top of the page) and when the button is clicked with slide down and sit at top: 0;

HTML

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

CSS

.container {
overflow:hidden;
height: 60px;
}

.one {
position: relative;
top: 0;
background-color: lightblue;
z-index: 1;
}

.two {
position: relative;
top: -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;
}

Here is a working fiddle http://jsfiddle.net/8ZFMJ/2/ - any help would be appreciated.

I have tried using slideToggle however this creates an 'expanding' effect which isn't the type of slide down I want to achieve.

Many thanks.

user2498890
  • 1,528
  • 4
  • 25
  • 58

6 Answers6

12

Try this http://jsfiddle.net/webcarvers/8ZFMJ/34/

remove this:

.one:hover + .two {
    top: 0px;
}

add this with jQuery Js

$(document).ready(function(){
var clicked=true;
$(".one").on('click', function(){
    if(clicked)
    {
        clicked=false;
        $(".two").css({"top": 0});
    }
    else
    {
        clicked=true;
        $(".two").css({"top": "-40px"});
    }
});
});
Mayank Tripathi
  • 1,346
  • 7
  • 21
6

almost the same (as the other answer) just also working if you have more than one container:

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

$('.one').on('click',function(){
    $(this).next('.two').slideToggle();
});
caramba
  • 21,963
  • 19
  • 86
  • 127
2

You can use slideToggle():

$('.one').click(function() {
    $('.two').slideToggle();    
})

Fiddle Demo

Felix
  • 37,892
  • 8
  • 43
  • 55
  • You can also tweak animation speed by using `$('.two').slideToggle('fast'); ` or `$('.two').slideToggle('slow'); ` – SimplyAzuma Feb 27 '14 at 14:46
  • Thanks for your response, I have seen this however I don't want the content to 'expand' I want the whole thing to slide down. It's hard for me to explain. If you look at the difference between the on hover and your fiddle you should see what I mean and the effect I want to create. – user2498890 Feb 27 '14 at 14:47
1

I think you need to use jQuery's slideToggle() function, have you tried that?

b4rbika
  • 147
  • 1
  • 7
  • Hi, thanks for your response, yeah I've seen that but it creates a sort of 'expanding' effect I want the whole div to slide down like in the fiddle I included at the top. – user2498890 Feb 27 '14 at 14:48
1

You need to use .slideToggle()

$(".one").on('click',function(){
    $(".two").slideToggle();
});

Demo

Dhaval Marthak
  • 17,246
  • 6
  • 46
  • 68
  • Hi, thanks for your response, yeah I've seen that but it creates a sort of 'expanding' effect I want the whole div to slide down like in the fiddle I included at the top – user2498890 Feb 27 '14 at 14:49
0

You can do it using pure CSS: http://jsfiddle.net/8ZFMJ/33/

HTML:

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

CSS:

.one:focus + .two {
    top: 0px;
}

You can also remove the link styling to your taste, so it looks like regular text.