ionic3 水平滚动 + 点击元素滚动到某元素

Posted haoaixue2006

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ionic3 水平滚动 + 点击元素滚动到某元素相关的知识,希望对你有一定的参考价值。

html

<ion-row>
        <ion-item no-lines>
          <ion-scroll  #card scrollX="true" class="ion-scrall-style" >
            <div #item (click)="onTap(avatar)" *ngFor="let avatar of avatars" class="scroll-item">
                <img src="{{avatar}}"/>
              </div>
          </ion-scroll>
        </ion-item>
      </ion-row>

 

CSS:

 .ion-scrall-style{
        white-space: nowrap;
        min-height: 200px;
        // height: 120px;
        overflow: hidden;
    
        .scroll-item {
          display: inline-block;
          background-color: #e74c3c;
          // height: auto;
          margin: 10px;
        }
      }

 

隐藏滚动条:

   /* Hide ion-content scrollbar */
      ::-webkit-scrollbar{
        display:none;
      }

 

Component

import { Component,AfterViewInit ,ViewChild, ViewChildren, ElementRef,QueryList} from ‘@angular/core‘;
import { NavController, Scroll } from ‘ionic-angular‘;
import { Content } from ‘ionic-angular‘;


@Component({
  selector: ‘page-home‘,

  templateUrl: ‘home.html‘
})
export class HomePage implements AfterViewInit{

    @ViewChild(‘card‘) card: any ;
    // @ViewChild(‘item‘) tiem: ElementRef ;
    @ViewChildren(‘item‘) items: QueryList<any>;
    @ViewChild(‘cardContent‘) cardContent: any;

    avatars:Array<string> = ["http://image.prntscr.com/image/15f7d1b8dca94296b249f56eb6cc78d3.png", 
    "http://image.prntscr.com/image/f2b0ac9e43334eddac9c1af05e573888.png",
     "http://image.prntscr.com/image/6915d39cf813481fa3c19fa292c582ba.png" ,
     "http://image.prntscr.com/image/ad357d428faf4e88ab3bdac78782b523.png",
      "http://image.prntscr.com/image/7e98362d62b2490c998fe1472dcb0601.png" ,
       ];
    
  constructor(public navCtrl: NavController)  {

  }

  ngAfterViewInit() {
   
  }


onTap(src: string) {
    // console.log(avatar);
    let index = this.avatars.indexOf(src);
    let element = this.items.toArray()[index];
    this.card._scrollContent.nativeElement.scrollLeft = element.nativeElement.offsetLeft;
}

}

 

以上是关于ionic3 水平滚动 + 点击元素滚动到某元素的主要内容,如果未能解决你的问题,请参考以下文章

采纳追加100分,求CSS,JS,鼠标滚动到某位置,元素出现?

CSS 如何添加水平滚动条

Ionic 3 水平滚动卡片

使 html 表格元素响应水平滚动

CSS div 元素 - 如何只显示水平滚动条?

滚动到水平div中的元素