简体   繁体   English

jQuery Animate-以固定速度将DIV移动到X,Y位置

[英]jQuery Animate - Move DIV to X, Y position with a fixed speed

My current jQuery code moves an element to an X and Y position on my page, however I would like a constant speed, regardless of distance. 我当前的jQuery代码将元素移动到页面上的X和Y位置,但是无论距离多远,我都希望速度恒定。

For an example, if I move a short distance, the time will be slow. 例如,如果我移动一小段距离,时间会很慢。 If I move to a further distance, the div will shoot to the position quickly, this is not what I want but I am unsure of how to change this. 如果我移到更远的距离,则div会迅速拍摄到该位置,这不是我想要的,但是我不确定如何更改。

$("#myAvatar").offset({ top: y, left: x})

I'm using the above code, would love to see how it is possible to have a fixed speed regardless of the distance the element moves. 我正在使用上面的代码,很乐意看到不管元素移动的距离如何都有固定的速度。

Sorry if I am being vague or this question is hard to understand, will gladly clarify. 抱歉,如果我不清楚或无法理解此问题,我们将很乐意澄清。

Edit** 编辑**

The CSS is CSS是

transition: left 1.2s cubic-bezier(.42,-0.3,.78,1.25), top 1.2s cubic-bezier(.42,-0.3,.78,1.25);

The transition speed, 1.2s is what I should be fixing here. 过渡速度1.2s是我应该在这里固定的速度。 I am unsure of how to dynamically adjust this in JavaScript/jQuery to achieve the same speed whatever the distance between the X and Y point. 无论X和Y点之间的距离如何,我不确定如何在JavaScript / jQuery中动态调整此速度以实现相同的速度。

you can use .animate() 您可以使用.animate()

$("#myAvatar").animate({ top: y, left: x},1200);

1200 is just a duration you can change it with duration you like but 1200 = 1.2s you have in css 1200只是一个持续时间,您可以根据自己喜欢的持续时间进行更改,但是css中的1200 = 1.2s

如果您想要恒定的速度,请使用“线性”而不是“ cubic-bezier(..)”

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

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