Bei einem Projekt sollen einzelne Akkordeons direkt von anderen Unterseiten angesprungen werden. Soweit so gut, mit folgendem Code im Template j_accordion klappt das auch wunderbar. Jetzt sollen aber standardmäßig alle Akkordeons, wenn man nicht von einer anderen Seite auf ein spezielles springt, geschlossen sein. Wie bekomme ich das noch hin?
Der Code:
Code:
<script src="<?php echo TL_ASSETS_URL; ?>assets/jquery/ui/<?php echo $GLOBALS['TL_ASSETS']['JQUERY_UI']; ?>/jquery-ui.min.js"></script><!-- comment for alteration 1 -->
<!-- <script src="<?php echo TL_ASSETS_URL; ?>assets/jquery/ui/<?php echo JQUERY_UI; ?>/jquery-ui.min.js"></script> --><!-- uncomment for alteration 1 -->
<script>
(function($) {
$(document).ready(function() {
// var hash = window.location.hash.replace('#', '');
// if (hash && $('section#' + hash + '.ce_accordion').length) {
// setTimeout(function() {
// window.scrollTo(0, 0);
// }, 1);
// } // uncomment block for alteration 2
var chosen = function() {
var hash = window.location.hash.replace('#', '');
if (hash === '') return null;
return document.getElementById(hash);
};
var active = 0; // comment for alteration 3
// var active = false; // uncomment for alteration 3
var element = chosen();
if (element !== null) {
active = $('section.ce_accordion').index(element);
}
$(document).accordion({
// Put custom options here
active: active,
heightStyle: 'content',
header: 'div.toggler',
collapsible: true,
create: function(event, ui) {
ui.header.addClass('active');
},
activate: function(event, ui) {
ui.newHeader.addClass('active');
ui.oldHeader.removeClass('active');
var id = ui.newHeader.parent().attr('id'),
hash = window.location.hash.replace('#', '');
if (id && id !== hash) {
history.pushState(null, null, window.location.pathname + '#' + id); // comment for alteration 4
// history.replaceState(null, null, window.location.pathname + '#' + id); // uncomment for alteration 4
}
}
});
$(window).on('hashchange', function() {
var element = chosen();
if (element !== null) {
$(element).children('div.toggler').trigger('click');
}
});
});
})(jQuery);
</script>
Lesezeichen