Dragging HTML element , implemented with HammerJS , is jittery on touch device

拈花ヽ惹草 提交于 2019-12-10 23:36:37

问题


I have a list of HTML elements as cards stacked on top of each other. I am trying to drag the element with pan event using HammerJS

export class HomePage {
  @ViewChildren('slides') slides;
  @ViewChild('stack') stack;
  constructor(    
    this.cards = [1,2,3,4,5]
  }
  ngAfterViewInit(){
    let hammer = new Hammer.Manager(this.stack.nativeElement, {  preventDefault: true,
      recognizers: [      [ Hammer.Pan, { threshold: 2 }] ]
    });
    hammer.get('pan').set({ direction: window['Hammer'].DIRECTION_ALL });
    this.element = this.slides.first.nativeElement;
    hammer.on('panmove', (ev) => {
      this.handlePan(ev);
    });
  }
  handlePan(
    let deltaX = ev.deltaX;
    let deltaY = ev.deltaY;
    this.renderer.setStyle(this.element, 'transform',`translate(${deltaX}px,${deltaY}px)`);
  }
}

home.html

<ion-content  >
  <div #stack class="stack">
  <ion-card *ngFor = "let c of cards" #slides >
    <ion-card-content>
      c
    </ion-card-content>
  </ion-card>
  </div>
</ion-content>

home.scss

  ion-card{
    position: absolute;
    height: 400px;
  }

This works smoothly in browser but on touch devices its jittery. But if I remove position: absolute from css it works smoothly on touch devices as well but the cards are not stacked on top of each other. I just feel thepanmove event is triggered little late on touch devices for some reason.I am stuck on this problem for some days, any kind of help in figuring out the problem would be appreciated.


回答1:


Finally found the answer. The issue is that styling is pretty slow on mobile devices. So have to make GPU acceleration in mobile devices. Simply add translate3d(0,0,0) which will pull in GPU acceleration.

this.renderer.setStyle(elem,"transform",`translate3d(0, 0, 0) translate(${xtranslate}px,${ytranslate}px)`)


来源:https://stackoverflow.com/questions/51233705/dragging-html-element-implemented-with-hammerjs-is-jittery-on-touch-device

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