当用户拖动时,我使用velocity.js对可拖动的SVG元素进行动画处理。但是,velocity.js会对前一个鼠标移动坐标进行排队&通过所有后续鼠标移动坐标进行动画处理。我想要的是velocity.js不是对坐标进行排队,而是将函数传递的最新坐标动画显示出来。
检查此jsFiddle
document.addEventListener("mousedown",mouseDown);
document.addEventListener("mouseup",endMove);
var click=false;
var clickX, clickY;
var moveX=0, moveY=0;
var lastMoveX=0, lastMoveY=0;
function mouseDown(evt){
evt.preventDefault();
var element=(typeof (window.event) !=='undefined') ? evt.srcElement:evt.target;
if(element.id==="mycirc")
{
click=true;
clickX = evt.clientX;
clickY = evt.clientY;
}
document.addEventListener("mousemove",moveboth);
return false;
}
function movexaxis(evt)
{
var clx=evt.clientX-clickX;
moveX = lastMoveX + clx;
return moveX;
}
function moveyaxis(evt)
{
var cly=evt.clientY-clickY;
moveY = lastMoveY + cly;
return moveY;
}函数moveboth(evt){ setTimeout(函数移动()){
evt.preventDefault();
var a=document.getElementById("mycirc");
if(click){
movexaxis(evt);
moveyaxis(evt);
Velocity(a,{translateX: moveX},{duration:"0ms"}, {queue:false});
Velocity(a,{translateY: moveY },{duration:"0ms"},{queue:false});
Velocity(a,"stop");
}
},34);
}
function endMove(evt){
click=false;
lastMoveX = moveX;
lastMoveY = moveY;
}发布于 2014-12-31 14:35:24
拖动并不是一种真正的动画类型,因此使用Velocity是一种主要的夸张。因为您已经有了存储x和y坐标的代码,所以您所需要做的就是使用requestAnimationFrame()更新元素的转换,以转换为每个帧上的这些坐标。就是这样(:
https://stackoverflow.com/questions/27704974
复制相似问题