最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery UI draggable position being affected by scrollTop? (JSFiddle Included) - Stack Overflow

programmeradmin5浏览0评论

/

After including jQueryUI the only javascript is:

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

The document scrolling has pounding effect on the drag-able div (which I thought had a fixed position). 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!

http://jsfiddle/mgJf2/2/

After including jQueryUI the only javascript is:

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

The document scrolling has pounding effect on the drag-able div (which I thought had a fixed position). 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!

Share Improve this question asked May 25, 2011 at 12:27 fancyfancy 51.5k64 gold badges158 silver badges230 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

Here is the bug report, there is no fix yet: http://bugs.jqueryui./ticket/5009

There is another known bug in jQuery UI without an official fix so far, fix is scheduled to version 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

I found that using a function on drag (fires onmousemove) you can offset the position of helper. You have to target helper in the example posted - I would imagine you would just substitute ui.item for 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. Mozilla was the only Browser working correctly without this fix. Have not tested on 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();
},
发布评论

评论列表(0)

  1. 暂无评论