Last active
January 22, 2016 16:27
-
-
Save nmenag/53c3efe5d217d12503c2 to your computer and use it in GitHub Desktop.
jQuery plugin sortable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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