页面滚动后,jQuery 可拖动在错误的位置显示帮助程序
我正在使用 jQuery draggable 和 droppable 用于我正在开发的工作计划系统。用户将作业拖到不同的日期或用户,然后使用 ajax 调用更新数据。
一切工作正常,除了当我向下滚动主页时(工作出现在超出浏览器窗口底部的大型周计划表上)。如果我尝试在此处拖动可拖动元素,该元素会在鼠标光标上方显示与我向下滚动相同数量的像素。悬停状态仍然可以正常工作,并且功能正常,但看起来不正确。
我正在使用 jQuery 1.6.0 和 jQuery UI 1.8.12。
我确信我需要添加一个偏移函数,但我不知道在哪里应用它,或者是否有更好的方法。这是我的 .draggable()
初始化代码:
$('.job').draggable({
zIndex: 20,
revert: 'invalid',
helper: 'original',
distance: 30,
refreshPositions: true,
});
知道我可以做什么来解决这个问题吗?
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.
I'm sure there's a offset function I need to add but I don't know where to apply it, or if there's a better way. Here's my .draggable()
initialisation code:
$('.job').draggable({
zIndex: 20,
revert: 'invalid',
helper: 'original',
distance: 30,
refreshPositions: true,
});
Any idea what I can do to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
这可能是一个相关的错误报告,它已经存在很长一段时间了:http://bugs.jqueryui.com/ticket/3740< /a>
这似乎发生在我测试的每个浏览器上(Chrome、FF4、IE9)。有几种方法可以解决此问题:
1. 在 css 中使用
position:absolute;
。绝对定位的元素似乎不受影响。2. 确保父元素(如果是正文则为事件)已设置
overflow:auto;
。我的测试表明该解决方案修复了位置,但它禁用了自动滚动功能。您仍然可以使用鼠标滚轮或箭头键滚动。3.手动应用上述错误报告中建议的修复,并彻底测试是否会导致其他问题。
4.等待官方修复。它计划在 jQuery UI 1.9 上发布,尽管过去已经被推迟了几次。
5. 如果您确信这种情况在每个浏览器上都会发生,您可以将这些 hack 放入受影响的可拖动对象的事件中以纠正计算。不过,需要测试很多不同的浏览器,因此它只能作为最后的手段:
This might be a related bug report, it's been around for quite a while: http://bugs.jqueryui.com/ticket/3740
It seems to happen on every browser I tested (Chrome, FF4, IE9). There are a few ways you can work around this issue:
1. Use
position:absolute;
in your css. Absolutely positioned elements don't seem to be affected.2. Make sure the parent element (event if it's the body) has
overflow:auto;
set. My test showed that this solution fixes the position, but it disables the autoscroll functionality. You can still scroll using the mousewheel or the arrow keys.3. Apply the fix suggested in the above bug report manually and test thouroughly if it causes other problems.
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:
该解决方案无需调整任何内容的位置即可工作,您只需克隆元素并使其绝对定位即可。
helper 可以是一个需要返回 DOM 元素来拖动的函数。
This solution works without adjusting the positioning of anything, you just clone the element and make it absolutely positioned.
The helper can be a function which needs to return the DOM element to drag with.
这对我有用:
This worked for me:
抱歉写了另一个答案。
由于我无法使用上述答案中的任何解决方案,因此在找到另一个合理的解决方案之前,我进行了大量的谷歌搜索并进行了许多令人沮丧的小编辑。
只要任何父元素的
位置
设置为“相对”,就会出现此问题。我必须重新调整我的标记并更改我的 CSS,但通过从所有父项中删除此属性,我能够让.sortable()
在所有浏览器中正常工作。Sorry for writing another answer.
As none of the solutions in the above answer could be used by me I did a lot of Googling and made many frustrating minor edits before finding another reasonable solution.
This issue seems to occur whenever any of the parent elements have
position
set to 'relative'. I had to reshuffle my markup and alter my CSS but by removing this property from all parents, I was able to get.sortable()
working properly in all browsers.看来这个bug经常出现,而且每次都有不同的解决方案。上述任何一项或我在互联网上找到的其他任何内容都不起作用。
我正在使用 jQuery 1.9.1 和 Jquery UI 1.10.3。
这就是我修复它的方法:
适用于 FF、IE、Chrome,我尚未在其他浏览器中测试它。
It looks like this bug comes around very often, and every time there is a different solution. None of the above, or anything else I found on the internet worked.
I'm using jQuery 1.9.1 and Jquery UI 1.10.3.
This is how I fixed it:
Works in FF, IE, Chrome, I've not yet tested it in other browsers.
我删除了溢出:
并且效果很好!
I delete the overflow:
And it works perfectly!
此错误已移至 http://bugs.jqueryui.com/ticket/6817 并且,大约 5 天前(2013 年 12 月 16 日左右)的问题似乎终于得到了修复。现在的建议是使用 http://code.jquery 的最新开发版本.com/ui/jquery-ui-git.js 或等待
版本 1.10.4 应该包含此修复。编辑:
看来此修复现在可能是 http://bugs.jqueryui.com/ticket/9315 计划在 1.11 版本之前发布。使用上面链接的 jQuery 源代码控制版本似乎确实解决了我和 @Scott Alexander 的问题(下面的评论)。
This bug got moved to http://bugs.jqueryui.com/ticket/6817 and, as of about 5 days ago (Dec 16, 2013 or thereabout) appears to have finally been fixed. The suggestion right now is to use the latest development build from http://code.jquery.com/ui/jquery-ui-git.js or wait for
version 1.10.4 which should contain this fix.Edit:
It seems this fix might now be part of http://bugs.jqueryui.com/ticket/9315 which isn't scheduled to drop until version 1.11. Using the above linked source control version of jQuery does seem to fix the issue for me and @Scott Alexander (comment below).
这个错误这么长时间都没有得到修复,这似乎很了不起。对我来说,这在 Safari 和 Chrome(即 webkit 浏览器)上是一个问题,但在 IE7/8/9 和 Firefox 上都没有问题,它们都工作正常。
我发现设置绝对或固定,带或不带 !important 都没有帮助,所以最后我在拖动处理函数中添加了一行:
我期望需要使该行特定于 webkit,但奇怪的是它在任何地方都工作得很好。 (预计我很快就会发表评论说“呃不,实际上它弄乱了所有其他浏览器”。)
Seems remarkable that this bug should have gone unfixed so long. For me it's a problem on Safari and Chrome (i.e. the webkit browsers) but not on IE7/8/9 nor on Firefox which all work fine.
I found that setting absolute or fixed, with or without !important, didn't help so in the end I added a line to my drag handler function:
I was expecting to need to make that line webkit-specific but curiously it worked fine everywhere. (Expect a comment from me soon saying 'er no, actually it messed up all the other browsers'.)
我所做的是:
what i have done is:
我在 Firefox 21.0 上使用 JQuery Draggable,并且遇到了同样的问题。光标停留在辅助图像上方一英寸处。我认为这是 Jquery 本身的问题,目前还没有解决。我找到了解决此问题的方法,即使用可拖动的“cursorAt”属性。我按如下方式使用它,但可以根据其要求更改它。
注意: 这适用于所有浏览器,因此使用此代码后,请在所有浏览器中检查您的页面以获得正确的左侧和顶部位置。
I am using JQuery draggable on Firefox 21.0, and I am having the same problem. The cursor stays an inch above the helper image. I think this is an issue in the Jquery itself which has still not been resolved. I found a workaround to this problem, which is using the "cursorAt" property of the draggable. I used it as follows, but one can change this according to its requirement.
Note: This will be applicable for all browsers, so after using this code check your page in all browsers to get the correct left and top positions.
我不完全确定这在每种情况下都有效,但这个解决方法我只是在我必须测试的所有各种情况下为我工作(以及此处和其他地方给出的先前提出的解决方案都失败的地方)
(我将其提交给jQuery.ui 跟踪器看看他们对此有何看法..如果这个 4 年前的错误最终能得到纠正那就太酷了:/)
I'm not fully sure that this will work in every case but this workaround I just did work for me on all the various situations I had to test (and where the previous proposed solutions given here and elsewhere all failed)
( I submitted it to the jQuery.ui tracker to see what they think about it.. would be cool if that 4year old bug could be finally corrected :/ )
在改编了我所读到的有关该问题的所有内容之后,这对我有用。
*elementThatScrolls是具有overflow:auto的父级或祖先;
计算出滚动元素的位置,并在每次移动/拖动时添加到辅助元素的位置。
希望对某人有所帮助,因为我在这方面浪费了很多时间。
This is what worked for me, after adapting everything I have read on the problem.
*elementThatScrolls is the parent or ancestor with overflow:auto;
Works out the position of the scrolling element and adds to the position of the helper each time it is moved/dragged.
Hope that helps someone, as I wasted major time on this.
这似乎可以解决问题,而无需在父级中使用position:absolute:)
This seems to do the workaround without a need to use the position:absolute in the parent :)
我设法通过添加
display: inline-block
解决了同样的问题向拖动的项目
添加
position:relative
对我来说不起作用(jQuery 在拖动开始时用position:absolute
覆盖它)使用的 jQuery 版本:
I managed to fix the same issue with adding
display: inline-block
to the dragged items
Adding
position: relative
did NOT work for me (jQuery overrides it withposition: absolute
on drag start)Used jQuery versions:
我遇到了同样的问题,发现这一切都是因为引导导航栏“navbar-fixed-top”类的位置固定,它使我的
比
低 60px
顶部。因此,当我在网站中移动可拖动表单时,光标出现在表单顶部 60 像素处。你可以看到,在Chrome调试工具中,在Elements界面中,点击
标签,你会看到top和body之间有一个间隙。
因为我在我的应用程序中使用这个导航栏,并且不想修改每个表单的拖动初始化调用(按照 DarthJDG 的过程)。我决定以这种方式扩展可拖动小部件,并在可拖动初始化中简单地添加 yOffset 。
现在,当我使用引导导航栏初始化站点中的可拖动元素/表单时:
当然,您必须根据自己的站点进行试验以确定正确的 yOffset。
不管怎样,感谢 DarthJDG 为我指明了正确的方向。干杯!
I had the same problem and found that all this was because of a bootstrap navbar "navbar-fixed-top" class with position to fixed, that move down my
<body>
60px lower than<html>
top. So when I was moving draggable forms in my site, the cursor appeared 60px on top of form.You can see that in Chrome debugging tool, in the Elements interface, click on the
<body>
tag and you will see a gap between top and the body.Since I'm using this navbar all over my applications and didn't want to modify my initializing call to drag (as per DarthJDG's procedure) for each forms. I decided to extend draggable widget this way and simply add a yOffset in my draggable initialisation.
Now when I initialize the draggable element/form in a site using bootstrap navbar:
Of course, you will have to experiment to determine the right yOffset as per your own site.
Thanks anyway to DarthJDG who pointed me the right direction. Cheers!
我在顶部使用了粘性导航,并认为这就是导致其他人似乎都遇到的滚动问题的原因。我用cursorAt尝试了其他人的想法,我尝试了遏制,除了在CSS中取消设置我的html溢出之外没有任何效果!太疯狂了,一点 CSS 就会把一切搞砸。这就是我所做的,它就像一个魅力:
I'm using a sticky nav at the top and thought that was what was causing the scroll issue everyone else seems to be having. I tried everyone else's idea with cursorAt, I tried containment, and nothing worked EXCEPT unsetting my html overflow in the CSS! Insane how a little CSS will screw everything up. This is what I did and it works like a charm:
我放弃使用 jQueryUi Draggable 并转而使用一个更好的插件,名为 jquery.even.drag,它小得多代码大小,没有 jQueryUI 的那些废话。
我在容器内使用此插件制作了一个演示页面,该容器具有位置:固定
演示
希望这对某人有帮助,因为这个问题很大。
I quit using jQueryUi draggable and moved to use a better plugin called jquery.even.drag, much smaller code size, without all the crap jQueryUI has.
I've made a demo page using this plugin inside a container which has position:fixed
Demo
Hope this helps somebody, because this problem is BIG.
我遇到了类似的问题,仅适用于在 Windows 8 上运行的特定 IE 10。所有其他浏览器都工作正常。删除cursorAt: { top: 0 } 解决了问题。
I had similar issue, only with particular IE 10 running on Windows 8. All other browsers worked fine. Removing cursorAt: { top: 0 } solved the problem.
我在这里尝试了各种答案,包括更新 jQuery,发现这对我有用。我在最新的 chrome 和 IE 上测试了这个。我想这将是一个不同的解决方案的问题,具体取决于您的 UI 布局。
I tried various answers in here, including updating jQuery and found that this worked for me. I tested this on latest chrome and IE. I guess this will be a problem with different solutions depending on your UI layout.
为什么不获取光标位置呢?我正在使用可排序插件,并使用以下代码修复它:
Why not take the cursor position? i'am using the sortable plugin and i fix it with this code:
使用
appendTo: "body'
并使用cursorAt
设置位置。这对我来说效果很好。
Use
appendTo: "body'
and set position withcursorAt
.This works fine form me.