admin管理员组文章数量:1424467
I would like to calculate the current scroll percentage of my IonContent (it's an Ionic + React app)
The page layout looks like this:
<IonPage>
<IonHeader>
<IonToolbar>
<IonButtons slot="start">
<IonMenuButton />
</IonButtons>
<IonTitle>Page</IonTitle>
</IonToolbar>
</IonHeader>
<IonContentid="main-content"
scrollEvents={true}
onIonScroll={(ev) => {
// ToDo: calculate scroll percentage
console.log(ev.detail.scrollTop);
}}>
{// very long custom ponent
}
<IonFooter>
<IonLabel>Footer </IonLabel>
</IonFooter>
</IonContent>
</IonPage>
From the IonScroll event, I can read out the scrollTop which seems to be the current scroll position.
Now I need to get the maximal scrolling height of the IonContent.
Related questions are:
- finding the maximum scroll position of a page
- Cross-Browser Method to Determine Vertical Scroll Percentage in Javascript
- how to calculate height of scroll content
The accepted answer from the first question seems to provide the most plete approach, by taking the max over a selection of scrolling height values:
var limit = Math.max( document.body.scrollHeight, document.body.offsetHeight,
document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight );
I've extended this by also adding:
document.scrollingElement.scrollHeight
The problem:
In my scenario, the maximum over these values is around 660. But the logging output from ev.detail.scrollTop
goes up to 680.76
.
I've connected the debug inspector and tried scrolling from the console, to see what would happen for scroll value around 660. I can still see the content move when scrolling from 660 to 680:
document.getElementById('main-content').scrollToPoint(0, 680);
How can I find the maximum scrolling coordinates?
I would like to calculate the current scroll percentage of my IonContent (it's an Ionic + React app)
The page layout looks like this:
<IonPage>
<IonHeader>
<IonToolbar>
<IonButtons slot="start">
<IonMenuButton />
</IonButtons>
<IonTitle>Page</IonTitle>
</IonToolbar>
</IonHeader>
<IonContentid="main-content"
scrollEvents={true}
onIonScroll={(ev) => {
// ToDo: calculate scroll percentage
console.log(ev.detail.scrollTop);
}}>
{// very long custom ponent
}
<IonFooter>
<IonLabel>Footer </IonLabel>
</IonFooter>
</IonContent>
</IonPage>
From the IonScroll event, I can read out the scrollTop which seems to be the current scroll position.
Now I need to get the maximal scrolling height of the IonContent.
Related questions are:
- finding the maximum scroll position of a page
- Cross-Browser Method to Determine Vertical Scroll Percentage in Javascript
- how to calculate height of scroll content
The accepted answer from the first question seems to provide the most plete approach, by taking the max over a selection of scrolling height values:
var limit = Math.max( document.body.scrollHeight, document.body.offsetHeight,
document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight );
I've extended this by also adding:
document.scrollingElement.scrollHeight
The problem:
In my scenario, the maximum over these values is around 660. But the logging output from ev.detail.scrollTop
goes up to 680.76
.
I've connected the debug inspector and tried scrolling from the console, to see what would happen for scroll value around 660. I can still see the content move when scrolling from 660 to 680:
document.getElementById('main-content').scrollToPoint(0, 680);
How can I find the maximum scrolling coordinates?
Share Improve this question asked May 19, 2020 at 8:46 lhklhk 30.4k36 gold badges137 silver badges220 bronze badges1 Answer
Reset to default 6I think I found the solution:
onIonScroll={async (ev) => {
const elem = document.getElementById("ion-content-id");
// the ion content has its own associated scrollElement
const scrollElement = await ( elem as any).getScrollElement()
const scrollPosition = ev.detail.scrollTop;
const totalContentHeight = scrollElement.scrollHeight;
const viewportHeight = elem.offsetHeight;
const percentage = scrollPosition / (totalContentHeight - viewportHeight);
console.log(percentage);
}}
I don't understand why this inpatibility was introduced though. Wouldn't it be possible to connect the scrollElement
of the IonContent
with document.scrollingElement
? The standard is still a draft but implemented in all major browsers (expect IE).
本文标签: javascripthow to get maximum scroll height for ion contentStack Overflow
版权声明:本文标题:javascript - how to get maximum scroll height for ion content? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745428670a2658229.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论