$( "#sortable" ).sortable({
change: function(event, ui) {
var pos = ui.helper.index() < ui.placeholder.index()
? { start: ui.helper.index(), end: ui.placeholder.index() }
: { start: ui.placeholder.index(), end: ui.helper.index() }
$(this)
.children().removeClass( 'highlight' )
.not( ui.helper ).slice( pos.start, pos.end ).addClass( 'highlight' );
},
stop: function(event, ui) {
$(this).children().removeClass( 'highlight' );
}
});
An example of how it could be done inside change event without storing arbitrary data into element storage. Since the element where drag starts is ui.helper
, and the element of current position is ui.placeholder
, we can take the elements between those two indexes and highlight them. Also, we can use this
inside handler since it refers to the element that the widget is attached. The example works with dragging in both directions.