$(function() {
$( "#draggable" ).draggable( {
/*http://www.elated.com/articles/drag-and-drop-with-jquery-your-essential-guide/*/
containment: '#demo',
cursor: 'move',
snap: '#droppable' // Snaped to target '#droppable' or true to any target
} );
$( "#droppable" ).droppable({
drop: function( event, ui ) {
$( this )
.addClass( "ui-state-highlight" )
.find( "p" )
.html( "Dropped!" );
}
});
});
/* SECOND EXAMPLE */
// http://www.elated.com/articles/drag-and-drop-with-jquery-your-essential-guide/
// Drag Helper, keeps original in its place, and creates a new copy.
$( initialize );
function initialize() {
$('#makeMeDraggable').draggable( {
stack: '#droppable div',
containment: '#demo',
cursor: 'move',
snap: '#droppable', /*Snaps it into the target div*/
helper: myHelper,
stop: handleDragStop,
revert: true
} );
$('#droppable').droppable( {
drop: handleDropEvent
} );
}
function handleDropEvent (event, ui) {
var draggable = ui.draggable;
alert(' the square with ID "' + draggable.attr('id') + '" was dopped!' );
}
function handleDragStop (event, ui) {
var offsetXPos = parseInt( ui.offset.left );
var offsetYPos = parseInt( ui.offset.top );
alert( "Drag stopped!\n\nOffset: (" + offsetXPos + ", " + offsetYPos + ")\n");
}
function myHelper( event ) {
return '
I am a helper - drag me!
';
}
/*****************************************************
****************** Card Example **************/
var correctCards = 0;
$( init );
function init() {
// Hide the success message
$('#successMessage').hide();
$('#successMessage').css( {
left: '580px',
top: '250px',
width: 0,
height: 0
} );
// Reset the game
correctCards = 0;
$('#cardPile').html( '' );
$('#cardSlots').html( '' );
// Create the pile of shuffled cards
var numbers = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
numbers.sort( function() { return Math.random() - .5 } );
for ( var i=0; i<10; i++ ) {
// places each number in the div, .data 'number' is a key to make the object,
$('' + numbers[i] + '
').data( 'number', numbers[i] ).attr( 'id', 'card'+numbers[i] ).appendTo( '#cardPile' ).draggable( {
containment: '#content',
stack: '#cardPile div',
cursor: 'move',
revert: true
} );
}
// Create the card slots
var words = [ 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten' ];
for ( var i=1; i<=10; i++ ) {
$('' + words[i-1] + '
').data( 'number', i ).appendTo( '#cardSlots' ).droppable( {
accept: '#cardPile div',
hoverClass: 'hovered',
drop: handleCardDrop
} );
}
}
function handleCardDrop( event, ui ) {
var slotNumber = $(this).data( 'number' );
var cardNumber = ui.draggable.data( 'number' );
// If the card was dropped to the correct slot,
// change the card colour, position it directly
// on top of the slot, and prevent it being dragged
// again
if ( slotNumber == cardNumber ) {
ui.draggable.addClass( 'correct' );
ui.draggable.draggable( 'disable' );
$(this).droppable( 'disable' );
ui.draggable.position( { of: $(this), my: 'left top', at: 'left top' } );
ui.draggable.draggable( 'option', 'revert', false );
correctCards++;
}
// If all the cards have been placed correctly then display a message
// and reset the cards for another go
if ( correctCards == 10 ) {
$('#successMessage').show();
$('#successMessage').animate( {
left: '380px',
top: '200px',
width: '400px',
height: '100px',
opacity: 1
} );
}
}
Jquery Drag and Drop
Example 1
Attempt to have it stay in dropped box.