2017-05-24 39 views
0

https://jsfiddle.net/scottbeeson/r5du4p6k/12/未捕獲錯誤:無法在初始化之前調用可拖動的方法;試圖調用方法「禁用」

如果長按拖動,當你釋放你得到以下錯誤:

Uncaught Error: cannot call methods on draggable prior to initialization; attempted to call method 'disable'

var t; 
$(document).on('touchstart mousedown','.menu-item', function (event) { 
    var self = this; 
    if ($(self).hasClass('draggable')) return; 
    t = setTimeout(function() { 
     $(self).draggable({ 
      revert: true, 
      helper: 'clone', 
      opacity: .75, 
      appendTo: 'body' 
     }).draggable('enable').addClass('draggable'); 
     $(self).trigger(event) 
    }, 800); 
}); 

$(document).on("touchend mouseup", function() { 
    clearTimeout(t); 
    $('.draggable').draggable('disable').removeClass('draggable'); 
}); 

回答

1

既然你複製原始的元素,當你嘗試刪除draggable - 您也可以將它複製到您剛纔複製的元素(這是不可拖動的),因此您不能disable它。

你可以做的是禁用它只是第一個元素:

$('.draggable').first().draggable('disable').removeClass('draggable'); 

檢查:
https://jsfiddle.net/r5du4p6k/13/

+0

哦,從拖動事件克隆?說得通。謝謝。 –

+0

當然:)歡迎您! – Dekel

+1

而不是使用'.first()'我用'$('#container .draggable')...'來限制範圍。這似乎更可靠。 –

相關問題