Skip to content

Commit

Permalink
⚡ Debounce resize events
Browse files Browse the repository at this point in the history
  • Loading branch information
ujibang committed Sep 12, 2024
1 parent 20f583d commit 370d8ec
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions _includes/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@
document.documentElement.style.setProperty('--vh', `${(w.height || w.innerHeight).toFixed(2) * 0.01}px`);
// console.debug('set css variable --vh =', document.documentElement.style.getPropertyValue('--vh'));

console.log(w)

w.addEventListener('resize', () => {
w.addEventListener('resize', debounce(() => {
const _vh = ((w.height || w.innerHeight) * 0.01).toFixed(2);

if (lastVH === _vh) {
Expand All @@ -76,6 +74,14 @@
document.documentElement.style.setProperty('--vh', `${_vh}px`);
window.scrollTo({top:0, left:0,behavior: 'instant'});
// console.debug('set css variable --vh =', _vh);
})
}), timeout = 100)

function debounce(func, timeout = 300) {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => { func.apply(this, args); }, timeout);
};
}
</script>
</head>

0 comments on commit 370d8ec

Please sign in to comment.