@@ -6,7 +6,6 @@ import ScrollHelper from './utils/scroll-helper';
66const $searchInput = $ ( '#search-input' ) ;
77const delta = ScrollHelper . getTopbarHeight ( ) ;
88
9- let didScroll ;
109let lastScrollTop = 0 ;
1110
1211function hasScrolled ( ) {
@@ -60,34 +59,41 @@ function handleLandscape() {
6059
6160export function switchTopbar ( ) {
6261 const orientation = screen . orientation ;
63- if ( orientation ) {
64- orientation . onchange = ( ) => {
65- const type = orientation . type ;
66- if ( type === 'landscape-primary' || type === 'landscape-secondary' ) {
67- handleLandscape ( ) ;
68- }
69- } ;
70- } else {
71- // for the browsers that not support `window.screen.orientation` API
72- $ ( window ) . on ( 'orientationchange' , ( ) => {
73- if ( $ ( window ) . width ( ) < $ ( window ) . height ( ) ) {
74- // before rotating, it is still in portrait mode.
75- handleLandscape ( ) ;
76- }
77- } ) ;
78- }
62+ let didScroll = false ;
7963
80- $ ( window ) . on ( 'scroll' , ( ) => {
81- if ( didScroll ) {
82- return ;
64+ const handleOrientationChange = ( ) => {
65+ const type = orientation . type ;
66+ if ( type === 'landscape-primary' || type === 'landscape-secondary' ) {
67+ handleLandscape ( ) ;
68+ }
69+ } ;
70+
71+ const handleWindowChange = ( ) => {
72+ if ( $ ( window ) . width ( ) < $ ( window ) . height ( ) ) {
73+ // before rotating, it is still in portrait mode.
74+ handleLandscape ( ) ;
8375 }
76+ } ;
77+
78+ const handleScroll = ( ) => {
8479 didScroll = true ;
85- } ) ;
80+ } ;
8681
87- setInterval ( ( ) => {
82+ const checkScroll = ( ) => {
8883 if ( didScroll ) {
8984 hasScrolled ( ) ;
9085 didScroll = false ;
9186 }
92- } , 250 ) ;
87+ } ;
88+
89+ if ( orientation ) {
90+ orientation . addEventListener ( 'change' , handleOrientationChange ) ;
91+ } else {
92+ // for the browsers that not support `window.screen.orientation` API
93+ $ ( window ) . on ( 'orientationchange' , handleWindowChange ) ;
94+ }
95+
96+ $ ( window ) . on ( 'scroll' , handleScroll ) ;
97+
98+ setInterval ( checkScroll , 250 ) ;
9399}
0 commit comments