简体   繁体   English

Ionic v5 滚动到顶部

[英]Ionic v5 scroll to top

Current environment:
    Ionic CLI                     : 6.7.0
    Ionic Framework               : @ionic/angular 5.1.0
    @angular-devkit/build-angular : 0.803.26
    @angular-devkit/schematics    : 8.3.26
    @angular/cli                  : 8.3.26
    @ionic/angular-toolkit        : 2.2.0

The documentation for ionic v5 has left me clueless. ionic v5 的文档让我一无所知。 I've yet to overcome the learning curve I suppose.我还没有克服我想的学习曲线。

How can I create a scroll to top button with a floating action button?如何使用浮动操作按钮创建滚动到顶部按钮? I've been on the Content Docs page to try and find the information I need.我一直在Content Docs页面上尝试查找我需要的信息。 So I'm thinking that I need to get a reference to the content in the typescript file.所以我想我需要参考 typescript 文件中的内容。 I'm accustomed to JS and in that case it would be as simple as document.getElementById() ... Here not so much.我已经习惯了 JS,在这种情况下,它就像document.getElementById()一样简单……这里就不多说了。 A quick google search brings me to V3 Framework docs .快速谷歌搜索将我带到V3 框架文档 It explicitly states To get a reference to the content component from a Page's logic, you can use Angular's @ViewChild annotation: Which is absolutely wonderful.它明确指出To get a reference to the content component from a Page's logic, you can use Angular's @ViewChild annotation:这绝对是美妙的。 Thank you.谢谢你。 However I'm now on V5, not V3, and they are different enough that everything breaks.但是我现在使用的是 V5,而不是 V3,它们的不同之处足以让一切都崩溃。

So I'm like okay, maybe I'm just an idiot and can't do anything to save my life.所以我很好,也许我只是个白痴,无法做任何事来挽救我的生命。 Scrolls down to methods getScrollElement and scrollToTop Cooooool... I got nothing from this doc.向下滚动到方法getScrollElementscrollToTop Coooooool...我从这个文档中什么也没得到。

To summarize, I have two questions.总结一下,我有两个问题。 How can I do this in v5, or could you show me to a doc that outlines it in a better way?我怎样才能在 v5 中做到这一点,或者你能告诉我一个以更好的方式概述它的文档吗? Am I crazy?我疯了吗? Maybe it's an angular problem and not an ionic problem.也许这是一个 angular 问题而不是离子问题。

This is an ionic challenge.这是一个离子挑战。

You have two options你有两个选择

1) Properly implement scroll to top using the standard ionic way as detailed in the framework documentation forum. 1)使用框架文档论坛中详述的标准离子方式正确实现滚动到顶部。 I include that link for context, but this direct link out of the question may be more useful.我包含该链接作为上下文,但这个直接链接可能更有用。

2) You can find your element and programmatically scroll it. 2)您可以找到您的元素并以编程方式滚动它。

document.querySelector('app-your-component-name').querySelector('ion-content').scrollToTop();

NOTE: If you are capable of posting what you have tried so far, it would help in giving you a more accurate answer.注意:如果您能够发布到目前为止所尝试的内容,这将有助于为您提供更准确的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM