大佬教程收集整理的这篇文章主要介绍了[译] 在 Angular 中使用 HammerJS (触摸手势),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
|- app/ |- app.component.html |- app.component.css |- app.component.ts |- app.module.ts |- main.ts |- index.html |- tsconfig.json@H_607_0@APP 组件 @H_607_0@让我们从 app 组件开始,在这个组件里,我们将定义头像列表和处理头像显示和隐藏的 swipe 事件.
// app/app.component.ts import { Component } from '@angular/core'; @Component({ modulEID: module.id,SELEctor: 'my-app',templateUrl: 'app.component.html',styleUrls: ['app.component.css'] }) export class AppComponent { // constant for swipe action: left or right SWIPE_ACTION = { LEFT: 'swipeleft',RIGHT: 'swiperight' }; // our list of avatars avatars = [ { name: 'kristy',image: 'http://semantic-ui.com/images/avatar2/large/kristy.png',visible: true },{ name: 'matthew',image: 'http://semantic-ui.com/images/avatar2/large/matthew.png',visible: false },{ name: 'chris',image: 'http://semantic-ui.com/images/avatar/large/chris.jpg',{ name: 'jenny',image: 'http://semantic-ui.com/images/avatar/large/jenny.jpg',visible: false } ]; // action triggered when user swipes swipe(currenTindex: number,action = this.SWIPE_ACTION.RIGHT) { // out of range if (currenTindex > this.avatars.length || currenTindex < 0) return; let nexTindex = 0; // swipe right,next avatar if (action === this.SWIPE_ACTION.RIGHT) { const isLast = currenTindex === this.avatars.length - 1; nexTindex = isLast ? 0 : currenTindex + 1; } // swipe left,prevIoUs avatar if (action === this.SWIPE_ACTION.LEFT) { const isFirst = currenTindex === 0; nexTindex = isFirst ? this.avatars.length - 1 : currenTindex - 1; } // toggle avatar visibility this.avatars.forEach((x,i) => x.visible = (i === nexTindeX)); } }@H_607_0@笔记:
<!-- app/app.component.html --> <div> <h4>Swipe Avatars with HAMMerJS</h4> <!-- loop each avatar in our avatar list --> <div class="swipe-Box" *ngFor="let avatar of avatars; let idx=index" (swipeleft)="swipe(idx,$event.typE)" (swiperight)="swipe(idx,$event.typE)" [class.visible]="avatar.visible" [class.hidden]="!avatar.visible"> <div> <img [src]="avatar.image" [alt]="avatar.name"> </div> <div> <a class="header">{{avatar.namE}}</a> </div> </div> </div>@H_607_0@笔记:
.swipe-Box { display: block; width: 100%; float: left; margin: 0; } .visible { display: block; } .hidden { display: none; }@H_607_0@笔记: @H_607_0@引入 HAMMerJS 脚本 @H_607_0@现在已经完成了组件,开始设置 HAMMerJS,首先,需要把 HAMMerJS 脚本文件引入到主视图文件 index.html 文件中
<!-- index.html --> <head> ... <!-- HAMMer JS --> <script src="https://cdnjs.cloudFlare.com/ajax/libs/hAMMer.js/2.0.8/hAMMer.js"></script> .... </head>@H_607_0@默认情况下,桌面浏览器是不支持 touch 事件,HAMMerJS 有一个扩展叫 touch-emulator.js,提供一个调试工具来模拟实现 touch 支持,你可以像引入 hAMMer.js 那样引入它
<!-- index.html --> <head> ... <!-- HAMMer JS Touch Emulator: Uncomment if for desktop --> <script src="http://cdn.rawgit.com/hAMMerjs/touchemulator/master/touch-emulator.js"></script> <script> TouchEmulator(); </script> ... </head>@H_607_0@具体是如何模拟实现,可以查看 hAMMer.js 的官方文档 @H_607_0@由于示例运行在 plunker,示例中引用了 HAMMerJS CDN URI,如果想本地管理,可以运行下面的命令.
npm install hAMMerjs --save@H_607_0@然后,在项目中引入JS 文件 @H_607_0@如果没有引入 HAMMerJS file,会有一个错误信息抛出: 'HAMMer.js is not loaded,can not bind swipeleft event'. @H_607_0@应用程序模块 @H_607_0@默认情况,如果没有任何自定义配置,就可以直接使用 HAMMerJS,Angular 支持 HAMMerJS 开箱即用,在应用启动时,不需要包含任何内容,您的应用程序模块看起来像下面这样
// app.module.ts import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; @NgModule({ imports: [ BrowserModule ],declarations: [ AppComponent ],bootstrap: [ AppComponent ],providers: [ ] }) export class AppModule { }@H_607_0@定制 HAMMerJS @H_607_0@如果你想增加一些自定义设置,像速度和阀值什么的,要怎么做呢? @H_607_0@快速说明
// app.module.ts import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { HAMMerGestureConfig,HAMMER_GEstuRE_CONfig } from '@angular/platform-browser'; import { AppComponent } from './app.component'; export class MyHAMMerConfig extends HAMMerGestureConfig { overrides = <any>{ 'swipe': {veLocity: 0.4,threshold: 20} // override default setTings } } @NgModule({ imports: [ BrowserModule ],providers: [ { provide: HAMMER_GEstuRE_CONfig,useClass: MyHAMMerConfig } ] // use our custom hAMMerjs config }) export class AppModule { }@H_607_0@在示例中,仅是想为 swipe 行为重写一些默认配置,如果想有更多的控制,可以像上面那样实现 HAMMerGestureConfig 类 @H_607_0@查看一下 HAMMerGestureConfig 没那么复杂的源码或者文档,整个类仅有两个属性( events,overrides) 和一个方法( buildHAMMer ) @H_607_0@总结: @H_607_0@Angular 与 HAMMerJS 集成以实现触摸手势事件检测非常容易.
以上是大佬教程为你收集整理的[译] 在 Angular 中使用 HammerJS (触摸手势)全部内容,希望文章能够帮你解决[译] 在 Angular 中使用 HammerJS (触摸手势)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。