我需要将元素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)
...
答案 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]
});