I've created this fiddle to demonstrate something I've been working on. I've been trying to mimic the behaviour of Twitter where if a tweet is expanded, the border-radius and margins of the previous and next tweets are altered.
I've used toggleclass to get this working. I would like advice about how to clean up my code. I'm sure I have more lines of code than I need and I'm sure there are more simple or better ways to achieve the desired effect.
$(document).ready(function(){
$(".things").hide();
$(".listname .btn-group").hide();
$(".expand, .expandfirst").click(function(){
$(this).prev(".btn-group").toggle();
$(this).children("span").toggle();
$(this).toggleClass('active');
$(this).closest(".list").find(".things").toggle();
$(this).parents(".list").next().find(".expand").toggleClass('after-expanded');
$(this).parents(".list").prev().find(".expand").toggleClass('before-expanded');
$("#listview, #thingview").removeClass('active');
});
$(".expand").click(function(){
$(this).toggleClass('expanded-list');
});
$("#listview").click(function(){
$(".things").hide();
$(".listname .btn-group").hide();
$(".listname a span").show();
$(".expand").removeClass('active expanded-list after-expanded');
$(".expandfirst").removeClass('active');
});
$("#thingview").click(function(){
$(".things").show();
$(".listname .btn-group").show();
$(".listname a span").hide();
$(".expand").addClass('active expanded-list after-expanded');
$(".expandfirst").addClass('active');
});
});
@import url('http://twitter.github.com/bootstrap/assets/css/bootstrap.css');
#lists {
padding-top: 20px;
}
.nav > li > a {
font-weight: bold;
}
.list .btn-group, .list .share {
margin: 7px;
}
.header {
background: #fff;
border: 1px solid #ccc;
border-radius: 5px 5px 0 0;
font-weight: bold;
padding: 8px;
}
.header h4 {
margin: 5px 0;
}
.header span {
margin-top: 3px;
}
.list ol {
border: 1px solid #ccc;
border-top: none;
border-radius: 0 0 5px 5px;
background-color: #fff;
list-style-position: inside;
margin: 0;
padding: 0;
}
.list ol li {
border-top: 1px solid #ccc;
padding: 8px;
position: relative;
}
.list ol li:first-child {
border-top: none;
}
.list .btn-group {
margin-top: 8px;
}
.listname {
font-weight: bold;
}
.things li a {
margin-left:14px;
}
.list a.btn {
text-align: left;
border: 1px solid #ccc;
border-top: none;
border-radius: 0;
}
.expand, .expandfirst {
display: block;
padding: 8px 0;
}
.list a.before-expanded {
border-bottom-left-radius: 6px;
border-bottom-right-radius: 6px;
}
.list a.expanded-list, .list a.after-expanded {
border-top-left-radius: 6px;
border-top-right-radius: 6px;
border-top: 1px solid #ccc;
margin-top: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="lists">
<div class="header">
<h4 style="display: inline-block;">Header</h4>
<div class="btn-group pull-right">
<button class="agree btn btn-mini dropdown-toggle" data-toggle="dropdown">Sort by</button>
<ul class="dropdown-menu">
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
</ul>
</div>
</div>
<div class="list">
<div class="listname">
<button class="share btn btn-mini pull-left"><i class="icon-heart-logo"></i></button>
<div class="btn-group pull-right">
<button class="btn btn-mini dropdown-toggle pull-right" data-toggle="dropdown"><i class="icon-5hearts"></i></button>
<ul class="dropdown-menu">
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
</ul>
</div>
<a class="expandfirst btn" href="#">Colors<span class="pull-right" style="margin-right: 8px;">56,789</span></a>
</div>
<ol class="things">
<li><a href="#">White</a><span class="pull-right">56,789</span></li>
<li><a href="#">Blue</a><span class="pull-right">56,789</span></li>
<li><a href="#">Red</a><span class="pull-right">56,789</span></li>
<li><a href="#">Green</a><span class="pull-right">56,789</span></li>
<li><a href="#">Orange</a><span class="pull-right">56,789</span></li>
</ol>
</div>
<div class="list">
<div class="listname">
<button class="share btn btn-mini pull-left"><i class="icon-heart-logo"></i></button>
<div class="btn-group pull-right">
<button class="btn btn-mini dropdown-toggle pull-right" data-toggle="dropdown"><i class="icon-5hearts"></i></button>
<ul class="dropdown-menu">
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
</ul>
</div>
<a class="expand btn" href="#">Colors<span class="pull-right" style="margin-right: 8px;">56,789</span></a>
</div>
<ol class="things">
<li><a href="#">White</a><span class="pull-right">56,789</span></li>
<li><a href="#">Blue</a><span class="pull-right">56,789</span></li>
<li><a href="#">Red</a><span class="pull-right">56,789</span></li>
<li><a href="#">Green</a><span class="pull-right">56,789</span></li>
<li><a href="#">Orange</a><span class="pull-right">56,789</span></li>
</ol>
</div>
<div class="list">
<div class="listname">
<button class="share btn btn-mini pull-left"><i class="icon-heart-logo"></i></button>
<div class="btn-group pull-right">
<button class="btn btn-mini dropdown-toggle pull-right" data-toggle="dropdown"><i class="icon-5hearts"></i></button>
<ul class="dropdown-menu">
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
</ul>
</div>
<a class="expand btn" href="#">Colors<span class="pull-right" style="margin-right: 8px;">56,789</span></a>
</div>
<ol class="things">
<li><a href="#">White</a><span class="pull-right">56,789</span></li>
<li><a href="#">Blue</a><span class="pull-right">56,789</span></li>
<li><a href="#">Red</a><span class="pull-right">56,789</span></li>
<li><a href="#">Green</a><span class="pull-right">56,789</span></li>
<li><a href="#">Orange</a><span class="pull-right">56,789</span></li>
</ol>
</div>
<div class="list">
<div class="listname">
<button class="share btn btn-mini pull-left"><i class="icon-heart-logo"></i></button>
<div class="btn-group pull-right">
<button class="btn btn-mini dropdown-toggle pull-right" data-toggle="dropdown"><i class="icon-5hearts"></i></button>
<ul class="dropdown-menu">
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
</ul>
</div>
<a class="expand btn" href="#">Colors<span class="pull-right" style="margin-right: 8px;">56,789</span></a>
</div>
<ol class="things">
<li><a href="#">White</a><span class="pull-right">56,789</span></li>
<li><a href="#">Blue</a><span class="pull-right">56,789</span></li>
<li><a href="#">Red</a><span class="pull-right">56,789</span></li>
<li><a href="#">Green</a><span class="pull-right">56,789</span></li>
<li><a href="#">Orange</a><span class="pull-right">56,789</span></li>
</ol>
</div>
</div>