我想實現一個簡單的拖放和拖動,如果有其他方法實現它,不需要鼠標放下,鼠標移動和鼠標事件。原因是因爲在我拖放鼠標事件之前,我的鼠標向上事件觸發了頁面上的附加事件。我不知道如何讓它適應火災,所以我正在尋找替代方法。實現拖放
Q
實現拖放
0
A
回答
3
你可以嘗試使用jQuery UI的http://jqueryui.com/draggable/和http://jqueryui.com/droppable/達到你想要什麼,兩者的結合可以讓你做了很多,它很容易爲文檔有一些例子來上手,你可以找到很多這樣的例子一個:http://www.elated.com/articles/drag-and-drop-with-jquery-your-essential-guide/
使其在移動設備(觸摸事件)兼容使用此腳本。讓jQuery UI的手機兼容:touchpunch.furf.com
我做了一個的jsfiddle,顯示你如何使用jQuery UI的可棄和可拖動的例子:
// jquery closure
(function($, window, undefined) {
// on dom ready
$(function() {
initializeDropzone();
initializeDraggables();
});
/**
*
* intializing the dropzone element
*
*/
var initializeDropzone = function() {
// initialize the dropzone to make the dropzone a droppable element
// http://jqueryui.com/droppable/
$('#dropzone').droppable({
accept: '.foo', // only elements with this class will get accepted by the dropzone
hoverClass: 'drop_hover', // css class that should get applied to the dropzone if mouse hovers over element
greedy: true,
drop: function onDrop(event, ui) {
console.log('#dropzone drop');
var droppedElement = ui.draggable;
console.log(droppedElement);
// create an object and fill it with data we extract from element that got dropped
var droppedElementData = {};
droppedElementData.id = parseInt(droppedElement.attr('data-foo-id'));
droppedElementData.name = droppedElement.text();
var dropLogElement = $('#dropLog').find('ul');
droppedElementData.position = dropLogElement.children().length + 1;
// create the list html to add a row about the dropped element to our log
var rowHtml = '';
rowHtml += '<li id="' + droppedElementData.position + '_' + droppedElementData.id + '">';
rowHtml += '<span class="position">' + droppedElementData.position + ') </span>';
rowHtml += '<span class="name">' + droppedElementData.name + '</span>';
rowHtml += '</li>';
var row = $(rowHtml);
// append the new row to the log
dropLogElement.append(row);
}
});
};
/**
*
* intializing draggable elements
*
*/
var initializeDraggables = function() {
// http://jqueryui.com/draggable/
// make all elements that have the class "foo" draggable
$('#droppables').find('.foo').draggable({
refreshPositions: true, // refresh position on mouse move, disable if performance is bad
revert: function(event) {
console.log('a "foo" got dropped on dropzone');
// if element is dropped on a dropzone then event will contain
// an object, else it will be false
if (event) {
// the event exists, this means the draggable element got dropped inside of the dropzone
console.log('YEP the event is not false');
// the element that is being dropped
var draggedElement = $(this);
// add explosion effect to dragged element
draggedElement.effect(
'explode',
1000,
function() {
console.log('drop scale effect finished');
console.log(draggedElement);
// put the element back to its original position
draggedElement.css('left', '0');
draggedElement.css('top', '0');
// make the element visible again by fading it in
draggedElement.show('fade', {}, 1000);
}
);
return false;
} else {
// false because draggable element got dropped outside of the dropzone
console.log('NOPE no object, the event is false');
return true;
}
},
start: function(event, ui) {
// the user started dragging an element
console.log('#droppables .foo drag start');
},
stop: function(event, ui) {
// the user has released the draggable element
console.log('#droppables .foo drag stop');
}
});
// make all elements that have the class "bar" draggable (but the dropzone wont accept them)
$('#nonDroppables').find('.bar').draggable(
{
revert: true, // if the element did not get dropped in the dropzone or not accepted by it, then revert its position back to its original coordinates
start: function(event, ui) {
// the user started dragging an element
console.log('#nonDroppables .bar drag start');
},
stop: function(event, ui) {
// the user has released the draggable element
console.log('#nonDroppables .bar drag stop');
}
}
);
};
})(jQuery, window);
相關問題
- 1. Android拖放實現
- 2. 實現窗口拖放WPF
- 3. 如何實現拖放gui?
- 4. 實現拖放在薑餅
- 5. 實現拖放類似
- 6. 在wp8.1中實現拖放
- 7. 在CakePHP中使用AJAX實現拖放
- 8. 如何實現拖放功能extjs 4.0.2a
- 9. 如何在android中實現拖放?
- 10. 如何在android 2.2中實現拖放?
- 11. 如何實現拖放操作
- 12. 實現拖放在UITable視圖
- 13. 在NSTableView中實現拖放操作
- 14. 在Java中實現拖放接口
- 15. 如何實現拖放圖像?
- 16. 如何實現拖放在GWT
- 17. dojox.mobile.ListItem使用dojo拖放實現
- 18. 使用UILongPressGestureRecognizer實現拖放UIImageView
- 19. 如何在Angular2中實現拖放?
- 20. 無法使用Osmdroid實現onTouchEvent(拖放)
- 21. 如何實現Ionic的拖放指令?
- 22. 如何實現拖放到畫布上?
- 23. 如何使用角度拖動實現拖放
- 24. 離子如何實現按下按鈕,釋放,拖放事件?
- 25. 在現有的datagrid行上實現拖放選項?
- 26. 實施textarea的拖放
- 27. 實時拖放在Swing中
- 28. Raphael Scope拖放n拖放多個紙張實例
- 29. 實現 - 拖-n-drop-using-jquery
- 30. 實現MKPinAnnotationView拖動功能?
你可以有一個布爾'isDragging',在mouseMove事件中將其設置爲'true',並在mouseUp事件中檢查其狀態以確定適當的行爲? – kevinsa5
如果您使用jQuery附加事件,則應按照它們附加的順序觸發。我們可以看到你的代碼嗎? – 2013-07-20 16:57:34
第一隻鼠標上掛在我的面前。當用戶在div上單擊(鼠標按下)時,我的魔方纔會被連接。另外,我在鼠標上設置的鼠標移動不會被'$(element).off(「mousemove」)'移除。 –