dragging items based on percentage to containment element

后端 未结 3 1629
长情又很酷
长情又很酷 2020-12-16 14:13

Here is what it looks like;

$( \"#box ul li\" ).draggable({
    helper: \"clone\"
});
$( \".item\" ).draggable({containment: \".door\"});

$( \".door\" ).dro         


        
相关标签:
3条回答
  • 2020-12-16 14:40

    I've found a solution;

    $( ".item" ).draggable({
      containment: ".door",
      stop: function( event, ui ) {
       $(this).css("left",parseInt($(this).css("left")) / ($(".door").width() / 100)+"%");
       $(this).css("top",parseInt($(this).css("top")) / ($(".door").height() / 100)+"%");
      }
    });
    
    0 讨论(0)
  • 2020-12-16 14:41

    Try this Code :

    $( ".element" ).draggable({
    
        stop: function (){
         var l = ( 100 * parseFloat($(this).css("left")) / parseFloat($(this).parent().css("width")) )+ "%" ;
         var t = ( 100 * parseFloat($(this).css("top")) / parseFloat($(this).parent().css("height")) )+ "%" ;
         $(this).css("left" , l);
         $(this).css("top" , t);
         }
    
    });
    
    0 讨论(0)
  • 2020-12-16 14:46

    Little bit updated version of all above:

    $('.element').draggable({
        containment: 'parent',
        stop: function( event, ui ) {
            var $elm = $(this);
            var pos = $elm.position(),
                parentSizes = {
                    height: $elm.parent().height(),
                    width: $elm.parent().width()
                };
    
            $elm.css('top', ((pos.top/parentSizes.height) * 100) + '%').css('left', ((pos.left/parentSizes.width) * 100) + '%');
    }});
    
    0 讨论(0)
提交回复
热议问题