拖拽最简单的实现方式[JQuery实现]

把Selector  ".gari-drop" 换成你自己的,万物皆可拖


$(function(){
	$(document).on("mousedown",".gari-drop",function(e){
		var el=$(this);
        var os = el.offset(); 
        dx = e.pageX-os.left, 
        dy = e.pageY-os.top;
        $(document).on('mousemove.drag', function(e){ 
        	el.offset({top: e.pageY-dy, left: e.pageX-dx}); 
    	});
	});
	$(document).on("mouseup",".gari-drop",function(e){
		console.log(e);
	    $(document).off('mousemove.drag'); 
	});
});

你可能感兴趣的:(拖拽最简单的实现方式[JQuery实现])