jQuery UI Accordion Anchors
Add anchors to jQuery 1.10.0+ which open the associated panel when the location hash matches.
by Mottie
HTML
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/cupertino/jquery-ui.css">
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<br>
<div id="accordion">
<h3>Section 1</h3>
<div>
<p>Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.</p>
</div>
<h3>Section 2</h3>
<div>
<p>Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In suscipit faucibus urna.</p>
</div>
<h3>Section 3</h3>
<div>
<p>Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis. Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui.</p>
<ul>
<li>List item one</li>
<li>List item two</li>
<li>List item three</li>
</ul>
</div>
<h3>Section 4</h3>
<div>
<p>Cras dictum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia mauris vel est.</p>
<p>Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</p>
</div>
</div>
<br>
<input id="hash" placeholder="Current hash">
<div class="spacer"></div>
CSS
.ui-accordion {
position: relative;
}
.ui-accordion .accordion-link {
position: absolute;
right: 2%;
margin-top: 16px;
z-index: 1;
width: 12px;
height: 12px;
background: url(http://i57.tinypic.com/fyfns4.png) center center no-repeat;
/* approx 12x12 link icon */
}
/* demo css below */
.spacer { height: 1000px; }
JavaScript
$(function () {
var hashId = 0,
$accordion = $('#accordion');
if (window.location.hash) {
$accordion.children('h3').each(function (i) {
var txt = this.textContent.toLowerCase().replace(/\s+/g, '_');
if ( txt === window.location.hash.slice(1) ) {
hashId = i;
}
});
}
$accordion.accordion({
active: hashId,
animate: false,
heightStyle: 'content',
collapsible: true,
create: function (event, ui) {
$accordion.children('h3').each(function (i) {
// set id here because jQuery UI sets them as "ui-accordion-#-header-#"
this.id = this.textContent.toLowerCase().replace(/\s+/g, '_');
// add the anchor
$(this).before('<a class="accordion-link link" data-index="' + i +
'" href="#' + this.id + '"></a>');
});
$accordion.find('.accordion-link').click(function () {
// the active option requires a numeric value (not a string, e.g. "1")
$accordion.accordion( "option", "active", $(this).data('index') );
/* one line of demo code below, don't include in production */
showHash();
// uncomment out the return false below to prevent the header jump
// return false;
});
}
});
});
// demo code below, delay to allow hash update
function showHash(){
setTimeout(function(){
$('#hash').val( window.location.hash );
}, 200);
};