$anchorScroll in Ionic 2? Scroll to element with ID

丶灬走出姿态 提交于 2019-12-07 06:59:27

I was in the same situation, where I wanted to scroll to the next page/element for each click of a button.

I looked at the same options you mentioned, and I found that in ion-slides (http://ionicframework.com/docs/v2/api/components/slides/Slides/), you can set the direction to 'vertical', which did the trick for me.

Don't know if it can help your use case.

Otherwise, you can look into Content, and the scrollTo function. http://ionicframework.com/docs/v2/api/components/content/Content/

I don't know if you are still facing this issue, but this can be solved it using YourHTMLelement.getBoundingClientRect(), which returns the bounds of that element as top, right, bottom and left.

With that, you can use content and scrollTo(left, top, duration) function. I created a function to scroll vertically to a specific element, which looks like:

scrollToElement(id) { 
    var el = document.getElementById(id);
    var rect = el.getBoundingClientRect();
    // scrollLeft as 0px, scrollTop as "topBound"px, move in 800 milliseconds
    this.content.scrollTo(0, rect.top, 800);
}

But you can change it the way you want to also scroll horizontally, with the scrollLeft parameter.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!