refactor(js): reduce function complexity
This commit is contained in:
parent
c283e7782f
commit
1967de1f1d
1 changed files with 30 additions and 24 deletions
|
@ -6,7 +6,6 @@ import ScrollHelper from './utils/scroll-helper';
|
||||||
const $searchInput = $('#search-input');
|
const $searchInput = $('#search-input');
|
||||||
const delta = ScrollHelper.getTopbarHeight();
|
const delta = ScrollHelper.getTopbarHeight();
|
||||||
|
|
||||||
let didScroll;
|
|
||||||
let lastScrollTop = 0;
|
let lastScrollTop = 0;
|
||||||
|
|
||||||
function hasScrolled() {
|
function hasScrolled() {
|
||||||
|
@ -60,34 +59,41 @@ function handleLandscape() {
|
||||||
|
|
||||||
export function switchTopbar() {
|
export function switchTopbar() {
|
||||||
const orientation = screen.orientation;
|
const orientation = screen.orientation;
|
||||||
if (orientation) {
|
let didScroll = false;
|
||||||
orientation.onchange = () => {
|
|
||||||
|
const handleOrientationChange = () => {
|
||||||
const type = orientation.type;
|
const type = orientation.type;
|
||||||
if (type === 'landscape-primary' || type === 'landscape-secondary') {
|
if (type === 'landscape-primary' || type === 'landscape-secondary') {
|
||||||
handleLandscape();
|
handleLandscape();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} else {
|
|
||||||
// for the browsers that not support `window.screen.orientation` API
|
const handleWindowChange = () => {
|
||||||
$(window).on('orientationchange', () => {
|
|
||||||
if ($(window).width() < $(window).height()) {
|
if ($(window).width() < $(window).height()) {
|
||||||
// before rotating, it is still in portrait mode.
|
// before rotating, it is still in portrait mode.
|
||||||
handleLandscape();
|
handleLandscape();
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
}
|
|
||||||
|
|
||||||
$(window).on('scroll', () => {
|
const handleScroll = () => {
|
||||||
if (didScroll) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
didScroll = true;
|
didScroll = true;
|
||||||
});
|
};
|
||||||
|
|
||||||
setInterval(() => {
|
const checkScroll = () => {
|
||||||
if (didScroll) {
|
if (didScroll) {
|
||||||
hasScrolled();
|
hasScrolled();
|
||||||
didScroll = false;
|
didScroll = false;
|
||||||
}
|
}
|
||||||
}, 250);
|
};
|
||||||
|
|
||||||
|
if (orientation) {
|
||||||
|
orientation.addEventListener('change', handleOrientationChange);
|
||||||
|
} else {
|
||||||
|
// for the browsers that not support `window.screen.orientation` API
|
||||||
|
$(window).on('orientationchange', handleWindowChange);
|
||||||
|
}
|
||||||
|
|
||||||
|
$(window).on('scroll', handleScroll);
|
||||||
|
|
||||||
|
setInterval(checkScroll, 250);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue