Reputation: 904
Have a look at this code here, as you will see I'm just doing a bit of practice with jQuery and trying to build a tab panel.
I got stuck on trying to get the sections to move out of the way once i clicked on a new section.
I have no idea why this is not working for me like this.
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div class="buttons">
<a href="" class="active_button" data-sectionId="section1">Section 1</a>
<a href="" data-sectionId="section2">Section 2</a>
<a href="" data-sectionId="section3">Section 3</a>
<a href="" data-sectionId="section4">Section 4</a>
</div>
<div class="sections">
<div class="section active_section" id="section1">
Section 1 <br>
Section 1 <br>
Section 1 <br>
Section 1 <br><br>
</div>
<div class="section" id="section2">
Section 2 <br>
Section 2 <br>
Section 2 <br>
Section 2 <br> <br>
</div>
<div class="section" id="section3">
Section 3 <br>
Section 3 <br>
Section 3 <br>
Section 3 <br><br>
</div>
<div class="section" id="section4">
Section 4 <br>
Section 4 <br>
Section 4 <br>
Section 4 <br><br>
</div>
</div>
</body>
</html>
sass
body
text-align: center
padding-top: 50px
a
color: white
text-decoration: none
padding: 10px
background-color: grey
margin: 0px -1px
border-radius: 10px 10px 0px 0px
transition: all 0.3s ease-in-out
&:hover
background-color: lightgrey
.active_button
background-color: lightgrey
.sections
position: relative
.section
display: none
padding: 20px
background-color: lightgrey
position: absolute
width: 286px
top: 10px
left: 50%
margin-left: -163px
border-radius: 0 0 10px 10px
.active_section
display: block
jquery
$(function() {
// capture click of section button
$("a").click(function(e) {
// prevent default link behaviour
e.preventDefault();
// hide the current active section
$(".section .active_section").slideUp(500, function(){
// then take away their active class
$(this).removeClass("active_section");
});
}); // click function closes here
// find out what section button is pressed
var sectionId = $("a").attr("data-sectionId");
// slide down that section
$("#"+sectionId).slideDown(500, function(){
// add the active class
$(this).addClass("active_section");
});
});
Upvotes: 0
Views: 58
Reputation: 719
The below function will give you smooth transition while sliding up and down
$(function() {
// capture click of section button
$("a").click(function(e) {
// prevent default link behaviour
e.preventDefault();
// hide the current active section
$(".active_section").slideUp(500, function(){
// then take away their active class
$(this).removeClass("active_section");
// find out what section button is pressed
});
var sectionId = $(this).attr("data-sectionId");
setTimeout(function() {
// slide down that section
$("#"+sectionId).slideDown(500, function(){
// add the active class
$(this).addClass("active_section");
});
}, 500);
}); // click function closes here
});
Upvotes: 0
Reputation: 1919
When you write $('.section .activeSection')
- this means that select an element with class activeSection which is a child of an element with class section. Also the whole code should be inside the click event:
$("a").click(function (e) {
// prevent default link behaviour
e.preventDefault();
var currentAnchor = $(this);
// hide the current active section
$(".active_section").slideUp(500, function () {
// then take away their active class
$(this).removeClass("active_section");
$('.active_button').removeClass('active_button');
$(currentAnchor).addClass('active_button');
});
// find out what section button is pressed
var sectionId = $(this).attr("data-sectionId");
console.log(sectionId);
// slide down that section
$("#" + sectionId).slideDown(500, function () {
// add the active class
$(this).addClass("active_section");
});
});
Upvotes: 1
Reputation: 29683
Check the inline comments for JS
Updated JS
$(function() {
$("a").click(function(e) {
e.preventDefault();
$(".section.active_section").slideUp(500, function(){
$(this).removeClass("active_section");
});
//You need to identify clicked element inside click event itself
//get the sectionId of clicked element using $(this)
var sectionId = $(this).attr("data-sectionId");
$("#"+sectionId).slideDown(500, function(){
$(this).addClass("active_section");
});
});
});
Upvotes: 0
Reputation: 516
please check this
[https://jsfiddle.net/k2v06bnv/][1]
small problems in the code , you have to be more careful about small things
Upvotes: 2
Reputation: 295
Move all your code to the click function, and it's work ;)
https://jsfiddle.net/pgytuq6j/
// capture click of section button
$("a").click(function(e) {
// prevent default link behaviour
e.preventDefault();
// find out what section button is pressed
var sectionId = $(this).attr("data-sectionId");
// hide the current active section
$(".active_section").slideUp(500, function(){
// then take away their active class
$(this).removeClass("active_section");
});
// slide down that section
$("#"+sectionId).slideDown(500, function(){
// add the active class
$(this).addClass("active_section");
});
}); // click function closes here
This is what you want ?
Upvotes: 2