Merge pull request #99 from nicofrand/mobile-drag

Make overlay dragging work on mobile devices
This commit is contained in:
Bram 2018-05-12 04:37:03 +02:00 committed by GitHub
commit 09432a3c99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -122,33 +122,53 @@ var dragg = function(id) {
this.x_pos = 0, this.y_pos = 0, // Stores x & y coordinates of the mouse pointer this.x_pos = 0, this.y_pos = 0, // Stores x & y coordinates of the mouse pointer
this.x_elem = 0, this.y_elem = 0; // Stores top, left values (edge) of the element this.x_elem = 0, this.y_elem = 0; // Stores top, left values (edge) of the element
// Start dragging var _initDrag = function(e){
window.addEvent(elem, 'mousedown', function(e){
// Prevent firefox native D'n'D behavior // Prevent firefox native D'n'D behavior
window.eventPreventDefault(e); window.eventPreventDefault(e);
if (e.type === "touchstart"){
x_pos = e.touches[0].clientX;
y_pos = e.touches[0].clientY;
}
selected = elem; selected = elem;
x_elem = x_pos - selected.offsetLeft; x_elem = x_pos - selected.offsetLeft;
y_elem = y_pos - selected.offsetTop; y_elem = y_pos - selected.offsetTop;
}); };
// Will be called when user dragging an element var _shutDrag = function(e){
window.addEvent(window, 'mousemove', function(e){ selected = null;
};
var _onMove = function(e){
// Get position // Get position
x_pos = document.all ? window.event.clientX : e.pageX; x_pos = document.all ? window.event: e.pageX;
y_pos = document.all ? window.event.clientY : e.pageY; y_pos = document.all ? window.event : e.pageY;
if (e.type === "touchmove"){
x_pos = e.touches[0].clientX;
y_pos = e.touches[0].clientY;
}
if (selected !== null) { if (selected !== null) {
dragged = true; dragged = true;
selected.style.left = (x_pos - x_elem) + 'px'; selected.style.left = (x_pos - x_elem) + 'px';
selected.style.top = (y_pos - y_elem) + 'px'; selected.style.top = (y_pos - y_elem) + 'px';
} }
}); };
// Start dragging
window.addEvent(elem, 'mousedown', _initDrag);
window.addEvent(elem, 'touchstart', _initDrag);
// Will be called when user dragging an element
window.addEvent(window, 'mousemove', _onMove);
window.addEvent(window, 'touchmove', _onMove);
// Destroy the object when we are done // Destroy the object when we are done
window.addEvent(window, 'mouseup', function(e){ window.addEvent(window, 'mouseup', _shutDrag);
selected = null; window.addEvent(window, 'touchend', _shutDrag);
}); window.addEvent(window, 'touchcancel', _shutDrag);
// Handle click event // Handle click event
window.addEvent(elem, 'click', function(e){ window.addEvent(elem, 'click', function(e){