使用JavaScript(而不是HTML5)拖放?
我正在使用HTML5制作一个离线混合android应用程序 无论如何,HTML5的拖放功能在android上还不受支持,所以我想使用其他方法使用JavaScript(而不是HTML5)拖放?,javascript,drag-and-drop,Javascript,Drag And Drop,我正在使用HTML5制作一个离线混合android应用程序 无论如何,HTML5的拖放功能在android上还不受支持,所以我想使用其他方法 使用JavaScript实现拖放的其他方式有哪些?您可以使用jquery ui touch punch或移动拖放实现移动中的拖放 它们可以分别在以下链接中找到 您可以使用jquery ui触摸冲孔或移动拖放功能实现移动拖放 它们可以分别在以下链接中找到 使用JavaScript运行拖放操作的基本方法是在浏览器中附加各种鼠标/触摸事件,而无需任何形式的额
使用JavaScript实现拖放的其他方式有哪些?您可以使用
jquery ui touch punch
或移动拖放
实现移动中的拖放
它们可以分别在以下链接中找到
您可以使用
jquery ui触摸冲孔
或移动拖放功能实现移动拖放
它们可以分别在以下链接中找到
使用JavaScript运行拖放操作的基本方法是在浏览器中附加各种鼠标/触摸事件,而无需任何形式的额外库或内置HTML5功能
- 鼠标向下/触摸启动
触发mousedown/touchstart时,您可以克隆已单击的元素(或移动现有元素),并将其设置为绝对位置,以便在页面中移动它。当拖动开始时,您将设置对正在拖动的元素的引用,以便您的移动事件可以跟踪当前正在移动的内容。一旦触发此事件,并且您有了对元素的引用,您就可以将mousemove/touchmove事件附加到文档中,并开始侦听实际的移动
- 鼠标移动/触摸移动
触发mousemove/touchmove事件时,可以使用事件位置在屏幕上移动元素。为了获得最佳性能,您可以将mousemove/touchmove事件附加到整个文档,而不仅仅是一个元素,否则会遇到鼠标移动速度快于更新元素速度的问题
- 鼠标/触摸端
最后,当mouseup/touchend被触发时,您可以使用最终位置来计算落点。您还可以释放mousemove/touchmove事件和对正在拖动的元素的任何引用
使用JQuery管理触摸事件的快速示例,可以在以下位置进行测试:
var目标=null;
$('Dragables li')。on('touchstart',函数(e){
var target=$(e.currentTarget).clone();
target.addClass('drawing');
//在添加到dom之前运行初始对齐
css('left',(e.originalEvent.touchs[0].clientX-(target.width()/2));
css('top',(e.originalEvent.touchs[0].clientY-(target.height()/2));
$('body')。追加(目标);
$(文档).on('touchmove',函数(e){
如果(目标!=null){
//移动目标
css('left',(e.originalEvent.touchs[0].clientX-(target.width()/2));
css('top',(e.originalEvent.touchs[0].clientY-(target.height()/2));
}
});
$(文档).on('touchend',函数(e){
如果(目标!=null){
var x=(target.offset().left+(target.width()/2));
变量y=(target.offset().top+(target.height()/2));
//计算是否在dropzone内
var offset=$('#dropzone')。offset();
如果(x>offset.left&&x<(offset.left+$('#dropzone').width())和&y>offset.top&&y<(offset.top+$('#dropzone').height()){
var drop=target.clone();
drop.removeClass('drawing');
$('#dropzone')。追加(已删除);
}
target.remove();
$(文档).off('touchmove');
目标。关闭(“修补”);
target=null;
}
});
});
使用JavaScript运行拖放操作的基础,不需要任何形式的额外库或内置HTML5功能,即附加到浏览器中的各种鼠标/触摸事件
- 鼠标向下/触摸启动
触发mousedown/touchstart时,您可以克隆已单击的元素(或移动现有元素),并将其设置为绝对位置,以便在页面中移动它。当拖动开始时,您将设置对正在拖动的元素的引用,以便您的移动事件可以跟踪当前正在移动的内容。一旦触发此事件,并且您有了对元素的引用,您就可以将mousemove/touchmove事件附加到文档中,并开始侦听实际的移动
- 鼠标移动/触摸移动
触发mousemove/touchmove事件时,可以使用事件位置在屏幕上移动元素。为了获得最佳性能,您可以将mousemove/touchmove事件附加到整个文档,而不仅仅是一个元素,否则会遇到鼠标移动速度快于更新元素速度的问题
- 鼠标/触摸端
最后,当mouseup/touchend被触发时,您可以使用最终位置来计算落点。您还可以释放mousemove/touchmove事件和对正在拖动的元素的任何引用
使用JQuery管理触摸事件的快速示例,可以在以下位置进行测试:
var目标=null;
$('Dragables li')。on('touchstart',函数(e){
var target=$(e.currentTarget).clone();
target.addClass('drawing');
//在添加到dom之前运行初始对齐
css('left',(e.originalEvent.touchs[0].clientX-(target.width()/2));
css('top',(e.originalEvent.touchs[0].clientY-(target.height()/2));
$('body')。追加(目标);
$(文档).on('touchmove',函数(e){
如果(目标!=null){
//移动目标
css('left',(e.originalEvent.touchs[0].clientX-(target.width()/2));
css('top',(e.originalEvent.touchs[0].clientY-(target.height()/2));
}
});
var target = null;
$('#draggables li').on('touchstart', function(e) {
var target = $(e.currentTarget).clone();
target.addClass('dragging');
// run an intial alignment before adding to the dom
target.css('left', (e.originalEvent.touches[0].clientX - (target.width() / 2)));
target.css('top', (e.originalEvent.touches[0].clientY - (target.height() / 2)));
$('body').append(target);
$(document).on('touchmove', function(e) {
if (target != null) {
// move the target
target.css('left', (e.originalEvent.touches[0].clientX - (target.width() / 2)));
target.css('top', (e.originalEvent.touches[0].clientY - (target.height() / 2)));
}
});
$(document).on('touchend', function(e) {
if (target != null) {
var x = (target.offset().left + (target.width() / 2));
var y = (target.offset().top + (target.height() / 2));
// calculate if were inside of the dropzone
var offset = $('#dropzone').offset();
if (x > offset.left && x < (offset.left + $('#dropzone').width()) && y > offset.top && y < (offset.top + $('#dropzone').height())) {
var dropped = target.clone();
dropped.removeClass('dragging');
$('#dropzone').append(dropped);
}
target.remove();
$(document).off('touchmove');
target.off('touchup');
target = null;
}
});
});