-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): custom animation for CSS Mode where smooth scrolling is n…
…ot supported
- Loading branch information
1 parent
a533958
commit a61da6a
Showing
4 changed files
with
55 additions
and
10 deletions.
There are no files selected for viewing
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,34 @@ | ||
import { getWindow } from 'ssr-window'; | ||
|
||
export default function animateCSSModeScroll({ swiper, targetPosition, side }) { | ||
const window = getWindow(); | ||
const currentPosition = -swiper.translate; | ||
const frameTime = 1000 / 60; | ||
const frames = swiper.params.speed / frameTime; | ||
const perFrame = (targetPosition - currentPosition) / frames; | ||
|
||
let progressPosition = currentPosition; | ||
swiper.wrapperEl.style.scrollSnapType = 'none'; | ||
window.cancelAnimationFrame(swiper.cssModeFrameID); | ||
|
||
const dir = targetPosition > currentPosition ? 'next' : 'prev'; | ||
|
||
const isOutOfBound = (progress, target) => { | ||
return (dir === 'next' && progress >= target) || (dir === 'prev' && progress <= target); | ||
}; | ||
|
||
const animate = () => { | ||
progressPosition += perFrame; | ||
if (isOutOfBound(progressPosition, targetPosition)) progressPosition = targetPosition; | ||
swiper.wrapperEl.scrollTo({ | ||
[side]: progressPosition, | ||
}); | ||
if (isOutOfBound(progressPosition, targetPosition)) { | ||
swiper.wrapperEl.style.scrollSnapType = ''; | ||
window.cancelAnimationFrame(swiper.cssModeFrameID); | ||
return; | ||
} | ||
swiper.cssModeFrameID = window.requestAnimationFrame(animate); | ||
}; | ||
animate(); | ||
} |
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
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