-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: add throttle for scroll and resize listener
- Loading branch information
Showing
6 changed files
with
46 additions
and
9 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 |
---|---|---|
@@ -1,7 +1,10 @@ | ||
const backToTop = document.getElementById('back-to-top'); | ||
|
||
if (backToTop !== null) { | ||
window.addEventListener('scroll', function() { | ||
window.scrollY > 100 ? backToTop.classList.add('show') : backToTop.classList.remove('show'); | ||
}); | ||
window.addEventListener( | ||
'scroll', | ||
throttle(function() { | ||
window.scrollY > 100 ? backToTop.classList.add('show') : backToTop.classList.remove('show'); | ||
}, delayTime) | ||
); | ||
} |
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,24 @@ | ||
// https://www.30secondsofcode.org/js/s/throttle/ | ||
|
||
const throttle = (fn, wait) => { | ||
let inThrottle, lastFn, lastTime; | ||
return function() { | ||
const context = this, | ||
args = arguments; | ||
if (!inThrottle) { | ||
fn.apply(context, args); | ||
lastTime = Date.now(); | ||
inThrottle = true; | ||
} else { | ||
clearTimeout(lastFn); | ||
lastFn = setTimeout(function() { | ||
if (Date.now() - lastTime >= wait) { | ||
fn.apply(context, args); | ||
lastTime = Date.now(); | ||
} | ||
}, Math.max(wait - (Date.now() - lastTime), 0)); | ||
} | ||
}; | ||
}; | ||
|
||
const delayTime = 420; |
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
2 changes: 1 addition & 1 deletion
2
...leSite/resources/_gen/assets/scss/scss/main.scss_b04c21e44835e7dcdc8cb03d103b5ef1.content
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
exampleSite/resources/_gen/assets/scss/scss/main.scss_b04c21e44835e7dcdc8cb03d103b5ef1.json
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 |
---|---|---|
@@ -1 +1 @@ | ||
{"Target":"css/meme.min.fee27c7492b717ad2200531e61fca428c2153b6479073d07250c50c33b5c2604.css","MediaType":"text/css","Data":{"Integrity":"sha256-/uJ8dJK3F60iAFMeYfykKMIVO2R5Bz0HJQxQwztcJgQ="}} | ||
{"Target":"css/meme.min.becf25eee993457d079cf9cb1933804875c3ac89960278e671d97ba4930e4ce6.css","MediaType":"text/css","Data":{"Integrity":"sha256-vs8l7umTRX0HnPnLGTOASHXDrImWAnjmcdl7pJMOTOY="}} |