码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-10-27 17:10:00      阅读:532      评论:0      收藏:0      [点我收藏+]

标签:mep   card   image   win   index   items   rom   space   fse   

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 水平滚动 + 点击元素滚动到某元素

标签:mep   card   image   win   index   items   rom   space   fse   

原文地址:https://www.cnblogs.com/haoaixue2006/p/9862247.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!