使 jQuery-ui 可拖动手柄覆盖整个页面

发布于 2024-09-04 15:32:23 字数 671 浏览 6 评论 0原文

单击窗口中任意位置时使元素可拖动的最佳方法是什么?

我是否必须使用覆盖整个窗口的容器

我尝试使 body 可拖动,但这不起作用。

使用容器

的问题是它会移动,因此在拖动后不再覆盖整个屏幕。

制作一个非常巨大的容器

怎么样?它在各个方向上跨越大量像素,这样您就永远不会到达它的边缘。这是一个坏主意吗?

这个想法(简化)是有一个中间有一个正方形的页面,可以通过拖动窗口的任何部分来拖动该正方形。

这是一个非常不必要的模型:)

替代文字

我正在尝试使用全屏 div,但是当我重置它时,其中的元素会随之移回.. http ://jsfiddle.net/LUFf6/

What would be the best way to make an element draggable when clicking anywhere in the window?

Would I have to use a container <div> that covers the whole window?

I tried making the body draggable but that didn't work.

The problem with using a container <div> is that it moves and therefore doesn't cover the whole of the screen any more after it has been dragged.

What about making a really vast container <div> that spans a large number of pixels in every direction so you would never get to the edge of it. Is that a bad idea?

The idea (simplified) is to have a page with a square in the middle that can be dragged by dragging any part of the window.

Here's a wonderfully unnecessary mockup :)

alt text

I'm trying with a full screen div, but when I reset it, the element within it moves back with it.. http://jsfiddle.net/LUFf6/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绅刃 2024-09-11 15:32:24

编辑:修改为包括鼠标距 div 的偏移量。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
<script>
function doMove(e) {
  var $d = $('#myDiv');
  var off = $d.offset();
  var left = e.pageX + $d.data('left');
  var top = e.pageY + $d.data('top');
  $d.offset({left: left, top: top});
};
$(function() {
  $(document).mousedown(function(e) {
     var $d = $('#myDiv');
     var off = $d.offset();
     $d.data('left',off.left - e.pageX);
     $d.data('top',off.top - e.pageY);     
     $(document).bind('mousemove', doMove);
  }).mouseup(function(e) {
     $(document).unbind('mousemove',doMove);
  });
});
</script>
<style>
div#myDiv {
  width: 50px;
  height: 50px;
  background-color: red;
  position: absolute;
  top: 100px;
  left: 100px;
}
</style>
<div id="myDiv"></div>

EDIT: revised to include mouse offset from the div.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
<script>
function doMove(e) {
  var $d = $('#myDiv');
  var off = $d.offset();
  var left = e.pageX + $d.data('left');
  var top = e.pageY + $d.data('top');
  $d.offset({left: left, top: top});
};
$(function() {
  $(document).mousedown(function(e) {
     var $d = $('#myDiv');
     var off = $d.offset();
     $d.data('left',off.left - e.pageX);
     $d.data('top',off.top - e.pageY);     
     $(document).bind('mousemove', doMove);
  }).mouseup(function(e) {
     $(document).unbind('mousemove',doMove);
  });
});
</script>
<style>
div#myDiv {
  width: 50px;
  height: 50px;
  background-color: red;
  position: absolute;
  top: 100px;
  left: 100px;
}
</style>
<div id="myDiv"></div>
风为裳 2024-09-11 15:32:24

这实际上取决于您想要实现的目标,因此我们需要了解更多。
您可以在每次“放置”事件后将可拖动的 div 移动到屏幕的左上角和全宽/高度吗?

不过,我个人不会走“大容器”路线。

it really depends on what you are trying to achieve, so we'll need to know a bit more really.
You could just move the draggable div to the top left of the screen and full width/height after each "drop" event?

I personally wouldn't go down the 'vast container' route though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文