forked from philbuchanan/Accordion-Shortcodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccordion.js
80 lines (66 loc) · 1.65 KB
/
accordion.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
(function($) {
'use strict';
var allTitles = $('.accordion-title'),
closeMe = $('span.close'),
allPanels = $('.accordion-content').hide(),
firstPanel = $('.accordion-content:first-of-type'),
duration = 250,
settings = {
// Set defaults
autoClose: true,
openFirst: false,
openAll: false,
clickToClose: false,
scroll: false
};
// Check for accordion settings variable passed from WordPress
if (typeof accordionSettings !== 'undefined') {
settings = accordionSettings;
}
// Open the first or all accordion items
if (settings.openAll) {
allPanels.show();
allTitles.addClass('open');
}
else if (settings.openFirst) {
firstPanel.prev().addClass('open');
firstPanel.slideDown(duration);
}
// Add event listener
function closeAll() {
allPanels.slideUp(duration);
allTitles.removeClass('open');
};
// Add event listener
closeMe.click(function(){
// Close all accordion items
if (settings.autoClose) {
closeAll();
}
});
allTitles.click(function() {
// Only open the item if item isn't already open
if (!$(this).hasClass('open')) {
// Close all accordion items
if (settings.autoClose) {
closeAll();
}
// Open clicked item
$(this).next().slideDown(duration, function() {
// Scroll page to the title
if (settings.scroll) {
$('html, body').animate({
scrollTop: $(this).prev().offset().top
}, duration);
}
});
$(this).addClass('open');
}
// If item is open, and click to close is set, close it
else if (settings.clickToClose) {
$(this).next().slideUp(duration);
$(this).removeClass('open');
}
return false;
});
}(jQuery));