欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

touch.js 拖动、缩放、旋转 (鼠标手势)功能代码

程序员文章站 2022-06-14 08:36:54
可以实现手势操作:拖动、缩放、旋转。封装好的脚本方法是这样的: var cat = window.cat || {}; cat.touchjs = {...

可以实现手势操作:拖动、缩放、旋转。封装好的脚本方法是这样的:

var cat = window.cat || {}; 
cat.touchjs = { 
  left: 0, 
  top: 0, 
  scaleval: 1,  //缩放 
  rotateval: 0,  //旋转 
  curstatus: 0,  //记录当前手势的状态, 0:拖动, 1:缩放, 2:旋转 
  //初始化 
  init: function ($targetobj, callback) { 
    touch.on($targetobj, 'touchstart', function (ev) { 
      cat.touchjs.curstatus = 0; 
      ev.preventdefault();//阻止默认事件 
    }); 
    if (!window.localstorage.cat_touchjs_data) 
      callback(0, 0, 1, 0); 
    else { 
      var jsonobj = json.parse(window.localstorage.cat_touchjs_data); 
      cat.touchjs.left = parsefloat(jsonobj.left), cat.touchjs.top = parsefloat(jsonobj.top), cat.touchjs.scaleval = parsefloat(jsonobj.scale), cat.touchjs.rotateval = parsefloat(jsonobj.rotate); 
      callback(cat.touchjs.left, cat.touchjs.top, cat.touchjs.scaleval, cat.touchjs.rotateval); 
    } 
  }, 
  //拖动 
  drag: function ($targetobj, callback) { 
    touch.on($targetobj, 'drag', function (ev) { 
      $targetobj.css("left", cat.touchjs.left + ev.x).css("top", cat.touchjs.top + ev.y); 
    }); 
    touch.on($targetobj, 'dragend', function (ev) { 
      cat.touchjs.left = cat.touchjs.left + ev.x; 
      cat.touchjs.top = cat.touchjs.top + ev.y; 
      callback(cat.touchjs.left, cat.touchjs.top); 
    }); 
  }, 
  //缩放 
  scale: function ($targetobj, callback) { 
    var initialscale = cat.touchjs.scaleval || 1; 
    var currentscale; 
    touch.on($targetobj, 'pinch', function (ev) { 
      if (cat.touchjs.curstatus == 2) { 
        return; 
      } 
      cat.touchjs.curstatus = 1; 
      currentscale = ev.scale - 1; 
      currentscale = initialscale + currentscale; 
      cat.touchjs.scaleval = currentscale; 
      var transformstyle = 'scale(' + cat.touchjs.scaleval + ') rotate(' + cat.touchjs.rotateval + 'deg)'; 
      $targetobj.css("transform", transformstyle).css("-webkit-transform", transformstyle); 
      callback(cat.touchjs.scaleval); 
    }); 
    touch.on($targetobj, 'pinchend', function (ev) { 
      if (cat.touchjs.curstatus == 2) { 
        return; 
      } 
      initialscale = currentscale; 
      cat.touchjs.scaleval = currentscale; 
      callback(cat.touchjs.scaleval); 
    }); 
  }, 
  //旋转 
  rotate: function ($targetobj, callback) { 
    var angle = cat.touchjs.rotateval || 0; 
    touch.on($targetobj, 'rotate', function (ev) { 
      if (cat.touchjs.curstatus == 1) { 
        return; 
      } 
      cat.touchjs.curstatus = 2; 
      var totalangle = angle + ev.rotation; 
      if (ev.fingerstatus === 'end') { 
        angle = angle + ev.rotation; 
      } 
      cat.touchjs.rotateval = totalangle; 
      var transformstyle = 'scale(' + cat.touchjs.scaleval + ') rotate(' + cat.touchjs.rotateval + 'deg)'; 
      $targetobj.css("transform", transformstyle).css("-webkit-transform", transformstyle); 
      $targetobj.attr('data-rotate', cat.touchjs.rotateval); 
      callback(cat.touchjs.rotateval); 
    }); 
  } 
}; 

html代码:

<div style="position:relative;width: 100%;height: 250px;overflow: hidden;border: 1px dashed #ff0000;"> 
 <img id="targetobj" style="position:relative;transform-origin:center" src="http://demo.somethingwhat.com/images/flower1.jpg" /> 
</div>

 js调用:

var $targetobj = $('#targetobj'); 
//初始化设置 
cat.touchjs.init($targetobj, function (left, top, scale, rotate) {}; 
//初始化拖动手势(不需要就注释掉) 
cat.touchjs.drag($targetobj, function (left, top) { }); 
//初始化缩放手势(不需要就注释掉) 
cat.touchjs.scale($targetobj, function (scale) { }); 
//初始化旋转手势(不需要就注释掉) 
cat.touchjs.rotate($targetobj, function (rotate) { }); 

以上所述是小编给大家介绍的touch.js 拖动、缩放、旋转 (鼠标手势)功能代码,希望对大家有所帮助