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
Infinite scroll not working #1685
Comments
You should ensure the scroll container height < scroll content height. |
@webfansplz Thanks for your reply. :) Yeah, but still it doesn't seem to work. I'm just using the example(s), but nothing gets triggered. |
Can you provide a mini repo ,thanks |
I tried no problem, this is my code: <script setup lang="ts">
import { ref, toRefs } from 'vue'
import { useInfiniteScroll } from '@vueuse/core'
const el = ref<HTMLElement>(null)
const data = ref([1,2,3,4,5,6])
useInfiniteScroll(
el,
() => {
for(let i = 0; i<=10;i++) {
data.value.push(Math.random())
}
},
{ distance: 10 }
)
</script>
<template>
<div ref="el" style="height: 100px;overflow: scroll;">
<div v-for="item in data">
{{ item }}
</div>
</div>
</template> |
@murongg Thanks, I'll try to add @webfansplz I'll try to add a mini repo later this week, ty. |
@murongg @webfansplz I can confirm it works on Tailwind using The root cause is Would it be possible to force an infinite scroll, like let's say a Reddit/YT feed (focussed on the body instead)? Thanks. |
I would think that infinite scroll should at least fire once, even if the content has less height than the scroll container (IMO it being a configuration option would be best). In my case, just ensuring the parent is smaller doesn't really work well with my design. On desktop, there are no issues as the design has a small height and is center on the screen, but when I switch to mobile, the container in question stretches the screen, and thus if I don't load enough data in the first go, I can't load additional data. I could just fetch more data when on mobile but that seems... counter intuitive. |
My hacky solution for now is once I fetch data, I check if the container still isn't scrollable, it'll make another call (assuming there is still more data to fetch), e.g: // if this fetch was triggered by a scroll event, don't trigger it a second time.
if (wasScrollEvent) return
setTimeout(() => {
if (scrollContainer.value.scrollHeight <= scrollContainer.value.clientHeight) {
fetchEvents(true)
}
}, 500) Full logic: const scrollContainer = ref(null) // attached to infinite scroll container.
function fetchEvents(wasScrollEvent) {
if (!hasNextPage.value) return
events.executeQuery().then((result) => {
const data = result.data.value.githubevents
if (data.pageInfo.hasNextPage) {
cursor.value = data.pageInfo.endCursor
} else {
hasNextPage.value = false
}
fetched.value = [...fetched.value, ...data.edges.map(({ node }) => toRaw(node))]
// if this fetch was triggered by a scroll event, don't trigger it a second time.
if (wasScrollEvent) return
setTimeout(() => {
if (scrollContainer.value.scrollHeight <= scrollContainer.value.clientHeight) {
fetchEvents(true)
}
}, 500)
})
}
onMounted(() => {
fetchEvents()
}) |
I've come up with my own solution, using VueScroll:
|
I accomplished this using 'useIntersectionObserver'
|
@adgower Do you mind telling me how I could get your example code to work in Nuxt3 with the following logic? For some reaason it's not loading the products correctly when using fetch:
|
Thanks @adgower. Below is a change that fixes this unexpected behavior. FYI @dosstx
Resources: |
Describe the bug
I'm trying to use infinite scroll, however the provided examples don't seem to work.
Reproduction
https://stackblitz.com/edit/vitejs-vite-srqqu3
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: