在这里查看此代码,因为您将看到我正在使用jQuery进行一些练习并尝试构建选项卡面板。
一旦我点击了新的部分,我就试图让这些部分移开。
我不知道为什么这对我不起作用。
<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");
});
});
答案 0 :(得分:2)
将您的所有代码移至点击功能,并将其工作;)
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
这是你想要的吗?
答案 1 :(得分:2)
请检查
[https://jsfiddle.net/k2v06bnv/][1]
代码中的小问题,你必须更加小心小事
答案 2 :(得分:1)
当您编写$('.section .activeSection')
时 - 这意味着选择具有类activeSection的元素,该类是具有类节的元素的子元素。整个代码也应该在click事件中:
$("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");
});
});
答案 3 :(得分:0)
<强> DEMO 强>
检查JS的内联注释
更新了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");
});
});
});
答案 4 :(得分:0)
以下功能可让您在上下滑动时平滑过渡
$(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
});