Code of the touch. js drag, zoom, and rotate (mouse gesture) Function

Source: Internet
Author: User
This article mainly introduces touch. js drag, zoom, rotate (mouse gesture) function, the code is easy to understand, very good, has reference value, friends who need can refer to the following to achieve gesture operations: drag, zoom, and rotate. The encapsulated script method is as follows:

Var cat = window. cat | {}; cat. touchjs = {left: 0, top: 0, scaleVal: 1, // scale rotateVal: 0, // rotate curStatus: 0, // record the status of the current gesture, 0: drag, 1: Zoom, 2: Rotate // initialize init: function ($ targetObj, callback) {touch. on ($ targetObj, 'touchstart', function (ev) {cat. touchjs. curStatus = 0; ev. preventDefault (); // block default events}); 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) {export targetobj.css ("left", cat. touchjs. left + ev.x2.16.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) '; Define targetobj.css ("transform", transformstyleapps.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) '; Define targetobj.css ("transform", transformstyleapps.css ("-webkit-transform", transformStyle); $ targetObj. attr ('data-rotate', cat. touchjs. rotateVal); callback (cat. touchjs. rotateVal );});}};

Html code:

Js call:

Var $ targetObj = $ ('# targetObj'); // initialize cat. touchjs. init ($ targetObj, function (left, top, scale, rotate) {}; // initialize the drag gesture (comment out if not required) cat. touchjs. drag ($ targetObj, function (left, top) {}); // initialize the scaling gesture (comment out if not needed) cat. touchjs. scale ($ targetObj, function (scale) {}); // initialize the rotation gesture (comment out if not needed) cat. touchjs. rotate ($ targetObj, function (rotate ){});

The above is a small series of touch. js drag, zoom, rotate (mouse gesture) function code, hope to help you, if you have any questions, please leave a message, xiaobian will reply to you in time. I would like to thank you for your support for PHP chinnet!

For more articles about the code of the touch. js drag, zoom, and rotate (mouse gesture) function, please follow PHP's Chinese website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.