Tôi đang cố gắng để thực hiện một chức năng kéo-như sử dụng các mô hình tiếp theo:cảm ứng Move sự kiện không sa thải sau khi chạm vào Bắt đầu mục tiêu được lấy ra
- Theo dõi Marker kiện Pointer Down.
- Khi sự kiện Xuống kích hoạt đăng ký các sự kiện Di chuyển và Di chuyển cửa sổ trên Màn hình và xóa điểm đánh dấu.
- Thực hiện một số thao tác trong khi Di chuyển.
- Khi sự kiện Up kích hoạt hủy đăng ký khỏi Di chuyển và Lên.
Điều này phù hợp với sự kiện chuột nhưng không hoạt động đối với sự kiện Chạm. Chúng không cháy sau khi phần tử mục tiêu Touch Start bị xóa. Tôi đã cố gắng sử dụng Pointer Events Polyfill nhưng nó không hoạt động.
Tôi đang sử dụng Công cụ Chrome Dev để mô phỏng sự kiện chạm. Xem mẫu:
initTestBlock('mouse', {
start: 'mousedown',
move: 'mousemove',
end: 'mouseup'
});
initTestBlock('touch', {
start: 'touchstart',
move: 'touchmove',
end: 'touchend'
});
initTestBlock('touch-no-remove', {
start: 'touchstart',
move: 'touchmove',
end: 'touchend'
}, true);
function initTestBlock(id, events, noRemove) {
var block = document.getElementById(id);
var parent = block.querySelector('.parent');
var target = block.querySelector('.target');
target.addEventListener(events.start, function(e) {
console.log(e.type);
if (!noRemove) {
setTimeout(function() {
// Remove target
target.parentElement.removeChild(target);
}, 1000);
}
function onMove(e) {
console.log(e.type);
var pt = getCoords(e);
parent.style.left = pt.x + 'px';
parent.style.top = pt.y + 'px';
}
function onEnd(e) {
console.log(e.type);
window.removeEventListener(events.move, onMove);
window.removeEventListener(events.end, onEnd);
}
window.addEventListener(events.move, onMove);
window.addEventListener(events.end, onEnd);
});
}
// Returns pointer coordinates
function getCoords(e) {
if (e instanceof TouchEvent) {
return {
x: e.touches[0].pageX,
y: e.touches[0].pageY
};
}
return {
x: e.pageX,
y: e.pageY
};
}
window.addEventListener('selectstart', function() {
return false;
}, true);
.parent {
background: darkred;
color: white;
width: 10em;
height: 10em;
position: absolute;
}
.target {
background: orange;
width: 4em;
height: 4em;
}
#mouse .parent {
left: 0em;
}
#touch .parent {
left: 11em;
}
#touch-no-remove .parent {
left: 22em;
}
<div id="mouse">
<div class="parent">Mouse events
<div class="target">Drag here</div>
</div>
</div>
<div id="touch">
<div class="parent">Touch events
<div class="target">Drag here</div>
</div>
</div>
<div id="touch-no-remove">
<div class="parent">Touch (no remove)
<div class="target">Drag here</div>
</div>
</div>
Tôi gặp sự cố liên quan. Bạn đã tìm thấy một giải pháp trong khi đó? –
@SebastianvomMeer Xem câu trả lời của tôi http://stackoverflow.com/a/34980275/4137472 –