Reputation: 1078
I'm currently trying to build a quick dropdown prototype with jQuery and I'm probably at the limit of my jQuery knowledge so I was hoping for some help in solving my problem.
What I'm aiming to achieve: when the user hovers over a link, a dropdown animates down, when the user clicks another link, the next dropdown animates over the top and so on.
Here's the HTML:
<div class="wrapper">
<div class="dropdowns">
<ul>
<li class="call"><a href="#">Call</a></li>
<li class="chat"><a href="#">Chat</a></li>
<li class="message"><a href="#">Send a message</a></li>
</ul>
</div>
<div class="slide call-panel">
<h1>Call now</h1>
</div>
<div class="slide chat-panel">
<h1>Online chat</h1>
</div>
<div class="slide message-panel">
<h1>Send us a message</h1>
</div>
</div>
CSS:
* {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
.wrapper {
max-width: 1200px;
margin: 0 auto;
}
.dropdowns {
overflow: hidden;
}
ul {
list-style: none;
margin: 0;
padding: 0;
}
li {
display: inline;
}
.call a {
background: #f1f1f1;
}
.chat a {
background: #e1e1e1;
}
.message a {
background: #f1f1f1;
}
a {
display: block;
width: 33.333%;
height: 50px;
line-height: 50px;
text-align: center;
float: left;
}
.call-panel {
height: 300px;
background: darkgrey;
display: none;
}
.chat-panel {
height: 300px;
background: darkgrey;
display: none;
}
.message-panel {
height: 300px;
background: darkgrey;
display: none;
}
h1 {
margin: 0;
padding: 0;
}
JS:
$( ".call a" ).click(function() {
toggleSlides( ".call-panel" );
});
$( ".chat a" ).click(function() {
toggleSlides( ".chat-panel" );
});
$( ".message a" ).click(function() {
toggleSlides( ".message-panel" );
});
function toggleSlides(slide){
$(".slide").slideUp ( "slow", function(){
$(slide).slideDown( "slow" );
} );
}
I've set up a quick fiddle here of what I have currently, but as you'll see it's not quite working how I intended, I'm getting animations coming up rather than down, some crazy repeat animations, all sorts - any help would be great!
Link: http://jsfiddle.net/2zwjZ/
Upvotes: 0
Views: 69
Reputation: 4609
you can do this with jquery this.hash;
$('.dropdowns li a').removeClass('active');
$('.dropdowns li a').click(function(){
var tabDiv = this.hash;
$('.slide').hide();
$(this).addClass('active');
$(tabDiv).slideDown();
return false;
});
Upvotes: 2