-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (54 loc) · 1.86 KB
/
index.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
/**
* ## jQueryAccordion module Usage
*
* ### Markup:
* <dl class="accordion">
* <dt data-id="tab1"><h3><a href="#tab1">Tab Title</a></h3></dt>
* <dd>Panel Content</dd>
* <dt data-id="tab1"><h3><a href="#tab2">Tab Title</a></h3></dt>
* <dd>Panel Content</dd>
* </dl>
*
* ### JS:
* $.fn.accordion = require('./jquery.accordion.js');
* $('dl.accordion').accordion();
*/
export default (($) => {
return function() {
//VARIABLES
const $tabs = $('dl.accordion dt'),
hashtag = window.location.hash,
panelId = window.location.hash.replace('#','');
//FUNCTIONS
const toggleAccPanelFn = function( $this_hash, $this_tab, $this_panel ){
if( $this_tab.hasClass('active') && $this_panel.is(':visible') ){
$this_panel.slideUp();
$this_tab.removeClass('active').addClass('inactive');
}
else{
$this_panel.slideDown();
$this_tab.addClass('active').removeClass('inactive');
window.location.hash = $this_hash;
}
}
//TAB ANCHOR CLICKS
$tabs.find('a').on('click', function(e){
e.preventDefault()
const this_hash = $(this).attr('href');
const this_tab = $(this).closest('dt');
const this_panel = this_tab.next();
toggleAccPanelFn(this_hash, this_tab, this_panel);
});
//PRESELECTED OPEN PANEL (USE OF HASHTAGS)
if( hashtag && $('dl.accordion dt[data-id="'+panelId+'"]').length>0 ){
const selected_tab = $('dl.accordion dt[data-id="'+panelId+'"]');
const selected_panel = selected_tab.next();
const accordionPos = selected_tab.offset().top;
//open panel
toggleAccPanelFn(hashtag, selected_tab, selected_panel)
//set scroll position to the open accordion panel
$(document).scrollTop(accordionPos);
//$('html, body').animate({ scrollTop: accordionPos }, 500);
}
}
})(jQuery)