Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add reactive window.scrollTo watcher to useWindowScroll #2394

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/core/useWindowScroll/demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
import { useWindowScroll } from '@vueuse/core'

const { x, y } = useWindowScroll()

function scrollToTop() {
x.value = 0
y.value = 0
}
</script>

<template>
Expand All @@ -15,7 +20,11 @@ const { x, y } = useWindowScroll()
Scroll value
</note>
x: {{ x }}<br>
y: {{ y }}
y: {{ y }}<br>

<button @click="scrollToTop">
Scroll Up
</button>
</div>
</div>
</template>
Expand Down
7 changes: 6 additions & 1 deletion packages/core/useWindowScroll/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ref } from 'vue-demi'
import { ref, watch } from 'vue-demi'
import { useEventListener } from '../useEventListener'
import type { ConfigurableWindow } from '../_configurable'
import { defaultWindow } from '../_configurable'
Expand All @@ -20,6 +20,11 @@ export function useWindowScroll({ window = defaultWindow }: ConfigurableWindow =
const x = ref(window.pageXOffset)
const y = ref(window.pageYOffset)

watch([x, y], ([newX, newY]) => {
if (window)
window.scrollTo(newX, newY)
})

useEventListener(
'scroll',
() => {
Expand Down