Refactor site-nav animation: drop Velocity

This commit is contained in:
Mimi 2020-07-02 11:36:06 +08:00
parent 9781cf0181
commit 659347cc1e
2 changed files with 41 additions and 13 deletions

View File

@ -1,4 +1,4 @@
/* global NexT, CONFIG, Velocity */
/* global NexT, CONFIG */
NexT.boot = {};
@ -12,18 +12,35 @@ NexT.boot.registerEvents = function() {
event.currentTarget.classList.toggle('toggle-close');
const siteNav = document.querySelector('.site-nav');
if (!siteNav) return;
const animateAction = siteNav.classList.contains('site-nav-on') ? 'slideUp' : 'slideDown';
if (typeof Velocity === 'function') {
Velocity(siteNav, animateAction, {
duration: 200,
complete: function() {
siteNav.classList.toggle('site-nav-on');
}
});
} else {
siteNav.classList.toggle('site-nav-on');
}
const animateAction = siteNav.classList.contains('site-nav-on');
const height = NexT.utils.getComputedStyle(siteNav);
siteNav.style.height = animateAction ? height : 0;
const toggle = () => siteNav.classList.toggle('site-nav-on');
const begin = () => {
siteNav.style.overflow = 'hidden';
};
const complete = () => {
siteNav.style.overflow = '';
siteNav.style.height = '';
};
window.anime(Object.assign({
targets : siteNav,
duration: 200,
height : animateAction ? [height, 0] : [0, height],
easing : 'linear'
}, animateAction ? {
begin,
complete: () => {
complete();
toggle();
}
} : {
begin: () => {
begin();
toggle();
},
complete
}));
});
const TAB_ANIMATE_DURATION = 200;

View File

@ -281,6 +281,17 @@ NexT.utils = {
return isFirefoxWithPDFJS || (supportsPdfMimeType && !isIOS);
},
getComputedStyle: function(element) {
const clone = element.cloneNode(true);
clone.style.position = 'absolute';
clone.style.visibility = 'hidden';
clone.style.display = 'block';
element.parentNode.appendChild(clone);
const height = clone.clientHeight;
element.parentNode.removeChild(clone);
return height;
},
/**
* Init Sidebar & TOC inner dimensions on all pages and for all schemes.
* Need for Sidebar/TOC inner scrolling if content taller then viewport.