2017-08-07 04:08:03 +08:00
|
|
|
|
|
|
|
class ChapterToggle {
|
|
|
|
|
|
|
|
constructor(elem) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.isOpen = elem.classList.contains('open');
|
|
|
|
elem.addEventListener('click', this.click.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
open() {
|
2018-09-22 23:36:35 +08:00
|
|
|
const list = this.elem.parentNode.querySelector('.inset-list');
|
2017-08-07 04:08:03 +08:00
|
|
|
|
|
|
|
this.elem.classList.add('open');
|
|
|
|
list.style.display = 'block';
|
2018-09-22 23:36:35 +08:00
|
|
|
list.style.maxHeight = '';
|
|
|
|
const maxHeight = list.getBoundingClientRect().height + 10;
|
|
|
|
list.style.maxHeight = '0px';
|
2017-08-07 04:08:03 +08:00
|
|
|
list.style.overflow = 'hidden';
|
2018-09-22 23:36:35 +08:00
|
|
|
list.style.transition = 'max-height ease-in-out 240ms';
|
2017-08-07 04:08:03 +08:00
|
|
|
|
|
|
|
let transitionEndBound = onTransitionEnd.bind(this);
|
|
|
|
function onTransitionEnd() {
|
|
|
|
list.style.overflow = '';
|
2018-09-22 23:36:35 +08:00
|
|
|
list.style.maxHeight = '';
|
2017-08-07 04:08:03 +08:00
|
|
|
list.style.transition = '';
|
2018-09-16 15:39:21 +08:00
|
|
|
list.style.display = `block`;
|
2017-08-07 04:08:03 +08:00
|
|
|
list.removeEventListener('transitionend', transitionEndBound);
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(() => {
|
2018-09-22 23:36:35 +08:00
|
|
|
requestAnimationFrame(() => {
|
|
|
|
list.style.maxHeight = `${maxHeight}px`;
|
|
|
|
list.addEventListener('transitionend', transitionEndBound)
|
|
|
|
});
|
2017-08-07 04:08:03 +08:00
|
|
|
}, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
2018-09-22 23:36:35 +08:00
|
|
|
const list = this.elem.parentNode.querySelector('.inset-list');
|
2017-08-07 04:08:03 +08:00
|
|
|
|
|
|
|
list.style.display = 'block';
|
2018-09-22 23:36:35 +08:00
|
|
|
this.elem.classList.remove('open');
|
|
|
|
list.style.maxHeight = list.getBoundingClientRect().height + 'px';
|
2017-08-07 04:08:03 +08:00
|
|
|
list.style.overflow = 'hidden';
|
2018-09-22 23:36:35 +08:00
|
|
|
list.style.transition = 'max-height ease-in-out 240ms';
|
2017-08-07 04:08:03 +08:00
|
|
|
|
2018-09-22 23:36:35 +08:00
|
|
|
const transitionEndBound = onTransitionEnd.bind(this);
|
2017-08-07 04:08:03 +08:00
|
|
|
function onTransitionEnd() {
|
|
|
|
list.style.overflow = '';
|
2018-09-22 23:36:35 +08:00
|
|
|
list.style.maxHeight = '';
|
2017-08-07 04:08:03 +08:00
|
|
|
list.style.transition = '';
|
|
|
|
list.style.display = 'none';
|
|
|
|
list.removeEventListener('transitionend', transitionEndBound);
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(() => {
|
2018-09-22 23:36:35 +08:00
|
|
|
requestAnimationFrame(() => {
|
|
|
|
list.style.maxHeight = `0px`;
|
|
|
|
list.addEventListener('transitionend', transitionEndBound)
|
|
|
|
});
|
2017-08-07 04:08:03 +08:00
|
|
|
}, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
click(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.isOpen ? this.close() : this.open();
|
|
|
|
this.isOpen = !this.isOpen;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-10 05:17:35 +08:00
|
|
|
export default ChapterToggle;
|