简体   繁体   English

javascript中的拖放操作

[英]the drag and drop operation in javascript

I know how to implement the drag and drop effect in JavaScript,but I found that in some site,the speed of the drag and drop operation will cause different result. 我知道如何在JavaScript中实现拖放效果,但我发现在某些站点中,拖放操作的速度会导致不同的结果。

Take the Google map for example,the map view is draggable ,when you drag the map slowly,the map will move with your mouse ,however if you drag the map very fast and then drop the mouse,the map will keep moving (a limited distance),it seems that it has a buffer distance. 以谷歌地图为例,地图视图是可拖动的,当您慢慢拖动地图时,地图将随您的鼠标移动,但是如果您快速拖动地图然后放下鼠标,地图将继续移动(限制距离),似乎它有一个缓冲距离。

I wonder how to make it? 我想知道如何制作它?

There is no vanilla effect for that, unfortunately. 不幸的是,没有香草效果。

You will have to count various parameters such as distance, direction and time that it took to complete the drag. 您将需要计算各种参数,例如完成拖动所需的距离,方向和时间。

Then depending on the results you can compute a bezier curve and extend the animation so that it seems seamless and smooth. 然后根据结果,您可以计算贝塞尔曲线并扩展动画,使其看起来无缝平滑。

Here's an example... http://jsfiddle.net/an44z/ 这是一个例子...... http://jsfiddle.net/an44z/

It works, if maybe not that well (written hastily for an old project, but it might give you a good idea of what is happening - should be happening). 它可以工作,如果可能不是那么好(匆忙写一个旧项目,但它可能会让你很好地了解正在发生的事情 - 应该发生)。

EDIT: Alternatively you can study this mooTools example since it is pretty much exactly what you're after. 编辑:或者你可以研究这个mooTools的例子,因为它几乎就是你所追求的。 http://mootools.net/demos/?demo=Drag.Scroll http://mootools.net/demos/?demo=Drag.Scroll

Sounds like you're looking for some sort of "drag velocity". 听起来你正在寻找某种“拖动速度”。 I spent some time this morning putting this mouse-to-throw example together for you so I hope it helps (I had some fun making it too). 我今天早上花了一些时间把这个鼠标抛到一起例子给你,所以我希望它有所帮助(我也有一些乐趣)。 The *principle * is the same for what you're wanting to do and is something you can have a lot of fun with. *原则*对于您想要做的事情是相同的,并且您可以享受很多乐趣。

Brunt of Javascript used: 使用Javascript的Brunt:

// this is the easing I used in my "throwing" animation
// I use this to average out some of the arrays (distance & angle)
$.easing.easeOutCirc = function (x, t, b, c, d) {
    return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
}

// stores the last drag spot, useful for calculating distances between points
var drag_last_spot;
// stores distances for use with calculating animation distance
var drag_distances;
// stores rads for use with calculating throw angle
var drag_rads;
// keeps the offset of the initial click to know where the mouse "went down" on the box
var drag_offset;
// keeps track of the "current target" since mouseUp and MouseMove are document level
var drag_target;
// keeps track of how long you held your mouse down on the box (to determine animation length)
var drag_time;

// taken via Google from http://jsfromhell.com/array/average
average = function(a){
    var r = {mean: 0, variance: 0, deviation: 0}, t = a.length;
    for(var m, s = 0, l = t; l--; s += a[l]);
    for(m = r.mean = s / t, l = t, s = 0; l--; s += Math.pow(a[l] - m, 2));
    return r.deviation = Math.sqrt(r.variance = s / t), r;
}
function onMouseDown(event) {
    // offset and last_spot are the same for the first move iteration
    drag_time = (new Date).getTime();
    drag_offset = drag_last_spot = {
        x: event.offsetX,
        y: event.offsetY
    };

    // initialize or reset the distances and angle arrays
    drag_distances = [];
    drag_rads = [];

    // because there are multiple boxes, we need to keep track of the target since our events are document level
    drag_target = $(this);

    $(document).bind("mousemove", onMouseMove);
    $(document).bind("mouseup", onMouseUp);
}

function onMouseMove(event) {
    // use pathagorean theorem for distance between two points (yay geometry!)
    var dist = Math.sqrt(Math.pow(drag_last_spot.x - event.clientX, 2) + Math.pow(drag_last_spot.y - event.clientY, 2));

    // push all the distances to this array for later use
    drag_distances.push(dist);

    // push all radians to this array for later use
    var cur_rad = Math.atan2(event.clientY - drag_last_spot.y, event.clientX - drag_last_spot.x);
    drag_rads.push(cur_rad);

    // we need to know the last drag spot so we can calc the distance of the points during the next onMouseMove
    drag_last_spot = {
        x: event.clientX,
        y: event.clientY
    };

    drag_target.css({left:event.clientX - drag_offset.x, top:event.clientY - drag_offset.y});
}

function onMouseUp(event) {
    /* FYI wherever you see .slice(-N) you can change N to any number. I recommend a small number as a short drag will only have 5 or 10 items. A big number will average more of your "throw", but a small number is seemingly safer.*/

    // this is the total duration of how long you dragged for
    var duration = ((new Date).getTime() - drag_time);

    // this is the distance of your drag (I times by three for a better animated effect)
    var dist = average(drag_distances.slice(-3)).mean * 3;

    // these help determine the direction of your throw (figures out the angle)
    var rad = average(drag_rads.slice(-3)).mean - Math.PI / 2;
    var to_left = event.clientX + Math.sin(rad) * -dist - drag_offset.x;
    var to_top = event.clientY + Math.cos(rad) * dist - drag_offset.y;

    // now to animation your throw!
    drag_target.animate({left:to_left, top:to_top}, duration * 2, "easeOutCirc");

    $(document).unbind("mousemove");
    $(document).unbind("mouseup");
}

This example is just the tip of the iceberg so I say mess around with the script to learn how it works as you'll likely come up with new cool ways to do things. 这个例子只是冰山一角,所以我说要乱用脚本来了解它是如何工作的,因为你可能会想出新的很酷的方法来做事。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM