JQuery 不包含可拖动的帮助程序

JQuery Draggable helper not contained

本文关键字:帮助程序 拖动 包含可 JQuery      更新时间:2023-09-26

我使用以下代码来初始化可拖动,使用助手来创建缓动效果。但是,使用帮助程序会破坏包含,并允许可拖动元素离开其容器。如何将帮助程序限制到容器?

更多信息:元素包含在左边框和上边框内,而不是右边框和下边框。

JSFiddle: http://jsfiddle.net/qmkVR/12/

        $( ".drag" ).draggable( {
            containment: con_outer,
            scroll: false,
            helper: function(){
                return $('<div></div>').css('opacity',0);
            },
            drag: function(event, ui){
                $(this).stop().animate({
                    top: ui.helper.position().top,
                    left: ui.helper.position().left
                },200,'easeOutCirc');
            },
            start: function() {
                //Make the dragged item the top-most
                zindex ++;
                $(this).css("z-index",zindex);
            }
        }).each(function(index, value) {
            var $this = $(this);
            $this.click(function() {
                //Click only registers if the object isn't being as dragged
                if($this.is(".ui-draggable-dragging")) {
                    return;
                }
                clickPhoto(index);
            });
        }); 
使用以下

代码将帮助程序定义为<div class="drag"></div>

helper: function(){
            return $('<div class="drag"></div>').css('opacity',0);
    },

JS小提琴

http://jsfiddle.net/qmkVR/13/