jQuery - draggable div with zoom

前端 未结 3 519
暖寄归人
暖寄归人 2020-12-19 00:58

This is my code:

http://jsfiddle.net/652nk/

HTML

3条回答
  •  半阙折子戏
    2020-12-19 01:25

    You don't need to set zoom property. I just added the difference to draggable's position which occurs due to the zoom property. Hope it helps.

    Fiddle

    http://jsfiddle.net/TqUeS/

    JS

    var zoom = $('#canvas').css('zoom');
    var canvasHeight = $('#canvas').height();
    var canvasWidth = $('#canvas').width();
    
    $('#dragme').draggable({
        drag: function(evt,ui)
        {
            // zoom fix
            ui.position.top = Math.round(ui.position.top / zoom);
            ui.position.left = Math.round(ui.position.left / zoom);
    
            // don't let draggable get outside the canvas
            if (ui.position.left < 0) 
                ui.position.left = 0;
            if (ui.position.left + $(this).width() > canvasWidth)
                ui.position.left = canvasWidth - $(this).width();  
            if (ui.position.top < 0)
                ui.position.top = 0;
            if (ui.position.top + $(this).height() > canvasHeight)
                ui.position.top = canvasHeight - $(this).height();  
    
        }                 
    });
    

提交回复
热议问题