-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds autoScrollBack behavior to Mobile Web Safari
- Loading branch information
1 parent
c95c2dd
commit e04c6a7
Showing
5 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import smoothscroll from 'smoothscroll-polyfill'; | ||
|
||
import getBrowser from '../getBrowser'; | ||
|
||
const init = () => { | ||
if (getBrowser() === 'safari') { | ||
const userAgent = navigator.userAgent.toLowerCase(); | ||
|
||
if (userAgent.indexOf('iphone')) { | ||
let top = 0; | ||
let innerHeightTooSmall = 0; | ||
|
||
if (userAgent.indexOf('iphone os 15_') > 0) { // iOS 15 | ||
if (window.outerHeight === 812) { // iPhone Pro | ||
top = 280; | ||
innerHeightTooSmall = 350; | ||
} else if (window.outerHeight === 896) { | ||
if (window.devicePixelRatio === 3) { // iPhone Pro Max | ||
top = 290; | ||
innerHeightTooSmall = 425; | ||
} else { // iPhone | ||
top = 245; | ||
innerHeightTooSmall = 470; | ||
} | ||
} | ||
} else if (userAgent.indexOf('iphone os 1') > 0) { // iOS Before 15 | ||
if (window.outerHeight === 812) { // iPhone Pro | ||
top = 280; | ||
innerHeightTooSmall = 350; | ||
} else if (window.outerHeight === 896) { | ||
if (window.devicePixelRatio === 3) { // iPhone Pro Max | ||
top = 290; | ||
innerHeightTooSmall = 425; | ||
} else { // iPhone | ||
top = 300; | ||
innerHeightTooSmall = 420; | ||
} | ||
} | ||
} | ||
|
||
if (!top || !innerHeightTooSmall) { | ||
return; | ||
} | ||
|
||
// const innerHeightTooSmall = 470; | ||
let isTouching = false; | ||
let timeout; | ||
smoothscroll.polyfill(); | ||
|
||
const scrollBack = () => { | ||
window.requestAnimationFrame(() => { | ||
window.scrollTo({ | ||
top, | ||
behavior: 'smooth', | ||
}); | ||
}); | ||
}; | ||
|
||
const clearTimeoutIfNeeded = () => { | ||
if (timeout) { | ||
clearTimeout(timeout); | ||
timeout = undefined; | ||
} | ||
}; | ||
|
||
const scheduleScrollback = () => { | ||
clearTimeoutIfNeeded(); | ||
|
||
if (!isTouching && innerHeightTooSmall > window.innerHeight) { | ||
timeout = setTimeout(scrollBack, 34); | ||
} | ||
}; | ||
|
||
document.addEventListener('touchstart', () => { | ||
isTouching = true; | ||
}); | ||
document.addEventListener('touchend', () => { | ||
isTouching = false; | ||
scheduleScrollback(); | ||
}); | ||
document.addEventListener('scroll', () => { | ||
scheduleScrollback(); | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
init(); |