前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >移动端缩放解决方案 hammerjs

移动端缩放解决方案 hammerjs

作者头像
lilugirl
发布2019-07-02 16:59:34
2.6K0
发布2019-07-02 16:59:34
举报
文章被收录于专栏:前端导学前端导学

本方案适合各种前端成熟框架 以 ionic3为例子

第一步 安装 hammerjs

代码语言:javascript
复制
npm install hammerjs

第二步 写核心缩放功能

代码语言:javascript
复制
import * as Hammer from 'hammerjs';

// 缩放功能
export class PinchZoom {

  static hammerIt(elmid: string) {

    const elm = document.getElementById(elmid);

    console.log('hammerIt begin elm', elm);
    const hammertime = new Hammer(elm, { touchAction: 'pan-x pan-y' });  // 兼容x轴和y轴方向的滚动


    hammertime.get('pinch').set({
      enable: true
    });

    console.log('hammertime', hammertime);

    let posX = 0,
      posY = 0,
      scale = 1,
      last_scale = 1,
      last_posX = 0,
      last_posY = 0,
      max_pos_x = 0,
      max_pos_y = 0,
      transform = '';
    const el = elm;


    hammertime.on('panleft panright panup pandown tap press pdoubletap pan pinch panend pinchend', function (ev) {

      console.log(ev.type + ' gesture detected.');

      if (ev.type === 'doubletap') {
        transform =
          'translate3d(0, 0, 0) ' +
          'scale3d(2, 2, 1) ';
        scale = 2;
        last_scale = 2;
        try {
          if (window.getComputedStyle(el, null).getPropertyValue('-webkit-transform').toString() !== 'matrix(1, 0, 0, 1, 0, 0)') {
            transform =
              'translate3d(0, 0, 0) ' +
              'scale3d(1, 1, 1) ';
            scale = 1;
            last_scale = 1;
          }
        } catch (err) { }
        // tslint:disable-next-line: deprecation
        el.style.webkitTransform = transform;
        transform = '';
      }

      // pan
      if (scale !== 1) {
        posX = last_posX + ev.deltaX;
        posY = last_posY + ev.deltaY;
        max_pos_x = Math.ceil((scale - 1) * el.clientWidth / 2);
        max_pos_y = Math.ceil((scale - 1) * el.clientHeight / 2);
        if (posX > max_pos_x) {
          posX = max_pos_x;
        }
        if (posX < -max_pos_x) {
          posX = -max_pos_x;
        }
        if (posY > max_pos_y) {
          posY = max_pos_y;
        }
        if (posY < -max_pos_y) {
          posY = -max_pos_y;
        }
      }


      // pinch
      if (ev.type === 'pinch') {
        scale = Math.max(.999, Math.min(last_scale * (ev.scale), 4));
      }
      if (ev.type === 'pinchend') { last_scale = scale; }

      // panend
      if (ev.type === 'panend') {
        last_posX = posX < max_pos_x ? posX : max_pos_x;
        last_posY = posY < max_pos_y ? posY : max_pos_y;
      }

      if (scale !== 1) {
        transform =
          'translate3d(' + posX + 'px,' + posY + 'px, 0) ' +
          'scale3d(' + scale + ', ' + scale + ', 1)';
      }

      if (transform) {
        // tslint:disable-next-line: deprecation
        el.style.webkitTransform = transform;
      }
    });
  }
}

第三步 具体使用

代码语言:javascript
复制
// html文件
// 定义个缩放区域

  <div id="pinchzoom">
    <div>
      <img src="你的图片地址" >
    </div>
    <div>放大缩小</div>
  </div>
代码语言:javascript
复制
// ts文件

...
import { PinchZoom } from 'xxxx';   // 引入手写的缩放功能类

...

  // 启动缩放功能
  ngOnInit() {
    const elm = document.getElementById('pinchzoom');
    PinchZoom.hammerIt(elm);
  }

本方案参考自 https://stackoverflow.com/questions/18011099/pinch-to-zoom-using-hammer-js

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 第一步 安装 hammerjs
  • 第二步 写核心缩放功能
  • 第三步 具体使用
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档