最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is dragging with css translate jumping? - Stack Overflow

programmeradmin5浏览0评论

I am trying to drag a container using transform translate but something is causing a jumpy behavior and I can't figure out what is the cause.
UPDATE: This must work on elements when their container is not always positioned at 0,0 from the document.

/

HTML:

<div id=container style="position:absolute;left:50px;top:50px;width:500px;height:500px;background-color:red;">
    <div id=tcontainer style="position:relative;left:50px;top:50px;width:400px;height:400px;background-color:green;">
        <div id=move style="position:relative;left:20px;top:20px;height:150px;width:150px;background-color:lightgray;">
    </div>
</div>


Javascript:

obj = {startPositionX:0,startPositionY:0};
$('#move').on("mousedown",function(){
    var move = $(this);
    obj.startPositionX=event.offsetX+$('#tcontainer').offset().left;
    obj.startPositionY=event.offsetY+$('#tcontainer').offset().top;
    $(document).on("mousemove",function(e){
        console.log("dragging", e.pageX-obj.startPositionX, e.pageY-obj.startPositionY);
        move.css('transform','translate('+(e.pageX-obj.startPositionX)+'px, '+(e.pageY-obj.startPositionY)+'px)');
    });
});
$(document).on("mouseup",function(){
    $(this).off("mousemove");
});

I am trying to drag a container using transform translate but something is causing a jumpy behavior and I can't figure out what is the cause.
UPDATE: This must work on elements when their container is not always positioned at 0,0 from the document.

http://jsfiddle/dML5t/2/

HTML:

<div id=container style="position:absolute;left:50px;top:50px;width:500px;height:500px;background-color:red;">
    <div id=tcontainer style="position:relative;left:50px;top:50px;width:400px;height:400px;background-color:green;">
        <div id=move style="position:relative;left:20px;top:20px;height:150px;width:150px;background-color:lightgray;">
    </div>
</div>


Javascript:

obj = {startPositionX:0,startPositionY:0};
$('#move').on("mousedown",function(){
    var move = $(this);
    obj.startPositionX=event.offsetX+$('#tcontainer').offset().left;
    obj.startPositionY=event.offsetY+$('#tcontainer').offset().top;
    $(document).on("mousemove",function(e){
        console.log("dragging", e.pageX-obj.startPositionX, e.pageY-obj.startPositionY);
        move.css('transform','translate('+(e.pageX-obj.startPositionX)+'px, '+(e.pageY-obj.startPositionY)+'px)');
    });
});
$(document).on("mouseup",function(){
    $(this).off("mousemove");
});
Share edited Dec 12, 2013 at 19:47 shuji asked Dec 12, 2013 at 5:36 shujishuji 7,5637 gold badges37 silver badges53 bronze badges 6
  • Is this an original concept/implementation or do you have a working, non-jerky example you're working from? If its a novel use of a css transform, its very clever but may just be jerky because of the lag between the js engine to the DOM to the CSS render. – Anthony Commented Dec 12, 2013 at 6:08
  • css-tricks./snippets/jquery/draggable-without-jquery-ui – Anthony Commented Dec 12, 2013 at 6:11
  • No, I am working on a svg project which I want to export with transform attributes. This is only an example I made. – shuji Commented Dec 12, 2013 at 6:12
  • What do you mean by export? – Anthony Commented Dec 12, 2013 at 6:14
  • @Anthony to save it and maybe using it in other environments where there is probably not support for css. – shuji Commented Dec 12, 2013 at 17:43
 |  Show 1 more ment

1 Answer 1

Reset to default 6

OffsetX and OffsetY may give you cursor pos relative to an element which is hovered now. You saved initial coordinates in mousedown. When mousemove was triggered your this coordinates changed a little, so when you subtract one from initials you got zeros (or 1px of difference) and your div went to top left corner. After it happaned your cursor hovered body element and in mousemove you get coordinates related to body. So when you subtract your zeros from the new coordinates you get real coordinates and your div go to the right place. Then you will get coordinates related to dragging div and will get zeros again, then real coords and so on.

Use pageX and pageY instead! fiddle

$('.move').on("mousedown",function(me){
    var move = $(this);

    var lastOffset = move.data('lastTransform');
    var lastOffsetX = lastOffset ? lastOffset.dx : 0,
        lastOffsetY = lastOffset ? lastOffset.dy : 0;

    var startX = me.pageX - lastOffsetX, startY = me.pageY - lastOffsetY;

    $(document).on("mousemove",function(e){
        var newDx = e.pageX - startX,
            newDy = e.pageY - startY;
        console.log("dragging", e.pageX-startX, e.pageY-startY);
        move.css('transform','translate(' + newDx + 'px, ' + newDy + 'px)');

        // we need to save last made offset
        move.data('lastTransform', {dx: newDx, dy: newDy });
    });
});
$(document).on("mouseup",function(){
    $(this).off("mousemove");
});

You need save original coords of your div (move.offset()) and use mouse offset (e.pageX-startX, e.pageY-startY) to get new coords.

发布评论

评论列表(0)

  1. 暂无评论