我目前正在申请中实施复活节彩蛋功能。每当用户将鼠标悬停在jQuery UI关闭按钮上时,对话框div应随机重新定位(因此用户无法关闭它)。
这是我到目前为止所尝试的内容:
$('.ui-icon-closethick').hover(function() {
console.log("hover");
$('#trollDialog').dialog('option', 'position',
[randomX, randomY]
);
});
不幸的是,对话框根本没有重新定位。如何使用绝对x和y位置重新定位对话框?
答案 0 :(得分:1)
这是一个快速工作的例子:
$("#dialog").dialog();
$(".ui-dialog-titlebar-close").hover(function () {
var randomPos = "left" + (Math.random() * 10 < 5 ? "-" : "+") + Math.random() * 100 + " " + "top" + (Math.random() * 10 < 5 ? "-" : "+") + Math.random() * 100;
$("#dialog").dialog("option", "position", { my: randomPos });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<div id="dialog" title="Hover to Move"></div>
&#13;
如果要使用jQuery-UI left
top
和position
属性的偏移量
注意:如果您查看&#34;整页&#34;
,该代码段的效果会更好答案 1 :(得分:0)
不是绝对定位,而是你can do relative:
$('.ui-icon-closethick').hover(function(e) {
var newPos = (Math.floor(Math.random() * 2) == 0 ? "left-" : "right-") +
(Math.floor(Math.random() * 100)) + " " +
(Math.floor(Math.random() * 2) == 0 ? "bottom-" : "top-") +
(Math.floor(Math.random() * 100));
$( "#trollDialog" ).dialog( "option", "position", {
my: newPos
});
});
不是完美的随机化,但无论如何都是一个例子