欢迎来到代码驿站!

JavaScript代码

当前位置:首页 > 网页前端 > JavaScript代码

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

时间:2022-05-06 09:20:31|栏目:JavaScript代码|点击:

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

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) { }); 

上一篇:js中的for如何实现foreach中的遍历

栏    目:JavaScript代码

下一篇:JS打印组合功能

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

本文地址:http://www.codeinn.net/misctech/201098.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有