Skip to content

Instantly share code, notes, and snippets.

@SebastienTainon
Created October 16, 2018 12:33
Show Gist options
  • Save SebastienTainon/2c0a38ac1e12b8ca9c863e437127ebdc to your computer and use it in GitHub Desktop.
Save SebastienTainon/2c0a38ac1e12b8ca9c863e437127ebdc to your computer and use it in GitHub Desktop.
Drag'n'drop Vue.js mixin to use with vue-drap-grop plugin to be able to use coordinates in Firefox
import $ from 'jquery';
export default {
data () {
return {
initOffset: {
x: 0,
y: 0,
},
coordinates: {
x: 0,
y: 0,
},
};
},
computed: {
shiftCoordinates() {
return {
x: this.coordinates.x - this.initOffset.x,
y: this.coordinates.y - this.initOffset.y,
}
},
},
mounted () {
// Dirty hack for Firefox only...
// @ref: https://stackoverflow.com/questions/887316/how-do-i-get-clientx-and-clienty-to-work-inside-my-drag-event-handler-on-firef?rq=1
$(document)
.on('dragstart', (e) => {
e = e.originalEvent;
this.initOffset.x = e.clientX || e.pageX;
this.initOffset.y = e.clientY || e.pageY;
});
$(document)
.on('dragover', (e) => {
e = e.originalEvent;
this.coordinates.x = e.clientX || e.pageX;
this.coordinates.y = e.clientY || e.pageY;
});
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment