JS 橡皮筋效果,有人知道吗?

发布于 2025-01-08 19:50:07 字数 255 浏览 3 评论 0原文

给定一个 div“square” 鉴于我已经在该 div 上有一个 touchmove 函数,并且我可以实时检测位置 X:

如何实现橡皮筋效果?

我的意思是:点击并向左拖动,直到阻力达到极限,如果松开手指,square div 将通过缓动动画返回到其初始位置,

有一个简单的数学吗?或者插件?

如果可能的话,不使用jquery更新

会更好

Given a div "square"
and given I already have a touchmove function on that div and I can detect the position X in real time:

how can I implement the rubber band effect?

I mean: tap and drag to the left until the resistance reach the limit and if ou release the finger the square div goes back to its initial position with an easing animation

there is a simple math for that? or a plugin?

UPDATE

w/o jquery would be better if possible

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恍梦境° 2025-01-15 19:50:07

将其原始位置存储在某处。

然后在拖尾事件上:

$(this).animate({
    top: original_top,
    left: original_left
}, 'slow');

演示:http://jsfiddle.net/maniator/T8zYt/

完整代码(使用 jQuery draggable):

(function($) {
    $.fn.rubber = function(resist) {

        var self = this,
            position = $(this).position(),
            selfPos = {
                top: position.top,
                left: position.left,
                maxTop: resist + position.top,
                maxLeft: resist + position.left,
                minTop: resist - position.top,
                minLeft: resist - position.left
            };
        self.draggable({
            drag: function() {
                var position = $(this).position(), width = $(this).width(),  height = $(this).height();
                if (position.left > selfPos.maxLeft || (position.left - width)  < selfPos.minLeft || position.top > selfPos.maxTop || (position.top - height) < selfPos.minTop) {
                    return false;
                }
            },
            stop: function() {
                $(this).animate({
                    top: selfPos.top,
                    left: selfPos.left
                }, 'slow');
            }
        })

    };
})(jQuery)

$('selector').rubber(10);​

Store its original position somewhere.

Then on the dragend event:

$(this).animate({
    top: original_top,
    left: original_left
}, 'slow');

Demo: http://jsfiddle.net/maniator/T8zYt/

Full code (with jQuery draggable):

(function($) {
    $.fn.rubber = function(resist) {

        var self = this,
            position = $(this).position(),
            selfPos = {
                top: position.top,
                left: position.left,
                maxTop: resist + position.top,
                maxLeft: resist + position.left,
                minTop: resist - position.top,
                minLeft: resist - position.left
            };
        self.draggable({
            drag: function() {
                var position = $(this).position(), width = $(this).width(),  height = $(this).height();
                if (position.left > selfPos.maxLeft || (position.left - width)  < selfPos.minLeft || position.top > selfPos.maxTop || (position.top - height) < selfPos.minTop) {
                    return false;
                }
            },
            stop: function() {
                $(this).animate({
                    top: selfPos.top,
                    left: selfPos.left
                }, 'slow');
            }
        })

    };
})(jQuery)

$('selector').rubber(10);​
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文