-
-
Notifications
You must be signed in to change notification settings - Fork 64
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
useOnScroll throwing errors #684
Comments
Can you provide your template code? Are you assigning |
@pikax ah, ok, I was applying the template ref into a library component. Adding it to a native element cleared the errors. However, it's not working yet. No scrolling happens. The documentation is quite thin on how to actually use it. I'm trying to programmatically scroll the viewport up to a component, and hopefully doing a smooth scroll? |
I need to do some work on the documentation. The way it work is: <template>
<div ref="tableElementRef">
<!-- ... -->
</div>
</template>
<script>
export default {
setup(){
const tableElementRef: Ref<HTMLElement | null> = ref(null);
const onClick = async function onClick () {
tableElementRef.value.scrollTo({ top: tableElementRef.value.scrollTop } );
};
return { tableElementRef, onClick };
}
}
</script> useOnScroll is intended to be a wrapper for the HTMLElement.
Currently calling
I haven't tried with library component, I think in v2 should be straight forward but in v3 with multiple root elements might be a bit tricky |
@pikax sorry but I still could not figure out how to use this - could you post a full setup code example on how to scroll top to a specific element? |
If you provide code somewhere where I can have a look, I can help you better. |
@pikax here's basically my stripped-down version. scrollTop.value is giving 0 and scrollTo method is not doing anything? <script lang="ts">
import { defineComponent, ref, Ref } from '@vue/composition-api';
import { useOnScroll } from 'vue-composable';
export default defineComponent({
setup: () => {
const titleRef: Ref<HTMLElement | null> = ref(null);
const { scrollTo, scrollTop } = useOnScroll(titleRef);
const scrollToElementTop = function scrollToElementTop() {
console.debug('scrollToElementTop', titleRef.value);
if (titleRef.value) {
console.debug('scrollToElementTop - scrolling...', scrollTop.value);
// scrollTop.value is giving 0 and scrollTo method is not doing anything
scrollTo({
top: scrollTop.value,
});
}
};
return {
titleRef,
scrollToElementTop,
};
},
});
</script>
<template>
<div style="padding-top: 300px">
<h2 ref="titleRef">Title</h2>
<div style="height: 500px;"></div>
<b-button @click="scrollToElementTop()">Scroll</b-button>
</div>
</template> |
Logging of |
Using |
Seems you're using it incorrectly, definitely something the docs are not very clear. The element passed to the At the least in your example, the element who scrolls is the window document, in your case you can change the code to: <script lang="ts">
import { defineComponent, ref, Ref } from '@vue/composition-api';
import { useOnScroll } from 'vue-composable';
export default defineComponent({
setup: () => {
const titleRef: Ref<HTMLElement | null> = ref(null);
const { scrollTo, scrollTop } = useOnScroll(); // use the document scrollable
const scrollToElementTop = function scrollToElementTop() {
console.debug('scrollToElementTop', titleRef.value);
if (titleRef.value) {
console.debug('scrollToElementTop - scrolling...', scrollTop.value);
// scrollTop.value is giving 0 and scrollTo method is not doing anything
scrollTo({
top: titleRef.value.offsetTop, // passing the title offset to top
});
}
};
return {
titleRef,
scrollToElementTop,
};
},
});
</script>
<template>
<div style="padding-top: 300px">
<h2 ref="titleRef">Title</h2>
<div style="height: 500px;"></div>
<button @click="scrollToElementTop()">Scroll</button>
</div>
</template> |
@pikax got it working using vue-scrollTo library. Needed to use
How would this be done in practice? |
scrollTo({
top: titleRef.value.offsetTop, // passing the title offset to top
behavior: 'smooth'
}); |
@pikax thanks, smooth scrolling works now. However I'm still having problem scrolling to the ref'fed element, as |
Have you tried to use setup: () => {
const titleRef: Ref<HTMLElement | null> = ref(null);
const { scrollIntoView } = useOnScroll(titleRef); // use the document scrollable
const scrollToElementTop = function scrollToElementTop() {
console.debug('scrollToElementTop', titleRef.value);
if (titleRef.value) {
scrollIntoView();
}
};
return {
titleRef,
scrollToElementTop,
};
}, |
@pikax how to define scroll container if titleRef is given as the param for useOnScroll() instead of scroll container? |
if you want to scroll to that component, you probably can you scrollIntoView if you provide an example, I might to be able more, but so far it seems |
@pikax I mean I got this working only by specifying the scroll container by passing it to |
@pikax can |
The text was updated successfully, but these errors were encountered: