$.extend({
getTransform: function() {
var prefix = ["", "-webkit-", "-moz-", "-o-", "-ms-"];
var style = document.createElement("div").style;
var transform = "",
i = 0,
len = prefix.length;
for (; i < len; i++) {
let t = prefix[i] + "transform";
if (t in style) {
return (transform = t);
}
}
return transform;
},
getControlPos: function(e) {
let pos = null;
if (/mouse/.test(e.type)) {
return (pos = {
x: e.clientX,
y: e.clientY
});
} else if (/touch/.test(e.type)) {
return (pos = {
x: e.pageX,
y: e.pageY
});
}
return pos;
}
});
$.fn.extend({
getTargetPos: function() {
var pos = {
x: 0,
y: 0
};
var el = this[0];
var transform = $.getTransform();
if (transform !== "") {
var transformValue = this.css("transform");
if (transformValue === "none") {
el.style[transform] = "translate(0, 0)";
return pos;
} else {
var tmp = transformValue.match(/-?\d+/g);
return (pos = {
x: parseInt(tmp[4].trim()),
y: parseInt(tmp[5].trim())
});
}
} else {
var positionValue = this.css("position");
if (positionValue === "static") {
el.style["position"] = "relative";
return pos;
} else {
return (pos = {
x: parseInt(this.css("left")),
y: parseInt(this.css("top"))
});
}
}
},
setTargetPos: function(pos) {
var edge = this.edge;
var transform = $.getTransform();
if (pos.x <= edge.xmin) {
pos.x = edge.xmin;
}
if (pos.x >= edge.xmax) {
pos.x = edge.xmax;
}
if (pos.y <= edge.ymin) {
pos.y = edge.ymin;
}
if (pos.y >= edge.ymax) {
pos.y = edge.ymax;
}
if (transform) {
this.css('transform', `translate(${pos.x}px, ${pos.y}px)`);
return 1;
} else {
this.css({
'left': pos.x + "px",
'top': pos.y + "px"
});
return 1;
}
return 0;
},
drag: function(options) {
var offset = this.offset();
var defaults = {
xmax: document.documentElement.clientWidth - offset["width"] - 1,
ymax: document.documentElement.clientHeight - offset["height"] - 1,
xmin: 0,
ymin: 0
};
this.edge = options || defaults;
var MOBILE = "ontouchstart" in window;
var target = this;
var el = this[0];
var state = {
moving: false,
startX: 0,
startY: 0,
sourceX: 0,
sourceY: 0
};
var setTargetState = function() {
var targetPos = target.getTargetPos();
state.sourceX = targetPos.x;
state.sourceY = targetPos.y;
};
var move = function(e) {
if (!state.moving) {
return;
}
var { x, y } = target.getControlPos(e),
dx = x - state.startX,
dy = y - state.startY;
target.setTargetPos({
x: (state.sourceX + dx).toFixed(),
y: (state.sourceY + dy).toFixed()
});
};
var end = function() {
setTargetState(el);
state.moving = false;
if (!MOBILE) {
$(document).unbind("mousemove", move);
$(document).unbind("mouseup", end);
} else {
$(document).unbind("touchmove", move);
$(document).unbind("touchend", end);
}
console.log("move end");
};
var start = function() {
var controlPos = target.getControlPos(e);
setTargetState(el);
state.moving = true;
state.startX = controlPos.x;
state.startY = controlPos.y;
if (!MOBILE) {
$(document).bind("mousemove", move);
$(document).bind("mouseup", end);
} else {
$(document).bind("touchmove", move);
$(document).bind("touchend", end);
}
};
!MOBILE ? target.on("mousedown", start) : target.on("touchstart", start);
}
});