移动端检测用户是否复制过一些文字的功能实现.
首先直接检测复制事件似乎是不可能的,因为没有复制这个事件,所以只能检测用户是否选中过或在这些文字上长按过的操作,因为选中或长按后最可能的操作就是复制啦html>
标题! function(a, b) {
"user strict";
a.jiance = {
_xy: [0, 0],
init: function(id, callback) {
var obj = document.getElementById(id);
obj.addEventListener('touchstart', function(event) {
if (event.targetTouches.length == 1) {
//设置如果长按两秒
jiance.timeid = setTimeout(function() {
//准备进行复制........
typeof callback == 'function' && callback(touch, obj);
}, 2000);
event.preventDefault();
var touch = event.targetTouches[0];
jiance._xy[0] = touch.pageX;
jiance._xy[1] = touch.pageY;
}
}, false);
obj.addEventListener('touchmove', function(event) {
if (event.targetTouches.length == 1) {
clearTimeout(jiance.timeid);
event.preventDefault();
var touch = event.targetTouches[0];
//移动超过10个像素判断为选中文字
if ((Math.abs(touch.pageX - jiance._xy[0])) > 10) {
//准备进行复制........
typeof callback == 'function' && callback(touch, obj);
}
}
}, false);
obj.addEventListener('touchend', function(event) {
clearTimeout(jiance.timeid);
jiance._xy[0] = 0;
jiance._xy[1] = 0;
}, false);
}
}
}(window);
/**
* 使用方法
* @param {[type]} touch [description]
* @param {[type]} dom) { var val [description]
* @return {[type]} [description]
*/
jiance.init('weixin', function(touch, dom) {
alert('copye');
var val = dom.value;
dom.setSelectionRange(0, val.length);
dom.focus();
});