首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jquery UI,使用线性而不是swing,何时恢复可拖动?

问Jquery UI,使用线性而不是swing,何时恢复可拖动?
EN

Stack Overflow用户
提问于 2012-04-26 06:36:38
回答 1查看 1.3K关注 0票数 1

我使用的是Jquery UI的draggable,并且我打开了恢复功能。

默认情况下,div会以“摆动”的方式恢复,

我想知道如何才能使div恢复为直线运动。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-26 22:12:48

你不能在draggable上使用常规的还原方法来获得自定义的返回缓动,因为它只支持更改持续时间。如果要使还原具有自定义效果,则需要如下所示的少量自定义代码,并插入JQueryUI showcase http://jqueryui.com/demos/effect/#easing中的任何自定义缓动效果。

请注意,在stop方法中,我将缓和效果设为easeInElastic以突出显示差异,但您可以将其更改为您想要的任何值(在本例中为线性)。

请注意,您需要包含JQuery UI才能获得这些效果。

http://jsfiddle.net/gregjuva/Hjf8p/

代码语言:javascript
复制
$(function() {
$("#draggable").draggable({
    // We Can't use revert, as we animate the original object so
    //revert: true,  <- don't use this

    helper: function(){
        // Create an invisible div as the helper. It will move and
        // follow the cursor as usual.
        return $('<div></div>').css('opacity',0);
    },
    create: function(){
        // When the draggable is created, save its starting
        // position into a data attribute, so we know where we
        // need to revert to.

        // cache $this to keep from having to make 
        // lots of DOM calls w jquery
        var $this = $(this); 
        $this.data('starttop',$this.position().top)
             .data('startleft',$this.position().left);
    },
    stop: function(){
        // When dragging stops, revert the draggable to its
        // original starting position.
        var $this = $(this);
        $this.stop().animate({
            top: $this.data('starttop'),
            left:$this.data('startleft')
        },1000,'easeInElastic'); 
        // replace with whatever jQueryUI easing you want
    },
    drag: function(event, ui){
        // During dragging, animate the original object to
        // follow the invisible helper with custom easing.
        $(this).stop().animate({
            top: ui.helper.position().top,
            left: ui.helper.position().left
        },0,'linear');
    }
   });
 });​
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10324811

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档