JSON

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

字号+ 作者:H5之家 来源:H5之家 2017-02-12 09:01 我要评论( )

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

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

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 【JS学习】JS基础2

    【JS学习】JS基础2

    2017-01-26 16:02

  • 微信小程序学习

    微信小程序学习

    2017-01-26 08:00

  • python 学习(json)(转)

    python 学习(json)(转)

    2017-01-14 16:00

  • AngularJs学习笔记(二)

    AngularJs学习笔记(二)

    2016-10-24 16:00

网友点评