如何在ionic2中使用content.scrollTo()进行离子滚动? [英] How can I use content.scrollTo() for ion-scroll in ionic2?

查看:155
本文介绍了如何在ionic2中使用content.scrollTo()进行离子滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试滚动到固定位置,例如scrollTo(500,20)。假设您使用的设备宽度为300像素。滚动目标现在已超出屏幕范围,您必须向右滚动。

I try to scroll to a fixed position, for example scrollTo(500, 20). Let's say that you are on a device, which has got a width of 300 pixel. The scroll target is now out of the screen scope, you have to scroll to the right.

我通过执行以下操作解决了这个问题:

I solved this by doing the following:

<ion-content>
    <ion-scroll scrollX="true" style="width:100%; height:100%; white-space: nowrap; ">
        <div style="width:1000px; ">
            [box1] [box2] [box3] [...]
        </div>
    </ion-scroll>
</ion-content>

到此为止一切都很好。如果我想通过按下按钮向右跳500像素,问题就开始了。向右滑动即可。我知道有一个功能可以为< ion-content> 执行此操作:

Up to here everything is fine. The problem starts if i want to jump 500 pixel to the right by pressing a button for example. Swiping to the right works. I know that there is a function to do this for <ion-content>:

@ViewChild(Content) content: Content;
[...]
this.content.scrollTo(500, 500, 500); 

遗憾的是,这不适用于我的情况。我认为问题是内容与设备大小有关,因此scrollTo()方法不会对< ion-scoll> 产生影响。如何使用scrollTo()方法< ion-scroll> 而不是< ion-content>

This unfortunately doesn't work in my case. I think the problem is that the content is related to the device size so the scrollTo() method does not take affect for <ion-scoll>. How can I use the scrollTo() method for <ion-scroll> instead of <ion-content>?

感谢您的帮助!

推荐答案


如何使用scrollTo()方法< ion-scroll> 而不是
< ion-content>

我还在研究如何为滚动设置动画,但至少这可能被认为是适合您的方案的解决方案。请查看此plunker

I'm still working on how to animate the scroll, but at least this may be considered as a solution for your scenario. Please take a look at this plunker.

由于我们不能使用 ion-content 进行滚动,我虽然要获取Scroll的实例,然后访问内部html滚动元素,然后使用 element.scrollLeft 属性滚动该元素:

Since we can't use theion-content for scrolling, I though about getting the instance of the Scroll, then accessing the inner html scroll element, and then using the element.scrollLeft property to scroll on that element:


element.scrollLeft属性获取或设置元素内容滚动到左边的
的像素数。 / p>

The element.scrollLeft property gets or sets the number of pixels that an element's content is scrolled to the left.

所以在组件代码中:

import { Component, ViewChild } from '@angular/core';
import { NavController, Content } from 'ionic-angular';

@Component({...})
export class HomePage {
  @ViewChild('scroll') scroll: any;

    constructor() {}

    public backToStart(): void {
      this.scroll.scrollElement.scrollLeft = 0;
    }

    public scrollToRight(): void {
      this.scroll.scrollElement.scrollLeft = 500;
    }

}

在视图中:

<ion-content padding>
  <ion-scroll #scroll scrollX="true" style="width:100%; height:150px; white-space: nowrap;">
        <div  style="width:1000px;">
            <div style="display:inline-block;height:100px;width:100px;border:1px solid black;"></div>
            <div style="display:inline-block;height:100px;width:100px;border:1px solid red;"></div>
            <div style="display:inline-block;height:100px;width:100px;border:1px solid blue;"></div>
            <div style="display:inline-block;height:100px;width:100px;border:1px solid green;"></div>
            <div style="display:inline-block;height:100px;width:100px;border:1px solid grey;"></div>
            <div style="display:inline-block;height:100px;width:100px;border:1px solid brown;"></div>
            <div style="display:inline-block;height:100px;width:100px;border:1px solid yellow;"></div>
            <div style="display:inline-block;height:100px;width:100px;border:1px solid orange;"></div>
            <div style="display:inline-block;height:100px;width:100px;border:1px solid pink;"></div>
            <div style="display:inline-block;height:100px;width:100px;border:1px solid violet;"></div>
        </div>
    </ion-scroll>

    <button (click)="backToStart()" ion-button text-only>Go to start</button>
    <button (click)="scrollToRight()" ion-button text-only>Scroll to right!</button>
</ion-content>

通过 this.scroll.scrollElement.scrollLeft = 500; 我们可以将离子滚动500px的内容滚动到右侧。然后,我们可以通过 this.scroll.scrollElement.scrollLeft = 0; 。再次回到起点。

By doing this.scroll.scrollElement.scrollLeft = 500;, we can scroll the content of the ion-scroll 500px to the right. We can then go back to the start again by doing this.scroll.scrollElement.scrollLeft = 0;.

这篇关于如何在ionic2中使用content.scrollTo()进行离子滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆