Drag and drop without JQuery UI

后端 未结 7 910
春和景丽
春和景丽 2020-12-14 19:50

I searched a lot to find a tutorial for drag & drop with jQuery alone (without UI), but due to the popularity of JQuery UI, all drag and drop features are based on JQuer

相关标签:
7条回答
  • 2020-12-14 19:57

    There are several plugins that you may use take a look at the following

    http://wayfarerweb.com/jquery/plugins/animadrag/

    http://threedubmedia.com/code/event/drag/demo/

    it still jquery but no UI

    0 讨论(0)
  • 2020-12-14 20:01

    I found this one very useful: http://draggabilly.desandro.com/

    0 讨论(0)
  • 2020-12-14 20:03

    http://thezillion.wordpress.com/2012/09/27/javascript-draggable-2-no-jquery

    See this. It's core JS and easy to implement.

    0 讨论(0)
  • 2020-12-14 20:11

    Came across the same problem, and 33.4 kilobytes for the minified jqueryUI with only draggable and droppable is too large for the limited functionality I needed. The approach below isn't working code - it's just a simple structure to visualize what needs to happen.

    $('.draggable').on{
      mousemove : function(){
        var mouseposition = { // this also needs to account for onclick offset of pointer vis-a-vis element
            x : pageX,
            y : pageY 
        };
        $(this).css({
          top : mouseposition.y,
          left : mouseposition.y
        });
      if( // this statement is kinda bogus, idea is to perform a collision detection via coordinate comparison
        $('.draggable').offset().top < $(.droppable').offset().top 
        &&
        $('.draggable').offset().left < $(.droppable').offset().left
      ) {
          alert('the item has been dropped');
      }
      }
    });
    
    0 讨论(0)
  • 2020-12-14 20:15

    I understand this is an old post, but i was also interested in doing this without Jquery UI. I checked the links above, but i found this to be the best. It's only 8kb minified (UI sortable ~30, i've read), and is independent of any mammoth JQuery library (although those CAN make our lives easier sometimes).

    0 讨论(0)
  • 2020-12-14 20:15

    Answer based on: https://www.sanwebe.com/2014/10/draggable-element-with-jquery-no-jquery-ui

    for sorting perhaps see: http://johnny.github.io/jquery-sortable/

    var draggable = $('#dragit'); //element 
    
    draggable.on('mousedown', function(e){
    	var dr = $(this).addClass("drag").css("cursor","move");
    	height = dr.outerHeight();
    	width = dr.outerWidth();
    	ypos = dr.offset().top + height - e.pageY,
    	xpos = dr.offset().left + width - e.pageX;
    	$(document.body).on('mousemove', function(e){
    		var itop = e.pageY + ypos - height;
    		var ileft = e.pageX + xpos - width;
    		if(dr.hasClass("drag")){
    			dr.offset({top: itop,left: ileft});
    		}
    	}).on('mouseup', function(e){
    			dr.removeClass("drag");
    	});
    });
    #dragit
      {
        background: red;
        width: 50px;
        height: 50px;
        cursor: move;
        position: relative;
      }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="dragit">Drag me</div>

    Constrain movement

    var draggable = $('#dragit-contained'); //element 
    
    draggable.on('mousedown', function(e){
    	var dr = $(this).addClass("drag").css("cursor","move");
    	height = dr.outerHeight();
    	width = dr.outerWidth();
    	max_left = dr.parent().offset().left + dr.parent().width() - dr.width();
    	max_top = dr.parent().offset().top + dr.parent().height() - dr.height();
    	min_left = dr.parent().offset().left;
    	min_top = dr.parent().offset().top;
    
    	ypos = dr.offset().top + height - e.pageY,
    	xpos = dr.offset().left + width - e.pageX;
    	$(document.body).on('mousemove', function(e){
    		var itop = e.pageY + ypos - height;
    		var ileft = e.pageX + xpos - width;
    		
    		if(dr.hasClass("drag")){
    			if(itop <= min_top ) { itop = min_top; }
    			if(ileft <= min_left ) { ileft = min_left; }
    			if(itop >= max_top ) { itop = max_top; }
    			if(ileft >= max_left ) { ileft = max_left; }
    			dr.offset({ top: itop,left: ileft});
    		}
    	}).on('mouseup', function(e){
    			dr.removeClass("drag");
    	});
    });
    .draggable-area
      {
        background: grey;
        width: 320px;
        height: 240px;
      }
    #dragit-contained
      {
        background: red;
        width: 50px;
        height: 50px;
        cursor: move;
        position: relative;
      }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="draggable-area">
    	<div id="dragit-contained">Drag me</div>
    </div>

    0 讨论(0)
提交回复
热议问题