如何将draggable元素返回到选项中?

时间:2015-05-28 17:34:45

标签: javascript jquery jquery-ui

我需要将元素containment返回到var windowWidth = $(window).width(); var windowHeight = $(window).height(); $(".draggable-box").draggable({ containment: [($(this).width() / 2), 50, windowWidth - 100, windowHeight - 200] }); 选项,我该如何实现?

$(this)

如果我使用window,则会返回 import java.lang.ClassLoader val cl = ClassLoader.getSystemClassLoader cl.asInstanceOf[java.net.URLClassLoader].getURLs.foreach(println) ...

2 个答案:

答案 0 :(得分:2)

  

如果我使用$(this),则会返回window ...

您需要引用每个.draggable元素:

$(".draggable-box").each(function ( i, draggable ) {
  var $draggable = $(draggable);
  $draggable.draggable({
    containment: [($draggable.width() / 2), 50, windowWidth - 100, windowHeight - 200]
  });
});

请注意,each回调this内部会引用元素(我的示例中为draggable),因此您可以使用this代替。

答案 1 :(得分:0)

draggable-box元素存储在自己的变量中。试试这个:

var windowWidth = $(window).width();
var windowHeight = $(window).height();
var $draggableBox = $(".draggable-box");

$draggableBox.draggable({
    containment: [($draggableBox.width() / 2), 50, windowWidth - 100, windowHeight - 200]
});