Skip to content

Instantly share code, notes, and snippets.

@nmenag
Last active January 22, 2016 16:27
Show Gist options
  • Save nmenag/53c3efe5d217d12503c2 to your computer and use it in GitHub Desktop.
Save nmenag/53c3efe5d217d12503c2 to your computer and use it in GitHub Desktop.
jQuery plugin sortable
(function ($) {
$.fn.sortable = function (options){
options = $.extend({
url: null
}, options);
return this.each(function (){
var handle = $(this);
var items = handle.children(options.items);
items.attr('draggable', true);
items.on('dragstart',function(e){
this.style.opacity = '0.4';
e.originalEvent.dataTransfer.setData('sourceId', $(this).attr('id'));
});
items.on('dragenter', function(e){
$(this).addClass('over');
});
items.on('dragover', function(e){
e.preventDefault();
});
items.on('dragleave', function(e){
$(this).removeClass('over');
});
items.on('drop', function(e){
e.stopPropagation();
var sourceId = e.originalEvent.dataTransfer.getData('sourceId');
var sourceEl = $('#' + sourceId);
$(e.currentTarget).before(sourceEl);
handle.children().each(function(index, el) {
$(el).data('index', index);
});
});
items.on('dragend', function(e){
this.style.opacity = '1';
$('div').removeClass('over');
});
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment