I am trying to create collapsible element where I want selected item expended by default when first loaded. Right now all the items are expended by default.
I want for example, first and last item to be expended by default until I click to collapse it and 2nd and 3rd to be colllapsed by default until I click to expend it.
Here is the code I am working with.
Would really appreciate your help.
Also I don't have understanding of how this code functions. So could please someone explain to me how the javascript is functioning?
thank you.
Best,
// expendable info
$(document).ready(function(){
$('.buy-info .tit').click(function(){
$(this).toggleClass('active');
$(this).next('.con').slideToggle(300);
});
});
.buy-info .notice {
position: relative;
background: #f4f4f4;
border-bottom: 1px solid #fff;
text-align: center;
font-size: 14px; line-height: 40px;
cursor: pointer;
}
.buy-info .tit {
position: relative;
background: #f4f4f4;
border-bottom: 1px solid #fff;
text-align: center;
font-size: 14px; line-height: 40px;
cursor: pointer;
}
.buy-info .tit:after {
content: '';
position: absolute; top: 14px; right: 12px;
width: 6px; height: 6px;
border: solid #111;
border-width: 2px 0 0 2px;
transform: rotate(225deg);
transition: all .3s;
}
.buy-info .tit.active:after {
top: 18px;
transform: rotate(45deg);
}
.buy-info .con {
/*display: none;*/
padding: 20px;
font-size: 11px; line-height: 1.4;
}
#prdInfo {
display: none;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" href="untitled.css">
<script src="untitled.js"></script>
</head>
<body>
<div class="buy-info">
<div class="notice">NOTICE</div>
<div class="tit active"> NOTICE 1</div>
<div class="con">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
<div class="tit active">NOTICE 2</div>
<div class="con">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
<div class="tit active">Notice3</div>
<div class="con">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
<div class="tit">Notice 4</div>
<div class="con">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
</div>
</body>
</html>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
You could do it like this:
$(document).ready(function() {
$('.buy-info .tit').click(function() {
$(this).toggleClass('active');
!$(this).hasClass('active') ? $(this).next('.con').slideUp(300) : $(this).next('.con').slideDown(300);
});
$('.buy-info .tit.active').each(function() {
!$(this).hasClass('active') ? $(this).next('.con').slideUp(300) : $(this).next('.con').slideDown(300);
})
});
So you add the class .active
to those you want to be displayed by default.
I've also enabled the line display:none
in .buy-info .con
Demo
// expendable info
$(document).ready(function() {
$('.buy-info .tit').click(function() {
$(this).toggleClass('active');
!$(this).hasClass('active') ? $(this).next('.con').slideUp(300) : $(this).next('.con').slideDown(300);
});
$('.buy-info .tit.active').each(function() {
!$(this).hasClass('active') ? $(this).next('.con').slideUp(300) : $(this).next('.con').slideDown(300);
})
});
.buy-info .notice {
position: relative;
background: #f4f4f4;
border-bottom: 1px solid #fff;
text-align: center;
font-size: 14px;
line-height: 40px;
cursor: pointer;
}
.buy-info .tit {
position: relative;
background: #f4f4f4;
border-bottom: 1px solid #fff;
text-align: center;
font-size: 14px;
line-height: 40px;
cursor: pointer;
}
.buy-info .tit:after {
content: '';
position: absolute;
top: 14px;
right: 12px;
width: 6px;
height: 6px;
border: solid #111;
border-width: 2px 0 0 2px;
transform: rotate(225deg);
transition: all .3s;
}
.buy-info .tit.active:after {
top: 18px;
transform: rotate(45deg);
}
.buy-info .con {
display: none;
padding: 20px;
font-size: 11px;
line-height: 1.4;
}
#prdInfo {
display: none;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" href="untitled.css">
<script src="untitled.js"></script>
</head>
<body>
<div class="buy-info">
<div class="notice">NOTICE</div>
<div class="tit active"> NOTICE 1</div>
<div class="con">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
<div class="tit">NOTICE 2</div>
<div class="con">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
<div class="tit">Notice3</div>
<div class="con">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
<div class="tit active">Notice 4</div>
<div class="con">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
</div>
</body>
</html>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>