简体   繁体   English

jQuery UI 可拖动 position 受 scrollTop 影响? (包括 JSFiddle)

[英]jQuery UI draggable position being affected by scrollTop? (JSFiddle Included)

http://jsfiddle.net/mgJf2/2/ http://jsfiddle.net/mgJf2/2/

After including jQueryUI the only javascript is:包含 jQueryUI 后,唯一的 javascript 是:

$("#scrollbox").draggable({
    axis: 'y',
    drag: function(event, ui) {
         $(document).scrollTop(ui.position.top);
    },
});

The document scrolling has compounding effect on the drag-able div (which I thought had a fixed position).文档滚动对可拖动的 div 具有复合效果(我认为它具有固定位置)。 It causes the scroll to run away.它会导致卷轴跑掉。 Take out the document scrolling and it works fine, just without that page scrolling I want.取出文档滚动,它工作正常,只是没有我想要的页面滚动。

Any ideas?有任何想法吗?

Thanks!谢谢!

Here is the bug report, there is no fix yet: http://bugs.jqueryui.com/ticket/5009这是错误报告,还没有修复: http://bugs.jqueryui.com/ticket/5009

There is another known bug in jQuery UI without an official fix so far, fix is scheduled to version 1.9. jQuery UI 中还有一个已知的 bug,目前还没有官方修复,修复计划到 1.9 版。 There are a few ways to work around it, ranging from simple to ugly hack, check out my response over here: jQuery draggable shows helper in wrong place after page scrolled有几种方法可以解决它,从简单到丑陋的hack ,在此处查看我的回复:

I found that using a function on drag (fires onmousemove ) you can offset the position of helper.我发现在拖动时使用 function (触发onmousemove )可以抵消助手的 position。 You have to target helper in the example posted - I would imagine you would just substitute ui.item for ui.helper .您必须在发布的示例中定位助手 - 我想您只需将ui.item替换为ui.helper

drag: function(event, ui) { 
    var offset = $(this).offset();
    var yPos = offset.top; 
    ui.helper.css('margin-top', $(window).scrollTop() + 'px');
}

This should fix the position when document scrolled in Chrome and Safari.当文档在 Chrome 和 Safari 中滚动时,这应该可以修复 position。 Mozilla was the only Browser working correctly without this fix. Mozilla 是唯一一个在没有此修复的情况下可以正常工作的浏览器。 Have not tested on IE.没有在IE上测试过。

Cheers干杯

Keep it simple.把事情简单化。 Here is my Solution and it works fine.这是我的解决方案,它工作正常。

drag: function(event, ui) { 
    ui.helper.css('margin-top', -(t-c.scrollTop()));
},
start: function( event, ui ) {
    t = c.scrollTop();
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM