Skip to content

Commit

Permalink
Merge pull request #431 from ismail9k/340-carousel-flickers-when-resi…
Browse files Browse the repository at this point in the history
…zing-it

340 carousel flickers when resizing it
  • Loading branch information
ismail9k authored Nov 27, 2024
2 parents 93df008 + 02216b3 commit 10f4a19
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
8 changes: 1 addition & 7 deletions src/components/Carousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export default defineComponent({
updateBreakpointsConfig()
updateSlidesData()
updateSlideSize()
}, 16)
})

/**
* Setup functions
Expand Down Expand Up @@ -168,8 +168,6 @@ export default defineComponent({
updateBreakpointsConfig()
initAutoplay()

window.addEventListener('resize', handleResize, { passive: true })

resizeObserver = new ResizeObserver(handleResize)
if (root.value) {
resizeObserver.observe(root.value)
Expand All @@ -189,10 +187,6 @@ export default defineComponent({
resizeObserver.unobserve(root.value)
resizeObserver = null
}

window.removeEventListener('resize', handleResize, {
passive: true,
} as EventListenerOptions)
})

/**
Expand Down
23 changes: 12 additions & 11 deletions src/utils/debounce.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/**
* return a debounced version of the function
* @param fn
* @param delay
* Returns a debounced version of the function using requestAnimationFrame.
*
* @param fn - The function to debounce.
*/
// eslint-disable-next-line no-unused-vars
export function debounce(fn: (...args: any[]) => unknown, delay: number): typeof fn {
let timerId: ReturnType<typeof setTimeout> | null
export function debounce(fn: (...args: any[]) => unknown): (...args: any[]) => void {
let frameId: number | null = null

return function (...args: any[]) {
if (timerId) {
clearTimeout(timerId)
if (frameId) {
cancelAnimationFrame(frameId)
}
timerId = setTimeout(() => {

frameId = requestAnimationFrame(() => {
fn(...args)
timerId = null
}, delay)
frameId = null
})
}
}
24 changes: 12 additions & 12 deletions src/utils/throttle.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/**
* return a throttle version of the function
* Throttling
* Returns a throttled version of the function using requestAnimationFrame.
*
* @param fn - The function to throttle.
*/
// eslint-disable-next-line no-unused-vars
export function throttle(fn: (...args: any[]) => unknown): typeof fn {
let isRunning = false
export function throttle(fn: (...args: any[]) => unknown): (...args: any[]) => void {
let isThrottled = false

return function (...args: any[]) {
if (!isRunning) {
isRunning = true
requestAnimationFrame(() => {
fn.apply(this, args)
isRunning = false
})
}
if (isThrottled) return

isThrottled = true
requestAnimationFrame(() => {
fn(...args)
isThrottled = false
})
}
}

0 comments on commit 10f4a19

Please sign in to comment.