I'm using jQuery draggable and droppable for a work-planning system I'm developing. Users drag jobs to a different day or user, and then data is updated using an ajax call.

Everything works fine, except when I scroll down the main page (Jobs appear on a large week planner that exceeds the bottom of my browser window). If I try and drag a draggable element here, the element appears above my mouse cursor the same amount of pixels as I've scrolled down.. The hover state still works fine and the functionality is bang on but it doesn't look right.

I'm using jQuery 1.6.0 and jQuery UI 1.8.12.

我确信我需要添加一个偏移函数,但我不知道在哪里应用它,或者是否有更好的方法.这是我的.draggable()初始化代码:

$('.job').draggable({
  zIndex: 20,
  revert: 'invalid',
  helper: 'original',
  distance: 30,
  refreshPositions: true,
});

Any idea what I can do to fix this?

推荐答案

This might be a related bug report, it's been around for quite a while: http://bugs.jqueryui.com/ticket/3740

我测试的每个浏览器(Chrome、FF4、IE9)似乎都会发生这种情况.有几种方法可以解决此问题:

1. Use position:absolute; in your css. Absolutely positioned elements don't seem to be affected.

2.确保父元素(如果是主体,则为事件)设置了overflow:auto;.我的测试表明,这个解决方案修复了位置,但它禁用了autoscroll功能.您仍然可以使用鼠标滚轮或箭头键滚动.

3.手动应用上述错误报告中建议的修复程序,并仔细测试是否会导致其他问题.

4. Wait for an official fix. It's scheduled to jQuery UI 1.9, although it has been postponed a few times in the past.

5. If you're confident that it happens on every browser, you can put these hacks into the affected draggables' events to correct the calculations. It's a lot of different browsers to test though, so it should only be used as a last resort:

$('.drag').draggable({
   scroll:true,
   start: function(){
      $(this).data("startingScrollTop",$(this).parent().scrollTop());
   },
   drag: function(event,ui){
      var st = parseInt($(this).data("startingScrollTop"));
      ui.position.top -= $(this).parent().scrollTop() - st;
   }
});

Jquery相关问答推荐

为什么我不能在 window.onload 事件的处理程序中将 $ jQuery 对象作为参数传递?

jQuery:如何动态检测窗口宽度?

输入类型=文件的jQuery更改方法

jQuery: Select 属性大于值的所有元素

如何使用 jQuery 重置 jquery Select 的 Select 选项?

SCRIPT7002:XMLHttpRequest:网络错误 0x2ef3,由于错误 00002ef3 无法完成操作

实用的 javascript 面向对象设计模式示例

谷歌图表重绘/zoom 窗口调整大小

使用 jQuery Select 焦点文本在 Safari 和 Chrome 中不起作用

jQuery.extend 和 jQuery.fn.extend 的区别?

offsetTop 与 jQuery.offset().top

带有 jquery.animate() 的 CSS 旋转跨浏览器

JQuery Ajax Post 导致 500 内部服务器错误

停止输入/书写后如何触发输入文本中的事件?

在 jQuery UI 自动完成上没有检测到结果

如何使用 JQuery 创建一个新的 img 标签,其中包含来自 JavaScript 对象的 src 和 id?

在 jQuery 中 Select 后代元素的最快方法是什么?

jQuery empty() 与 remove()

detach()、hide() 和 remove() 之间的区别 - jQuery

如何使用 jQuery 或仅使用 Javascript 将按钮重定向到另一个页面