【问题标题】:Draggable SimpleModal Popup可拖动的 SimpleModal 弹出窗口
【发布时间】:2011-03-04 22:37:51
【问题描述】:

对于 Jquery Eric Martin 的 SimpleModal 1.4.1,我希望弹出窗口可以拖动,所以我尝试了这个 $('#basic-modal-content').modal({ onShow:函数(对话框){
dialog.container.draggable({句柄:'div'}); } });

弹出窗口显示,但我收到“对象不支持方法属性”的错误

我在 div 中添加了 jquery-ui-1.8.10 作为脚本引用和 class="ui-widget-content"。

想法?


编辑:删除句柄:'div' 没有任何新功能,同样的错误,无法移动对话框

这两个不起作用,错误“对象不支持方法的属性”

 $('#basic-modal-content').modal({
        onShow: function(dialog) { $(dialog.container).draggable(); } 
    });


   $('#basic-modal-content').modal({
        onShow: function(dialog) { $(dialog.container).draggable({handle: 'div'}); } 
    });

console.log($(dialog.container));
Result :[object Object]

【问题讨论】:

  • 去掉 { handle: 'div' } 部分是否有效?
  • 试试"$(dialog.container).draggable(...)"
  • 这很奇怪,我在家里试过,效果很好...你能把结果放在:“console.log($(dialog.container));” ?
  • 这是我的“console.log”内容:[div#simplemodal-container.simplemodal-container]

标签: jquery draggable simplemodal


【解决方案1】:

您好,我确认我的评论 :),使用这个:

jQuery(function ($) {
    // Load dialog on page load
    //$('#basic-modal-content').modal();

    // Load dialog on click
    $('#basic-modal .basic').click(function (e) {
        $('#basic-modal-content').modal({
            onShow: function(dialog) {
                console.log($(dialog));

                $(dialog.container).draggable();
            }
        });

        return false;
    });
});

你必须指向一个 DOM 元素!

编辑:我已经添加了我使用的入口代码。

【讨论】:

  • 我有同样的问题,但它在 IE 9 中不起作用有什么想法吗?
【解决方案2】:

如果您实际上不需要使用 JQuery UI 库,可以使用我在 jsfiddle 上找到的以下代码 http://jsfiddle.net/mkUJf/666/ “div#modalbox-container”可以是任何东西。我只是选择使用模态的外部容器。

//make modal draggable
$(function () {
    $('body').on('mousedown', 'div#modalbox-container', function () {
        $(this).addClass('draggable').parents().on('mousemove', function (e) {
            $('.draggable').offset({
                top: e.pageY - $('.draggable').outerHeight() / 2,
                left: e.pageX - $('.draggable').outerWidth() / 2
            }).on('mouseup', function () {
                $(this).removeClass('draggable');
            });
        });
        e.preventDefault();
    }).on('mouseup', function () {
        $('.draggable').removeClass('draggable');
    });
});

或者你可以添加一个jQuery插件,如下所示: 参考:https://css-tricks.com/snippets/jquery/draggable-without-jquery-ui/

(function($) {
$.fn.drags = function(opt) {

    opt = $.extend({handle:"",cursor:"move"}, opt);

    if(opt.handle === "") {
        var $el = this;
    } else {
        var $el = this.find(opt.handle);
    }

    return $el.css('cursor', opt.cursor).on("mousedown", function(e) {
        if(opt.handle === "") {
            var $drag = $(this).addClass('draggable');
        } else {
            var $drag = $(this).addClass('active-handle').parent().addClass('draggable');
        }
        var z_idx = $drag.css('z-index'),
            drg_h = $drag.outerHeight(),
            drg_w = $drag.outerWidth(),
            pos_y = $drag.offset().top + drg_h - e.pageY,
            pos_x = $drag.offset().left + drg_w - e.pageX;
        $drag.css('z-index', 1000).parents().on("mousemove", function(e) {
            $('.draggable').offset({
                top:e.pageY + pos_y - drg_h,
                left:e.pageX + pos_x - drg_w
            }).on("mouseup", function() {
                $(this).removeClass('draggable').css('z-index', z_idx);
            });
        });
        e.preventDefault(); // disable selection
    }).on("mouseup", function() {
        if(opt.handle === "") {
            $(this).removeClass('draggable');
        } else {
            $(this).removeClass('active-handle').parent().removeClass('draggable');
        }
    });

}
})(jQuery);

这两段代码几乎都是即插即用的。

【讨论】:

    【解决方案3】:

    SimpleModal 没有内置这些​​功能...但是您可以使用 JQuery..

    $j('#simplemodal-container').draggable({ containment: "#masterpageBody" });
    

    simplemodal-container --> 是你的容器div id

    masterpageBody --> 是主人body id

    我们必须使用“遏制”,以免拖到屏幕之外。

    【讨论】:

      猜你喜欢
      • 2013-08-06
      • 2014-07-07
      • 1970-01-01
      • 2011-04-07
      • 2013-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多